Skip to content

Commit dd802d6

Browse files
committed
Make BaseTestCase for Perl unit test
1 parent 879c690 commit dd802d6

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

dnf5.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ BuildRequires: perl(strict)
242242
BuildRequires: perl(Test::More)
243243
BuildRequires: perl(Test::Exception)
244244
BuildRequires: perl(warnings)
245+
BuildRequires: perl(FindBin)
245246
%endif
246247
%endif
247248

test/perl5/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ endif()
44

55

66
find_package(Perl REQUIRED)
7-
foreach(MODULE "strict" "Test::More" "Test::Exception" "warnings")
7+
foreach(MODULE "strict" "Test::More" "Test::Exception" "warnings" "FindBin")
88
message(STATUS "Checking for ${MODULE} Perl module")
99
execute_process(
1010
COMMAND "${PERL_EXECUTABLE}" -e "require ${MODULE}"

test/perl5/libdnf5/BaseTestCase.pm

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Copyright Contributors to the libdnf project.
2+
#
3+
# This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
4+
#
5+
# Libdnf is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 2 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# Libdnf is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with libdnf. If not, see <https://www.gnu.org/licenses/>.
17+
18+
package BaseTestCase;
19+
20+
use strict;
21+
use warnings;
22+
23+
use File::Temp qw(tempdir);
24+
use File::Spec::Functions 'catfile';
25+
26+
use libdnf5::base;
27+
use libdnf5::repo;
28+
29+
30+
my $PROJECT_BINARY_DIR = $ENV{"PROJECT_BINARY_DIR"};
31+
my $PROJECT_SOURCE_DIR = $ENV{"PROJECT_SOURCE_DIR"};
32+
33+
sub new {
34+
my $class = shift;
35+
my $self = {};
36+
37+
$self->{base} = new libdnf5::base::Base();
38+
39+
$self->{tmpdir} = tempdir("libdnf5_perl5_unittest.XXXX", TMPDIR => 1, CLEANUP => 1);
40+
41+
my $config = $self->{base}->get_config();
42+
$config->get_installroot_option()->set($libdnf5::conf::Option::Priority_RUNTIME, $self->{tmpdir}."/installroot");
43+
$config->get_cachedir_option()->set($libdnf5::conf::Option::Priority_RUNTIME, $self->{tmpdir}."/cache");
44+
$config->get_optional_metadata_types_option()->set($libdnf5::conf::Option::Priority_RUNTIME, $libdnf5::conf::OPTIONAL_METADATA_TYPES);
45+
46+
# Prevent loading plugins from host
47+
$config->get_plugins_option()->set(0);
48+
49+
my $vars = $self->{base}->get_vars()->get();
50+
$vars->set("arch", "x86_64");
51+
52+
$self->{base}->setup();
53+
54+
$self->{repo_sack} = $self->{base}->get_repo_sack();
55+
$self->{package_sack} = $self->{base}->get_rpm_package_sack();
56+
57+
return bless ($self, $class);
58+
}
59+
60+
sub tearDown {
61+
my $self = shift;
62+
# shutil.rmtree(self.temp_dir)
63+
}
64+
65+
sub _add_repo {
66+
# Add a repo from `repo_path`.
67+
my $self = shift;
68+
my $repoid = shift;
69+
my $repo_path = shift;
70+
my $load = shift // 1; # True is default
71+
72+
my $repo = $self->{repo_sack}->create_repo($repoid);
73+
$repo->get_config()->get_baseurl_option()->set($libdnf5::conf::Option::Priority_RUNTIME, "file://".$repo_path);
74+
if ($load) {
75+
$self->{repo_sack}->load_repos($libdnf5::repo::Repo::Type_AVAILABLE);
76+
}
77+
78+
return $repo
79+
}
80+
81+
sub add_repo_repomd {
82+
# Add a repo from PROJECT_SOURCE_DIR/test/data/repos-repomd/<repoid>/repodata
83+
my $self = shift;
84+
my $repoid = shift;
85+
my $load = shift // 1; # True is default
86+
87+
my $repo_path = catfile($PROJECT_SOURCE_DIR, "/test/data/repos-repomd", $repoid);
88+
return $self->_add_repo($repoid, $repo_path, $load)
89+
}
90+
91+
sub add_repo_rpm {
92+
# Add a repo from PROJECT_BINARY_DIR/test/data/repos-rpm/<repoid>/repodata
93+
my $self = shift;
94+
my $repoid = shift;
95+
my $load = shift // 1; # True is default
96+
97+
my $repo_path = catfile($PROJECT_BINARY_DIR, "test/data/repos-rpm", $repoid);
98+
return $self->_add_repo($repoid, $repo_path, $load)
99+
}
100+
101+
sub add_repo_solv {
102+
# Add a repo from PROJECT_SOURCE_DIR/test/data/repos-solv/<repoid>.repo
103+
my $self = shift;
104+
my $repoid = shift;
105+
106+
my $repo_path = catfile($PROJECT_SOURCE_DIR, "/test/data/repos-solv", $repoid.".repo");
107+
return $self->{repo_sack}->create_repo_from_libsolv_testcase($repoid, $repo_path);
108+
}
109+
110+
1;

0 commit comments

Comments
 (0)