Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWIG bindings and unit tests for Vars::detect_release() #1804

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(SWIG_COMPILE_OPTIONS
-Wno-strict-aliasing
-Wno-unused-function
-Wno-unused-parameter
-Wno-unused-but-set-variable
)

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Expand Down
1 change: 0 additions & 1 deletion bindings/libdnf5/base.i
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
#define CV __perl_CV

%template(BaseWeakPtr) libdnf5::WeakPtr<libdnf5::Base, false>;
%template(VarsWeakPtr) libdnf5::WeakPtr<libdnf5::Vars, false>;

%ignore std::vector<libdnf5::plugin::PluginInfo>::insert;
%ignore std::vector<libdnf5::plugin::PluginInfo>::pop;
Expand Down
14 changes: 14 additions & 0 deletions bindings/libdnf5/conf.i
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,22 @@ wrap_unique_ptr(StringUniquePtr, std::string);
%ignore ConfigParserOptionNotFoundError;
%include "libdnf5/conf/config_parser.hpp"

%newobject libdnf5::Vars::detect_release;
%extend libdnf5::Vars {
static char * detect_release(const BaseWeakPtr & base, const std::string & install_root_path) {
std::unique_ptr<std::string> release = libdnf5::Vars::detect_release(base, install_root_path);
if (release) {
char * release_cstr = new char[release->size() + 1];
strcpy(release_cstr, release->c_str());
return release_cstr;
}
return nullptr;
}
};
%ignore libdnf5::Vars::detect_release;
%ignore libdnf5::ReadOnlyVariableError;
%include "libdnf5/conf/vars.hpp"
%template(VarsWeakPtr) libdnf5::WeakPtr<libdnf5::Vars, false>;

%include "libdnf5/conf/config.hpp"
%include "libdnf5/conf/config_main.hpp"
Expand Down
1 change: 1 addition & 0 deletions dnf5.spec
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ BuildRequires: perl(strict)
BuildRequires: perl(Test::More)
BuildRequires: perl(Test::Exception)
BuildRequires: perl(warnings)
BuildRequires: perl(FindBin)
%endif
%endif

Expand Down
2 changes: 1 addition & 1 deletion test/perl5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ endif()


