Skip to content

Commit dd98644

Browse files
committed
test(fix): debug tests
1 parent cf5a8cc commit dd98644

2 files changed

Lines changed: 62 additions & 37 deletions

File tree

.github/workflows/tests.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,28 @@ jobs:
3131
python-tests:
3232
name: Python Tests
3333
runs-on: ubuntu-latest
34+
env:
35+
CI: true
36+
37+
strategy:
38+
matrix:
39+
python-version: ["3.10", "3.11", "3.12"]
3440

3541
steps:
3642
- uses: actions/checkout@v4
3743

3844
- name: Set up Python
3945
uses: actions/setup-python@v5
4046
with:
41-
python-version: '3.x'
47+
python-version: ${{ matrix.python-version }}
4248

4349
- name: Run bump-version tests
4450
run: python -m unittest tests/test_bump_version.py -v
4551

4652
- name: Test bump-version script syntax
4753
run: python -m py_compile scripts/bump-version.py
4854

49-
- name: Validate version format
55+
- name: Smoke test bump-version script
5056
run: |
51-
# Test that the script runs with --help would work (basic smoke test)
52-
python scripts/bump-version.py 1.0.0 || echo "Expected to fail without files, that's ok for syntax check"
57+
python scripts/bump-version.py 1.0.0 || \
58+
echo "Expected failure due to missing files"

tests/test_bump_version.py

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,97 +8,116 @@
88
import sys
99
import tempfile
1010
import unittest
11+
from contextlib import contextmanager
1112
from pathlib import Path
1213

1314
BASE_DIR = Path(__file__).parent.parent
1415
SCRIPT_PATH = BASE_DIR / "scripts" / "bump-version.py"
1516

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
2241

2342

2443
class BaseBumpTest(unittest.TestCase):
2544
"""Shared environment setup for file-based tests."""
2645

2746
def setUp(self):
28-
"""Create temporary directory and switch CWD"""
2947
self.tmp_dir = tempfile.TemporaryDirectory()
3048
self.test_path = Path(self.tmp_dir.name)
3149
self.original_cwd = Path.cwd()
3250
os.chdir(self.test_path)
3351

52+
self.bump_version = load_bump_version()
53+
3454
def tearDown(self):
35-
"""Restore CWD and cleanup temp files"""
3655
os.chdir(self.original_cwd)
3756
self.tmp_dir.cleanup()
3857

3958
def create_mock_project(self, version="2.8.0"):
40-
"""Helper to scaffold standard test files."""
4159
(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",
4362
)
4463
(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",
4666
)
4767

4868

4969
class TestVersionValidation(unittest.TestCase):
5070
"""Test version format validation"""
5171

5272
def test_validation(self):
53-
"""Test that valid and invalid semantic versions are handled"""
73+
bump_version = load_bump_version()
74+
5475
cases = [
5576
("1.0.0", True),
5677
("2.9.0", True),
5778
("1.0", False),
5879
("v1.0.0", False),
5980
("", False),
6081
]
82+
6183
for version, expected in cases:
6284
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+
)
6489

6590

6691
class TestBumpLogic(BaseBumpTest):
6792
"""Test the core version bumping functionality"""
6893

6994
def test_successful_bump(self):
70-
"""Test happy path: reading current and updating to new version."""
7195
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)
7496

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()
77102

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)
81106

82107
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)
85109

86110

87111
class TestCIEnvironment(unittest.TestCase):
88112
"""Test behavior in CI environment"""
89113

90114
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)
93116

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, "")
102121

103122

104123
if __name__ == "__main__":

0 commit comments

Comments
 (0)