aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorArthur Zamarin <arthurzam@gentoo.org>2022-03-20 19:52:33 +0200
committerArthur Zamarin <arthurzam@gentoo.org>2022-04-19 10:41:30 +0300
commit8ef3abc479c26b964696c6d40e6caf551a43e375 (patch)
tree3b3a408e81def0eda7248f6a72ae973e5afe258c /tests
parentImprove pkgdev commit tests (diff)
downloadpkgdev-8ef3abc479c26b964696c6d40e6caf551a43e375.tar.gz
pkgdev-8ef3abc479c26b964696c6d40e6caf551a43e375.tar.bz2
pkgdev-8ef3abc479c26b964696c6d40e6caf551a43e375.zip
Add configuration support
Resolves: https://github.com/pkgcore/pkgdev/issues/26 Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/scripts/test_cli.py90
-rw-r--r--tests/scripts/test_pkgdev_commit.py24
-rw-r--r--tests/scripts/test_pkgdev_push.py2
-rw-r--r--tests/scripts/test_pkgdev_showkw.py21
4 files changed, 125 insertions, 12 deletions
diff --git a/tests/scripts/test_cli.py b/tests/scripts/test_cli.py
new file mode 100644
index 0000000..752f36d
--- /dev/null
+++ b/tests/scripts/test_cli.py
@@ -0,0 +1,90 @@
+import textwrap
+
+import pytest
+from pkgdev import cli
+from snakeoil.cli import arghparse
+
+
+class TestConfigFileParser:
+
+ @pytest.fixture(autouse=True)
+ def _create_argparser(self, tmp_path):
+ self.config_file = str(tmp_path / 'config')
+ self.parser = arghparse.ArgumentParser(prog='pkgdev cli_test')
+ self.namespace = arghparse.Namespace()
+ self.config_parser = cli.ConfigFileParser(self.parser)
+
+ def test_no_configs(self):
+ config = self.config_parser.parse_config(())
+ assert config.sections() == []
+ namespace = self.config_parser.parse_config_options(self.namespace)
+ assert vars(namespace) == {}
+
+ def test_ignored_configs(self):
+ # nonexistent config files are ignored
+ config = self.config_parser.parse_config(('foo', 'bar'))
+ assert config.sections() == []
+
+ def test_bad_config_format_no_section(self, capsys):
+ with open(self.config_file, 'w') as f:
+ f.write('foobar\n')
+ with pytest.raises(SystemExit) as excinfo:
+ self.config_parser.parse_config((self.config_file,))
+ out, err = capsys.readouterr()
+ assert not out
+ assert 'parsing config file failed: File contains no section headers' in err
+ assert self.config_file in err
+ assert excinfo.value.code == 2
+
+ def test_bad_config_format(self, capsys):
+ with open(self.config_file, 'w') as f:
+ f.write(textwrap.dedent("""
+ [DEFAULT]
+ foobar
+ """))
+ with pytest.raises(SystemExit) as excinfo:
+ self.config_parser.parse_config((self.config_file,))
+ out, err = capsys.readouterr()
+ assert not out
+ assert 'parsing config file failed: Source contains parsing errors' in err
+ assert excinfo.value.code == 2
+
+ def test_nonexistent_config_options(self, capsys):
+ """Nonexistent parser arguments don't cause errors."""
+ with open(self.config_file, 'w') as f:
+ f.write(textwrap.dedent("""
+ [DEFAULT]
+ cli_test.foo=bar
+ """))
+ with pytest.raises(SystemExit) as excinfo:
+ self.config_parser.parse_config_options(None, configs=(self.config_file,))
+ out, err = capsys.readouterr()
+ assert not out
+ assert 'failed loading config: unknown arguments: --foo=bar' in err
+ assert excinfo.value.code == 2
+
+ def test_config_options_other_prog(self):
+ self.parser.add_argument('--foo')
+ with open(self.config_file, 'w') as f:
+ f.write(textwrap.dedent("""
+ [DEFAULT]
+ other.foo=bar
+ """))
+ namespace = self.parser.parse_args(['--foo', 'foo'])
+ assert namespace.foo == 'foo'
+ # config args don't override not matching namespace attrs
+ namespace = self.config_parser.parse_config_options(namespace, configs=[self.config_file])
+ assert namespace.foo == 'foo'
+
+ def test_config_options(self):
+ self.parser.add_argument('--foo')
+ with open(self.config_file, 'w') as f:
+ f.write(textwrap.dedent("""
+ [DEFAULT]
+ cli_test.foo=bar
+ """))
+ namespace = self.parser.parse_args(['--foo', 'foo'])
+ assert namespace.foo == 'foo'
+ # config args override matching namespace attrs
+ namespace = self.config_parser.parse_config_options(namespace, configs=[self.config_file])
+ assert namespace.foo == 'bar'
diff --git a/tests/scripts/test_pkgdev_commit.py b/tests/scripts/test_pkgdev_commit.py
index ac3fc8b..550671f 100644
--- a/tests/scripts/test_pkgdev_commit.py
+++ b/tests/scripts/test_pkgdev_commit.py
@@ -167,7 +167,7 @@ class TestPkgdevCommit:
self.cache_dir = str(tmp_path)
self.scan_args = ['--pkgcheck-scan', f'--config no --cache-dir {self.cache_dir}']
# args for running pkgdev like a script
- self.args = ['pkgdev', 'commit'] + self.scan_args
+ self.args = ['pkgdev', 'commit', '--config', 'no'] + self.scan_args
def test_empty_repo(self, capsys, repo, make_git_repo):
git_repo = make_git_repo(repo.location, commit=True)
@@ -914,6 +914,28 @@ class TestPkgdevCommit:
self.script()
assert excinfo.value.code == 0
+ def test_config_opts(self, capsys, repo, make_git_repo, tmp_path):
+ config_file = str(tmp_path / 'config')
+ with open(config_file, 'w') as f:
+ f.write(textwrap.dedent("""
+ [DEFAULT]
+ commit.scan=
+ """))
+
+ git_repo = make_git_repo(repo.location)
+ repo.create_ebuild('cat/pkg-0')
+ git_repo.add_all('cat/pkg-0')
+ repo.create_ebuild('cat/pkg-1', license='')
+ git_repo.add_all('cat/pkg-1', commit=False)
+ with patch('sys.argv', ['pkgdev', 'commit', '--config', config_file] + self.scan_args), \
+ pytest.raises(SystemExit) as excinfo, \
+ chdir(git_repo.path):
+ self.script()
+ out, err = capsys.readouterr()
+ assert excinfo.value.code == 1
+ assert not err
+ assert 'MissingLicense' in out
+
def test_failed_manifest(self, capsys, repo, make_git_repo):
git_repo = make_git_repo(repo.location)
repo.create_ebuild('cat/pkg-0')
diff --git a/tests/scripts/test_pkgdev_push.py b/tests/scripts/test_pkgdev_push.py
index cc13fb1..4eeee4b 100644
--- a/tests/scripts/test_pkgdev_push.py
+++ b/tests/scripts/test_pkgdev_push.py
@@ -65,7 +65,7 @@ class TestPkgdevPush:
@pytest.fixture(autouse=True)
def _setup(self, tmp_path, make_repo, make_git_repo):
self.cache_dir = str(tmp_path / 'cache')
- self.scan_args = ['--pkgcheck-scan', f'--config no --cache-dir {self.cache_dir}']
+ self.scan_args = ['--config', 'no', '--pkgcheck-scan', f'--config no --cache-dir {self.cache_dir}']
# args for running pkgdev like a script
self.args = ['pkgdev', 'push'] + self.scan_args
diff --git a/tests/scripts/test_pkgdev_showkw.py b/tests/scripts/test_pkgdev_showkw.py
index 538744a..51348f2 100644
--- a/tests/scripts/test_pkgdev_showkw.py
+++ b/tests/scripts/test_pkgdev_showkw.py
@@ -3,8 +3,8 @@ from typing import NamedTuple, List
from unittest.mock import patch
import pytest
+from snakeoil.contexts import chdir
from pkgdev.scripts import run
-from snakeoil.contexts import chdir, os_environ
class Profile(NamedTuple):
"""Profile record used to create profiles in a repository."""
@@ -19,7 +19,7 @@ class TestPkgdevShowkwParseArgs:
def test_missing_target(self, capsys, tool):
with pytest.raises(SystemExit):
- tool.parse_args(['showkw'])
+ tool.parse_args(['showkw', '--config', 'no'])
captured = capsys.readouterr()
assert captured.err.strip() == (
'pkgdev showkw: error: missing target argument and not in a supported repo')
@@ -27,7 +27,7 @@ class TestPkgdevShowkwParseArgs:
def test_unknown_arches(self, capsys, tool, make_repo):
repo = make_repo(arches=['amd64'])
with pytest.raises(SystemExit):
- tool.parse_args(['showkw', '-a', 'unknown', '-r', repo.location])
+ tool.parse_args(['showkw', '--config', 'no', '-a', 'unknown', '-r', repo.location])
captured = capsys.readouterr()
assert captured.err.strip() == (
"pkgdev showkw: error: unknown arch: 'unknown' (choices: amd64)")
@@ -35,6 +35,7 @@ class TestPkgdevShowkwParseArgs:
class TestPkgdevShowkw:
script = partial(run, 'pkgdev')
+ base_args = ['pkgdev', 'showkw', '--config', 'n']
def _create_repo(self, make_repo):
repo = make_repo(arches=['amd64', 'ia64', 'mips', 'x86'])
@@ -47,7 +48,7 @@ class TestPkgdevShowkw:
return repo
def _run_and_parse(self, capsys, *args):
- with patch('sys.argv', ['pkgdev', 'showkw', "--format", "presto", *args]), \
+ with patch('sys.argv', [*self.base_args, "--format", "presto", *args]), \
pytest.raises(SystemExit) as excinfo:
self.script()
assert excinfo.value.code == None
@@ -63,7 +64,7 @@ class TestPkgdevShowkw:
def test_match(self, capsys, make_repo):
repo = self._create_repo(make_repo)
repo.create_ebuild('foo/bar-0')
- with patch('sys.argv', ['pkgdev', 'showkw', '-r', repo.location, 'foo/bar']), \
+ with patch('sys.argv', [*self.base_args, '-r', repo.location, 'foo/bar']), \
pytest.raises(SystemExit) as excinfo:
self.script()
assert excinfo.value.code == None
@@ -74,7 +75,7 @@ class TestPkgdevShowkw:
def test_match_short_name(self, capsys, make_repo):
repo = self._create_repo(make_repo)
repo.create_ebuild('foo/bar-0')
- with patch('sys.argv', ['pkgdev', 'showkw', '-r', repo.location, 'bar']), \
+ with patch('sys.argv', [*self.base_args, '-r', repo.location, 'bar']), \
pytest.raises(SystemExit) as excinfo:
self.script()
assert excinfo.value.code == None
@@ -85,7 +86,7 @@ class TestPkgdevShowkw:
def test_match_cwd_repo(self, capsys, make_repo):
repo = self._create_repo(make_repo)
repo.create_ebuild('foo/bar-0')
- with patch('sys.argv', ['pkgdev', 'showkw', 'foo/bar']), \
+ with patch('sys.argv', [*self.base_args, 'foo/bar']), \
pytest.raises(SystemExit) as excinfo, \
chdir(repo.location):
self.script()
@@ -97,7 +98,7 @@ class TestPkgdevShowkw:
def test_match_cwd_pkg(self, capsys, make_repo):
repo = self._create_repo(make_repo)
repo.create_ebuild('foo/bar-0')
- with patch('sys.argv', ['pkgdev', 'showkw']), \
+ with patch('sys.argv', self.base_args), \
pytest.raises(SystemExit) as excinfo, \
chdir(repo.location + '/foo/bar'):
self.script()
@@ -107,7 +108,7 @@ class TestPkgdevShowkw:
def test_no_matches(self, capsys, make_repo):
repo = self._create_repo(make_repo)
- with patch('sys.argv', ['pkgdev', 'showkw', '-r', repo.location, 'foo/bar']), \
+ with patch('sys.argv', [*self.base_args, '-r', repo.location, 'foo/bar']), \
pytest.raises(SystemExit) as excinfo:
self.script()
assert excinfo.value.code == 1
@@ -165,7 +166,7 @@ class TestPkgdevShowkw:
repo = self._create_repo(make_repo)
repo.create_ebuild('foo/bar-0', keywords=('amd64', '~ia64', '~mips', '~x86'))
repo.create_ebuild('foo/bar-1', keywords=('~amd64', '~ia64', '~mips', 'x86'))
- with patch('sys.argv', ['pkgdev', 'showkw', '-r', repo.location, 'foo/bar', "--collapse", arg]), \
+ with patch('sys.argv', [*self.base_args, '-r', repo.location, 'foo/bar', "--collapse", arg]), \
pytest.raises(SystemExit) as excinfo:
self.script()
out, err = capsys.readouterr()