find_package(Perl REQUIRED)
foreach(MODULE "strict" "Test::More" "Test::Exception" "warnings")
foreach(MODULE "strict" "Test::More" "Test::Exception" "warnings" "FindBin")
message(STATUS "Checking for ${MODULE} Perl module")
execute_process(
COMMAND "${PERL_EXECUTABLE}" -e "require ${MODULE}"
Expand Down
110 changes: 110 additions & 0 deletions test/perl5/libdnf5/BaseTestCase.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright Contributors to the libdnf project.
#
# This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
#
# Libdnf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Libdnf is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libdnf. If not, see <https://www.gnu.org/licenses/>.

package BaseTestCase;

use strict;
use warnings;

use File::Temp qw(tempdir);
use File::Spec::Functions 'catfile';

use libdnf5::base;
use libdnf5::repo;


my $PROJECT_BINARY_DIR = $ENV{"PROJECT_BINARY_DIR"};
my $PROJECT_SOURCE_DIR = $ENV{"PROJECT_SOURCE_DIR"};

sub new {
my $class = shift;
my $self = {};

$self->{base} = new libdnf5::base::Base();

$self->{tmpdir} = tempdir("libdnf5_perl5_unittest.XXXX", TMPDIR => 1, CLEANUP => 1);

my $config = $self->{base}->get_config();
$config->get_installroot_option()->set($libdnf5::conf::Option::Priority_RUNTIME, $self->{tmpdir}."/installroot");
$config->get_cachedir_option()->set($libdnf5::conf::Option::Priority_RUNTIME, $self->{tmpdir}."/cache");
$config->get_optional_metadata_types_option()->set($libdnf5::conf::Option::Priority_RUNTIME, $libdnf5::conf::OPTIONAL_METADATA_TYPES);

# Prevent loading plugins from host
$config->get_plugins_option()->set(0);

my $vars = $self->{base}->get_vars()->get();
$vars->set("arch", "x86_64");

$self->{base}->setup();

$self->{repo_sack} = $self->{base}->get_repo_sack();
$self->{package_sack} = $self->{base}->get_rpm_package_sack();

return bless ($self, $class);
}

sub tearDown {
my $self = shift;
# shutil.rmtree(self.temp_dir)
}

sub _add_repo {
# Add a repo from `repo_path`.
my $self = shift;
my $repoid = shift;
my $repo_path = shift;
my $load = shift // 1; # True is default

my $repo = $self->{repo_sack}->create_repo($repoid);
$repo->get_config()->get_baseurl_option()->set($libdnf5::conf::Option::Priority_RUNTIME, "file://".$repo_path);
if ($load) {
$self->{repo_sack}->load_repos($libdnf5::repo::Repo::Type_AVAILABLE);
}

return $repo
}

sub add_repo_repomd {
# Add a repo from PROJECT_SOURCE_DIR/test/data/repos-repomd/<repoid>/repodata
my $self = shift;
my $repoid = shift;
my $load = shift // 1; # True is default

my $repo_path = catfile($PROJECT_SOURCE_DIR, "/test/data/repos-repomd", $repoid);
return $self->_add_repo($repoid, $repo_path, $load)
}

sub add_repo_rpm {
# Add a repo from PROJECT_BINARY_DIR/test/data/repos-rpm/<repoid>/repodata
my $self = shift;
my $repoid = shift;
my $load = shift // 1; # True is default

my $repo_path = catfile($PROJECT_BINARY_DIR, "test/data/repos-rpm", $repoid);
return $self->_add_repo($repoid, $repo_path, $load)
}

sub add_repo_solv {
# Add a repo from PROJECT_SOURCE_DIR/test/data/repos-solv/<repoid>.repo
my $self = shift;
my $repoid = shift;

my $repo_path = catfile($PROJECT_SOURCE_DIR, "/test/data/repos-solv", $repoid.".repo");
return $self->{repo_sack}->create_repo_from_libsolv_testcase($repoid, $repo_path);
}

1;
43 changes: 43 additions & 0 deletions test/perl5/libdnf5/conf/test_vars.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright Contributors to the libdnf project.
#
# This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
#
# Libdnf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Libdnf is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libdnf. If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use Test::More;

use FindBin;
use lib "$FindBin::Bin/.."; # Add to search path
use BaseTestCase;

use File::Spec::Functions 'catfile';

use libdnf5::conf;


# test_detect_release
{
my $test = new BaseTestCase();

my $installroot = $test->{base}->get_config()->get_installroot_option()->get_value();

# Cannot detect release in nonexistent directory, return undef
my $release = libdnf5::conf::Vars::detect_release($test->{base}->get_weak_ptr(), catfile($installroot, "nonexist"));
is($release, undef);
}

done_testing();
11 changes: 11 additions & 0 deletions test/python3/libdnf5/conf/test_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@
# You should have received a copy of the GNU General Public License
# along with libdnf. If not, see <https://www.gnu.org/licenses/>.

import os

import libdnf5

import base_test_case


class TestVars(base_test_case.BaseTestCase):
def test_getting_undefined_variable(self):
vars = self.base.get_vars()
self.assertRaises(IndexError, vars.get_value, "undefined")

def test_detect_release(self):
installroot = self.base.get_config().installroot
# Cannot detect release in nonexistent directory, return None
release = libdnf5.conf.Vars.detect_release(
self.base.get_weak_ptr(), os.path.join(installroot, "nonexist"))
self.assertEqual(release, None)
33 changes: 33 additions & 0 deletions test/ruby/libdnf5/conf/test_vars.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright Contributors to the libdnf project.
#
# This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
#
# Libdnf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Libdnf is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libdnf. If not, see <https://www.gnu.org/licenses/>.

require 'test/unit'
include Test::Unit::Assertions

require 'libdnf5/conf'

require 'base_test_case'


class TestVars < BaseTestCase
def test_detect_release()
installroot = @base.get_config().get_installroot_option().get_value()
# Cannot detect release in nonexistent directory, return nil
release = Conf::Vars::detect_release(@base.get_weak_ptr(), File.join(installroot, "nonexist"))
assert_equal(nil, release)
end
end
Loading