|
5 | 5 | from pathlib import Path |
6 | 6 |
|
7 | 7 |
|
8 | | -PACKAGE_INIT = Path(__file__).resolve().parents[1] / ( |
9 | | - "src/giant_python/__init__.py" |
| 8 | +PACKAGE_INIT = ( |
| 9 | + Path(__file__).resolve().parents[1] / "src" / "giant_python" / "__init__.py" |
10 | 10 | ) |
11 | 11 |
|
12 | 12 |
|
13 | 13 | class PackageMetadataTest(unittest.TestCase): |
14 | 14 | """Tests for package metadata declarations.""" |
15 | 15 |
|
16 | | - def test_init_module_is_valid_and_exports_version(self): |
17 | | - """The package __init__ module should parse and export __version__.""" |
18 | | - module = ast.parse(PACKAGE_INIT.read_text(encoding="utf-8")) |
| 16 | + def test_init_module_is_valid_and_has_single_version_line(self): |
| 17 | + """Only one ``__version__`` line may exist so CI sed bumps stay safe. |
19 | 18 |
|
20 | | - all_assignment = next( |
| 19 | + The shared AIND ``release-bump-version`` workflow rewrites every line |
| 20 | + that contains ``__version__`` with:: |
| 21 | +
|
| 22 | + sed 's|__version__.*|__version__ = "X.Y.Z"|' |
| 23 | +
|
| 24 | + Listing ``"__version__"`` in ``__all__`` therefore produces invalid |
| 25 | + Python on the next bump. Keep a single assignment line instead. |
| 26 | + """ |
| 27 | + source = PACKAGE_INIT.read_text(encoding="utf-8") |
| 28 | + module = ast.parse(source) |
| 29 | + |
| 30 | + version_lines = [ |
| 31 | + line for line in source.splitlines() if "__version__" in line |
| 32 | + ] |
| 33 | + self.assertEqual(len(version_lines), 1) |
| 34 | + self.assertRegex( |
| 35 | + version_lines[0].strip(), |
| 36 | + r'^__version__ = "[0-9]+\.[0-9]+\.[0-9]+"$', |
| 37 | + ) |
| 38 | + |
| 39 | + version_assignment = next( |
21 | 40 | node |
22 | 41 | for node in module.body |
23 | 42 | if isinstance(node, ast.Assign) |
24 | 43 | and any( |
25 | | - isinstance(target, ast.Name) and target.id == "__all__" |
| 44 | + isinstance(target, ast.Name) and target.id == "__version__" |
26 | 45 | for target in node.targets |
27 | 46 | ) |
28 | 47 | ) |
29 | | - self.assertIsInstance(all_assignment.value, (ast.List, ast.Tuple)) |
30 | | - exported_names = [ |
31 | | - elt.value |
32 | | - for elt in all_assignment.value.elts |
33 | | - if isinstance(elt, ast.Constant) and isinstance(elt.value, str) |
34 | | - ] |
35 | | - |
36 | | - self.assertIn("__version__", exported_names) |
| 48 | + self.assertIsInstance(version_assignment.value, ast.Constant) |
| 49 | + self.assertIsInstance(version_assignment.value.value, str) |
| 50 | + self.assertRegex( |
| 51 | + version_assignment.value.value, r"^[0-9]+\.[0-9]+\.[0-9]+$" |
| 52 | + ) |
37 | 53 |
|
38 | 54 |
|
39 | 55 | if __name__ == "__main__": |
|
0 commit comments