|
2 | 2 | # Licensed under GNU Affero GPL version 3 or later |
3 | 3 |
|
4 | 4 | import os |
| 5 | +from contextlib import contextmanager |
5 | 6 | from io import StringIO |
6 | 7 | from tempfile import TemporaryDirectory |
7 | 8 | from textwrap import dedent |
|
10 | 11 |
|
11 | 12 | from parameterized import parameterized |
12 | 13 |
|
13 | | -from ..tree_diff import (_replace_special_keywords_for_ebuild, enrich_config, main, |
14 | | - parse_command_line) |
| 14 | +from ..tree_diff import (_replace_special_keywords_for_ebuild, enrich_config, |
| 15 | + iterate_new_and_changed_ebuilds, main, parse_command_line) |
15 | 16 |
|
16 | 17 |
|
17 | 18 | class ReplaceSpecialKeywordsTest(TestCase): |
@@ -60,6 +61,120 @@ def test_not_given__auto_detection(self): |
60 | 61 | self.assertEqual(config.keywords, {'one', 'two', '~*'}) |
61 | 62 |
|
62 | 63 |
|
| 64 | +class IterateNewAndChangedEbuildsTest(TestCase): |
| 65 | + @classmethod |
| 66 | + @contextmanager |
| 67 | + def _tempdir_config( |
| 68 | + cls, |
| 69 | + keywords: str, |
| 70 | + pessimistic: bool = False, |
| 71 | + ): |
| 72 | + with TemporaryDirectory() as temp_old_portdir, \ |
| 73 | + TemporaryDirectory() as temp_new_portdir: |
| 74 | + argv = ['gentoo-tree-diff', '--keywords', keywords] |
| 75 | + if pessimistic: |
| 76 | + argv.append('--pessimistic') |
| 77 | + argv += [temp_old_portdir, temp_new_portdir] |
| 78 | + |
| 79 | + config = parse_command_line(argv) |
| 80 | + enrich_config(config) |
| 81 | + |
| 82 | + yield config |
| 83 | + |
| 84 | + @classmethod |
| 85 | + def _create_ebuild(cls, |
| 86 | + portdir, |
| 87 | + ebuild_filename, |
| 88 | + keywords: str = None, |
| 89 | + extra_content: str = None): |
| 90 | + filename = os.path.join(portdir, ebuild_filename) |
| 91 | + os.makedirs(os.path.dirname(filename), exist_ok=True) |
| 92 | + with open(filename, 'w') as f: |
| 93 | + if keywords is not None: |
| 94 | + print(f'KEYWORDS="{keywords}"', file=f) |
| 95 | + if extra_content is not None: |
| 96 | + print(extra_content, file=f) |
| 97 | + f.flush() |
| 98 | + with open(filename) as f: |
| 99 | + print('CONTENT', f.read()) |
| 100 | + |
| 101 | + def test_new_live_ebuild_ignored_by_filename(self): |
| 102 | + keywords = 'one' |
| 103 | + with self._tempdir_config(keywords=keywords) as config: |
| 104 | + self._create_ebuild(config.new_portdir, 'cat/pkg/pkg-123.ebuild', keywords=keywords) |
| 105 | + self._create_ebuild(config.new_portdir, 'cat/pkg/pkg-9999.ebuild', keywords=keywords) |
| 106 | + actual_news = list(iterate_new_and_changed_ebuilds(config)) |
| 107 | + self.assertEqual(actual_news, ['cat/pkg-123']) |
| 108 | + |
| 109 | + def test_new_ebuild_without_keyword_line_ignored(self): |
| 110 | + keywords = 'one' |
| 111 | + with self._tempdir_config(keywords='one') as config: |
| 112 | + self._create_ebuild(config.new_portdir, 'cat/pkg/pkg-123.ebuild', keywords=keywords) |
| 113 | + self._create_ebuild(config.new_portdir, 'cat/pkg/pkg-456.ebuild', keywords=None) |
| 114 | + actual_news = list(iterate_new_and_changed_ebuilds(config)) |
| 115 | + self.assertEqual(actual_news, ['cat/pkg-123']) |
| 116 | + |
| 117 | + def test_new_ebuild_without_matching_keyword_ignored(self): |
| 118 | + keywords = 'one' |
| 119 | + with self._tempdir_config(keywords='one') as config: |
| 120 | + self._create_ebuild(config.new_portdir, 'cat/pkg/pkg-123.ebuild', keywords=keywords) |
| 121 | + self._create_ebuild(config.new_portdir, 'cat/pkg/pkg-456.ebuild', keywords='other') |
| 122 | + actual_news = list(iterate_new_and_changed_ebuilds(config)) |
| 123 | + self.assertEqual(actual_news, ['cat/pkg-123']) |
| 124 | + |
| 125 | + def test_unchanged_file_ignored(self): |
| 126 | + keywords = 'one' |
| 127 | + ebuild_filename = 'cat/pkg/pkg-123.ebuild' |
| 128 | + with self._tempdir_config(keywords=keywords) as config: |
| 129 | + self._create_ebuild(config.old_portdir, ebuild_filename, keywords=keywords) |
| 130 | + self._create_ebuild(config.new_portdir, ebuild_filename, keywords=keywords) |
| 131 | + actual_news = list(iterate_new_and_changed_ebuilds(config)) |
| 132 | + self.assertEqual(actual_news, []) |
| 133 | + |
| 134 | + def test_changed_ebuild_without_matching_keywords_ignored(self): |
| 135 | + keywords = 'one' |
| 136 | + ebuild_filename = 'cat/pkg/pkg-123.ebuild' |
| 137 | + with self._tempdir_config(keywords='one') as config: |
| 138 | + self._create_ebuild(config.old_portdir, ebuild_filename, keywords=keywords) |
| 139 | + self._create_ebuild(config.new_portdir, ebuild_filename, keywords='other') |
| 140 | + actual_news = list(iterate_new_and_changed_ebuilds(config)) |
| 141 | + self.assertEqual(actual_news, []) |
| 142 | + |
| 143 | + @parameterized.expand([ |
| 144 | + ('pessimistic, not ignored', True), |
| 145 | + ('not pessimistic, ignored', False), |
| 146 | + ]) |
| 147 | + def test_changed_ebuild_with_matching_identical_keywords(self, _, pessimistic: bool): |
| 148 | + keywords = 'one' |
| 149 | + ebuild_filename = 'cat/pkg/pkg-123.ebuild' |
| 150 | + with self._tempdir_config(keywords=keywords, pessimistic=pessimistic) as config: |
| 151 | + self._create_ebuild(config.old_portdir, |
| 152 | + ebuild_filename, |
| 153 | + keywords=keywords, |
| 154 | + extra_content='# old') |
| 155 | + self._create_ebuild(config.new_portdir, |
| 156 | + ebuild_filename, |
| 157 | + keywords=keywords, |
| 158 | + extra_content='# new') |
| 159 | + actual_news = list(iterate_new_and_changed_ebuilds(config)) |
| 160 | + expected_news = ['cat/pkg-123'] if pessimistic else [] |
| 161 | + self.assertEqual(actual_news, expected_news) |
| 162 | + |
| 163 | + @parameterized.expand([ |
| 164 | + ('pessimistic, not ignored', True), |
| 165 | + ('not pessimistic, not ignored', False), |
| 166 | + ]) |
| 167 | + def test_changed_ebuild_with_matching_changed_keywords(self, _, pessimistic: bool): |
| 168 | + ebuild_filename = 'cat/pkg/pkg-123.ebuild' |
| 169 | + with self._tempdir_config(keywords='one', pessimistic=pessimistic) as config: |
| 170 | + self._create_ebuild(config.old_portdir, ebuild_filename, |
| 171 | + keywords='~one') # did not match keywords, previously |
| 172 | + self._create_ebuild(config.new_portdir, ebuild_filename, |
| 173 | + keywords='one') # just went stable, now matches keywords |
| 174 | + actual_news = list(iterate_new_and_changed_ebuilds(config)) |
| 175 | + self.assertEqual(actual_news, ['cat/pkg-123']) |
| 176 | + |
| 177 | + |
63 | 178 | class MainTest(TestCase): |
64 | 179 | @staticmethod |
65 | 180 | def _create_file_with_keywords(filename, keywords): |
|
0 commit comments