|
8 | 8 | import sys |
9 | 9 | import tempfile |
10 | 10 | import unittest |
| 11 | +from contextlib import contextmanager |
11 | 12 | from pathlib import Path |
12 | 13 |
|
13 | 14 | BASE_DIR = Path(__file__).parent.parent |
14 | 15 | SCRIPT_PATH = BASE_DIR / "scripts" / "bump-version.py" |
15 | 16 |
|
16 | | -spec = importlib.util.spec_from_file_location("bump_version", str(SCRIPT_PATH)) |
17 | | -bump_version = importlib.util.module_from_spec(spec) |
18 | | -# Assigning spec to the module is required for importlib.reload() to work |
19 | | -bump_version.__spec__ = spec |
20 | | -sys.modules["bump_version"] = bump_version |
21 | | -spec.loader.exec_module(bump_version) |
| 17 | + |
| 18 | +def load_bump_version(*, ci: bool = False): |
| 19 | + """ |
| 20 | + Load bump-version.py as a fresh module instance. |
| 21 | + Optionally simulates CI environment. |
| 22 | + """ |
| 23 | + module_name = "bump_version_test_instance" |
| 24 | + |
| 25 | + @contextmanager |
| 26 | + def ci_env(): |
| 27 | + if ci: |
| 28 | + os.environ["CI"] = "true" |
| 29 | + try: |
| 30 | + yield |
| 31 | + finally: |
| 32 | + if ci: |
| 33 | + os.environ.pop("CI", None) |
| 34 | + |
| 35 | + with ci_env(): |
| 36 | + spec = importlib.util.spec_from_file_location(module_name, SCRIPT_PATH) |
| 37 | + module = importlib.util.module_from_spec(spec) |
| 38 | + sys.modules.pop(module_name, None) |
| 39 | + spec.loader.exec_module(module) |
| 40 | + return module |
22 | 41 |
|
23 | 42 |
|
24 | 43 | class BaseBumpTest(unittest.TestCase): |
25 | 44 | """Shared environment setup for file-based tests.""" |
26 | 45 |
|
27 | 46 | def setUp(self): |
28 | | - """Create temporary directory and switch CWD""" |
29 | 47 | self.tmp_dir = tempfile.TemporaryDirectory() |
30 | 48 | self.test_path = Path(self.tmp_dir.name) |
31 | 49 | self.original_cwd = Path.cwd() |
32 | 50 | os.chdir(self.test_path) |
33 | 51 |
|
| 52 | + self.bump_version = load_bump_version() |
| 53 | + |
34 | 54 | def tearDown(self): |
35 | | - """Restore CWD and cleanup temp files""" |
36 | 55 | os.chdir(self.original_cwd) |
37 | 56 | self.tmp_dir.cleanup() |
38 | 57 |
|
39 | 58 | def create_mock_project(self, version="2.8.0"): |
40 | | - """Helper to scaffold standard test files.""" |
41 | 59 | (self.test_path / "ConsumableManager.toc").write_text( |
42 | | - f"## Version: {version}\n## Interface: 30300", encoding="utf-8" |
| 60 | + f"## Version: {version}\n## Interface: 30300", |
| 61 | + encoding="utf-8", |
43 | 62 | ) |
44 | 63 | (self.test_path / "ConsumableManager.lua").write_text( |
45 | | - f"-- Version: {version}\nlocal v = 'v{version}'", encoding="utf-8" |
| 64 | + f"-- Version: {version}\nlocal v = 'v{version}'", |
| 65 | + encoding="utf-8", |
46 | 66 | ) |
47 | 67 |
|
48 | 68 |
|
49 | 69 | class TestVersionValidation(unittest.TestCase): |
50 | 70 | """Test version format validation""" |
51 | 71 |
|
52 | 72 | def test_validation(self): |
53 | | - """Test that valid and invalid semantic versions are handled""" |
| 73 | + bump_version = load_bump_version() |
| 74 | + |
54 | 75 | cases = [ |
55 | 76 | ("1.0.0", True), |
56 | 77 | ("2.9.0", True), |
57 | 78 | ("1.0", False), |
58 | 79 | ("v1.0.0", False), |
59 | 80 | ("", False), |
60 | 81 | ] |
| 82 | + |
61 | 83 | for version, expected in cases: |
62 | 84 | with self.subTest(version=version): |
63 | | - self.assertEqual(bump_version.validate_version(version), expected) |
| 85 | + self.assertEqual( |
| 86 | + bump_version.validate_version(version), |
| 87 | + expected, |
| 88 | + ) |
64 | 89 |
|
65 | 90 |
|
66 | 91 | class TestBumpLogic(BaseBumpTest): |
67 | 92 | """Test the core version bumping functionality""" |
68 | 93 |
|
69 | 94 | def test_successful_bump(self): |
70 | | - """Test happy path: reading current and updating to new version.""" |
71 | 95 | self.create_mock_project("2.8.0") |
72 | | - self.assertEqual(bump_version.get_current_version(), "2.8.0") |
73 | | - self.assertEqual(bump_version.bump_version("2.9.0"), 0) |
74 | 96 |
|
75 | | - toc_content = (self.test_path / "ConsumableManager.toc").read_text() |
76 | | - lua_content = (self.test_path / "ConsumableManager.lua").read_text() |
| 97 | + self.assertEqual(self.bump_version.get_current_version(), "2.8.0") |
| 98 | + self.assertEqual(self.bump_version.bump_version("2.9.0"), 0) |
| 99 | + |
| 100 | + toc = (self.test_path / "ConsumableManager.toc").read_text() |
| 101 | + lua = (self.test_path / "ConsumableManager.lua").read_text() |
77 | 102 |
|
78 | | - self.assertIn("## Version: 2.9.0", toc_content) |
79 | | - self.assertIn("-- Version: 2.9.0", lua_content) |
80 | | - self.assertIn("'v2.9.0'", lua_content) |
| 103 | + self.assertIn("## Version: 2.9.0", toc) |
| 104 | + self.assertIn("-- Version: 2.9.0", lua) |
| 105 | + self.assertIn("'v2.9.0'", lua) |
81 | 106 |
|
82 | 107 | def test_missing_files(self): |
83 | | - """Verify error code 2 when files are missing.""" |
84 | | - self.assertEqual(bump_version.bump_version("2.9.0"), 2) |
| 108 | + self.assertEqual(self.bump_version.bump_version("2.9.0"), 2) |
85 | 109 |
|
86 | 110 |
|
87 | 111 | class TestCIEnvironment(unittest.TestCase): |
88 | 112 | """Test behavior in CI environment""" |
89 | 113 |
|
90 | 114 | def test_colors_disabled_in_ci(self): |
91 | | - """Verify reload() functionality with registered sys.modules.""" |
92 | | - import importlib |
| 115 | + bump_version = load_bump_version(ci=True) |
93 | 116 |
|
94 | | - os.environ["CI"] = "true" |
95 | | - # Reloading now works because __spec__ was assigned during import |
96 | | - importlib.reload(bump_version) |
97 | | - try: |
98 | | - self.assertEqual(bump_version.Colors.RED, "") |
99 | | - finally: |
100 | | - del os.environ["CI"] |
101 | | - importlib.reload(bump_version) |
| 117 | + self.assertEqual(bump_version.Colors.RED, "") |
| 118 | + self.assertEqual(bump_version.Colors.GREEN, "") |
| 119 | + self.assertEqual(bump_version.Colors.YELLOW, "") |
| 120 | + self.assertEqual(bump_version.Colors.NC, "") |
102 | 121 |
|
103 | 122 |
|
104 | 123 | if __name__ == "__main__": |
|
0 commit comments