Skip to content

Commit 4acfd67

Browse files
authored
Merge pull request #38 from hartwork/more-test-coverage
gentoo-tree-diff: More test coverage
2 parents cf87f5d + 6f06af4 commit 4acfd67

4 files changed

Lines changed: 97 additions & 6 deletions

File tree

binary_gentoo/internal/cli/tests/test_tree_diff.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,56 @@
88
from unittest import TestCase
99
from unittest.mock import patch
1010

11-
from ..tree_diff import main
11+
from parameterized import parameterized
12+
13+
from ..tree_diff import (_replace_special_keywords_for_ebuild, enrich_config, main,
14+
parse_command_line)
15+
16+
17+
class ReplaceSpecialKeywordsTest(TestCase):
18+
@parameterized.expand([
19+
('no ops', {'one', '~two'}, {'three', '~four'}, {'one', '~two'}),
20+
('star op', {'one', '~two', '*'}, {'three', '~four'}, {'one', '~two', 'three'}),
21+
('tilde star op', {'one', '~two', '~*'}, {'three', '~four'}, {'one', '~two', '~four'}),
22+
('double star op', {'one', '~two', '**'}, {'three',
23+
'~four'}, {'one', '~two', 'three', '~four'}),
24+
('start op + tilde star op', {'one', '~two', '*',
25+
'~*'}, {'three', '~four'}, {'one', '~two', 'three',
26+
'~four'}),
27+
])
28+
def test(self, _, accept_keywords, ebuild_keywords, expected_effective_keywords):
29+
actual_effective_keywords = _replace_special_keywords_for_ebuild(
30+
accept_keywords, ebuild_keywords)
31+
self.assertEqual(actual_effective_keywords, expected_effective_keywords)
32+
33+
34+
class EnrichConfigTest(TestCase):
35+
magic_keywords = 'one two ~*'
36+
37+
@classmethod
38+
def _fake_subprocess_check_output(cls, argv):
39+
if argv == ['portageq', 'envvar', 'ACCEPT_KEYWORDS']:
40+
stdout = cls.magic_keywords
41+
else:
42+
stdout = f'Hello from: {" ".join(argv)}'
43+
return (stdout + '\n').encode('ascii')
44+
45+
def test_given__empty(self):
46+
config = parse_command_line(['gentoo-tree-diff', '--keywords', '', 'dir1', 'dir2'])
47+
with self.assertRaises(ValueError):
48+
enrich_config(config)
49+
50+
def test_given__not_empty(self):
51+
config = parse_command_line(
52+
['gentoo-tree-diff', '--keywords', 'one ~two *', 'dir1', 'dir2'])
53+
enrich_config(config)
54+
self.assertEqual(config.keywords, {'one', 'two', '~two', '*'})
55+
56+
def test_not_given__auto_detection(self):
57+
config = parse_command_line(['gentoo-tree-diff', 'dir1', 'dir2'])
58+
with patch('subprocess.check_output', self._fake_subprocess_check_output):
59+
enrich_config(config)
60+
self.assertEqual(config.keywords, {'one', 'two', '~*'})
1261

1362

1463
class MainTest(TestCase):
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (C) 2021 Sebastian Pipping <sebastian@pipping.org>
2+
# Licensed under GNU Affero GPL version 3 or later
3+
4+
from dataclasses import dataclass
5+
from subprocess import call
6+
from tempfile import TemporaryDirectory
7+
from typing import List
8+
from unittest import TestCase
9+
from unittest.mock import patch
10+
11+
from parameterized import parameterized
12+
13+
from ..tree_sync import main
14+
15+
16+
@dataclass
17+
class RunRecord:
18+
call_args_list: List["call"]
19+
20+
21+
class MainTest(TestCase):
22+
@staticmethod
23+
def _run_gentoo_tree_sync_with_subprocess_mocked(backup: bool) -> RunRecord:
24+
with TemporaryDirectory() as temp_portdir_old,\
25+
TemporaryDirectory() as temp_portdir_new:
26+
argv = ['gentoo-tree-sync']
27+
if backup:
28+
argv += ['--backup-to', temp_portdir_old]
29+
argv.append(temp_portdir_new)
30+
31+
with patch('sys.argv', argv), patch('subprocess.check_call') as check_call_mock:
32+
main()
33+
34+
return RunRecord(call_args_list=check_call_mock.call_args_list, )
35+
36+
@parameterized.expand([
37+
('with backup', True),
38+
('without backup', False),
39+
])
40+
def test_success_invokes_docker(self, _, backup: bool):
41+
run_record = self._run_gentoo_tree_sync_with_subprocess_mocked(backup=backup)
42+
43+
docker_run_call = run_record.call_args_list[0]
44+
self.assertEqual(docker_run_call.args[0][:2], ['docker', 'run'])
45+
46+
self.assertEqual(len(run_record.call_args_list), 1)

binary_gentoo/internal/cli/tree_diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def enrich_config(config):
105105

106106
# add stable keywords for testing keywords
107107
config.keywords = {kw for kw in config.keywords.split(" ") if kw}
108-
config.keywords |= {k[1:] for k in config.keywords if k.startswith('~')}
108+
config.keywords |= {k[1:] for k in config.keywords if k.startswith('~') and not k == '~*'}
109109

110110
return config
111111

binary_gentoo/internal/cli/tree_sync.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,3 @@ def main():
103103
with exception_reporting():
104104
config = parse_command_line(sys.argv)
105105
sync(config)
106-
107-
108-
if __name__ == '__main__':
109-
main()

0 commit comments

Comments
 (0)