Skip to content

Commit cf5a8cc

Browse files
committed
test: debug tests
1 parent 70fc6c1 commit cf5a8cc

File tree

2 files changed

+75
-35
lines changed

2 files changed

+75
-35
lines changed

scripts/bump-version.py

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#!/usr/bin/env python3
22
"""
33
bump-version.py - Automatically bump version in .toc and .lua files
4+
Usage: python bump-version.py <new_version>
5+
Example: python bump-version.py 2.9.0
6+
7+
Exit codes:
8+
0 - Success
9+
1 - Invalid arguments or version format
10+
2 - File operation error
411
"""
512

613
import os
@@ -9,6 +16,7 @@
916
from pathlib import Path
1017

1118

19+
# ANSI color codes (disabled in CI)
1220
class Colors:
1321
RED = "\033[0;31m"
1422
GREEN = "\033[0;32m"
@@ -30,18 +38,21 @@ def get_current_version() -> str:
3038
"""Get current version from .toc file"""
3139
toc_path = Path("ConsumableManager.toc")
3240
if not toc_path.exists():
33-
print(f"{Colors.RED}Error: {toc_path.name} not found{Colors.NC}")
41+
print(f"{Colors.RED}Error: ConsumableManager.toc not found{Colors.NC}")
42+
return "unknown"
43+
try:
44+
content = toc_path.read_text(encoding="utf-8")
45+
for line in content.splitlines():
46+
if line.startswith("## Version:"):
47+
return line.split(":", 1)[1].strip()
48+
except Exception as e:
49+
print(f"{Colors.RED}Error reading version: {e}{Colors.NC}")
3450
return "unknown"
35-
36-
content = toc_path.read_text(encoding="utf-8")
37-
for line in content.splitlines():
38-
if line.startswith("## Version:"):
39-
return line.split(":", 1)[1].strip()
4051
return "unknown"
4152

4253

4354
def update_file(filepath: str, old_content: str, new_content: str) -> bool:
44-
"""Update file content using pathlib and return success status"""
55+
"""Update file content and return success status"""
4556
path = Path(filepath)
4657
if old_content == new_content:
4758
print(f"{Colors.YELLOW}{Colors.NC} No changes needed: {path.name}")
@@ -56,50 +67,60 @@ def update_file(filepath: str, old_content: str, new_content: str) -> bool:
5667

5768

5869
def bump_version(new_version: str) -> int:
59-
"""Bump version in .toc and .lua files"""
70+
"""
71+
Bump version in .toc and .lua files
72+
Returns: 0 on success, 2 on error
73+
"""
74+
6075
print(f"{Colors.YELLOW}Bumping version to: {Colors.GREEN}{new_version}{Colors.NC}")
6176

6277
current_version = get_current_version()
6378
print(f"Current version: {Colors.YELLOW}{current_version}{Colors.NC}")
64-
print(f"New version: {Colors.GREEN}{new_version}{Colors.NC}\n")
79+
print(f"New version: {Colors.GREEN}{new_version}{Colors.NC}")
80+
print()
6581

82+
success = True
6683
toc_path = Path("ConsumableManager.toc")
6784
lua_path = Path("ConsumableManager.lua")
68-
success = True
6985

70-
# Update TOC
86+
# Update ConsumableManager.toc
7187
if toc_path.exists():
72-
content = toc_path.read_text(encoding="utf-8")
73-
new_content = re.sub(
88+
toc_content = toc_path.read_text(encoding="utf-8")
89+
new_toc = re.sub(
7490
r"^## Version:.*$",
7591
f"## Version: {new_version}",
76-
content,
92+
toc_content,
7793
flags=re.MULTILINE,
7894
)
79-
if update_file(str(toc_path), content, new_content):
95+
96+
if update_file(str(toc_path), toc_content, new_toc):
8097
print(f"{Colors.GREEN}{Colors.NC} Updated: {toc_path.name}")
8198
else:
8299
success = False
83100
else:
84101
print(f"{Colors.RED}{Colors.NC} File not found: {toc_path.name}")
85102
success = False
86103

87-
# Update LUA
104+
# Update ConsumableManager.lua
88105
if lua_path.exists():
89-
content = lua_path.read_text(encoding="utf-8")
90-
new_content = re.sub(
106+
lua_content = lua_path.read_text(encoding="utf-8")
107+
108+
# Update header comment
109+
new_lua = re.sub(
91110
r"^-- Version:.*$",
92111
f"-- Version: {new_version}",
93-
content,
112+
lua_content,
94113
flags=re.MULTILINE,
95114
)
96-
new_content = re.sub(
97-
r'local version = HexColor\("v[\d.]+',
98-
f'local version = HexColor("v{new_version}',
99-
new_content,
115+
116+
# Update status display version (Improved regex to catch quoted versions)
117+
new_lua = re.sub(
118+
r"v\d+\.\d+\.\d+",
119+
f"v{new_version}",
120+
new_lua,
100121
)
101122

102-
if update_file(str(lua_path), content, new_content):
123+
if update_file(str(lua_path), lua_content, new_lua):
103124
print(
104125
f"{Colors.GREEN}{Colors.NC} Updated: {lua_path.name} (header + status)"
105126
)
@@ -109,25 +130,32 @@ def bump_version(new_version: str) -> int:
109130
print(f"{Colors.RED}{Colors.NC} File not found: {lua_path.name}")
110131
success = False
111132

133+
# Summary
134+
print()
112135
if success:
113-
print(f"\n{Colors.GREEN}✓ Version bump complete!{Colors.NC}")
136+
print(f"{Colors.GREEN}✓ Version bump complete!{Colors.NC}")
114137
return 0
115-
116-
print(f"\n{Colors.RED}✗ Version bump failed!{Colors.NC}")
117-
return 2
138+
else:
139+
print(f"{Colors.RED}✗ Version bump failed!{Colors.NC}")
140+
return 2
118141

119142

120143
def main():
121144
if len(sys.argv) != 2:
122145
print(f"{Colors.RED}Error: No version specified{Colors.NC}")
146+
print(f"Usage: {sys.argv[0]} <new_version>")
147+
print(f"Example: {sys.argv[0]} 2.9.0")
123148
sys.exit(1)
124149

125150
new_version = sys.argv[1]
151+
126152
if not validate_version(new_version):
127153
print(f"{Colors.RED}Error: Invalid version format{Colors.NC}")
154+
print("Please use semantic versioning: X.Y.Z (e.g., 2.9.0)")
128155
sys.exit(1)
129156

130-
sys.exit(bump_version(new_version))
157+
exit_code = bump_version(new_version)
158+
sys.exit(exit_code)
131159

132160

133161
if __name__ == "__main__":

tests/test_bump_version.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env python3
2+
"""
3+
Unit tests for bump-version.py
4+
"""
5+
26
import importlib.util
7+
import os
38
import sys
49
import tempfile
510
import unittest
@@ -10,6 +15,8 @@
1015

1116
spec = importlib.util.spec_from_file_location("bump_version", str(SCRIPT_PATH))
1217
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
1320
sys.modules["bump_version"] = bump_version
1421
spec.loader.exec_module(bump_version)
1522

@@ -18,16 +25,14 @@ class BaseBumpTest(unittest.TestCase):
1825
"""Shared environment setup for file-based tests."""
1926

2027
def setUp(self):
28+
"""Create temporary directory and switch CWD"""
2129
self.tmp_dir = tempfile.TemporaryDirectory()
2230
self.test_path = Path(self.tmp_dir.name)
2331
self.original_cwd = Path.cwd()
24-
import os
25-
2632
os.chdir(self.test_path)
2733

2834
def tearDown(self):
29-
import os
30-
35+
"""Restore CWD and cleanup temp files"""
3136
os.chdir(self.original_cwd)
3237
self.tmp_dir.cleanup()
3338

@@ -42,10 +47,13 @@ def create_mock_project(self, version="2.8.0"):
4247

4348

4449
class TestVersionValidation(unittest.TestCase):
50+
"""Test version format validation"""
51+
4552
def test_validation(self):
53+
"""Test that valid and invalid semantic versions are handled"""
4654
cases = [
4755
("1.0.0", True),
48-
("0.0.1", True),
56+
("2.9.0", True),
4957
("1.0", False),
5058
("v1.0.0", False),
5159
("", False),
@@ -56,6 +64,8 @@ def test_validation(self):
5664

5765

5866
class TestBumpLogic(BaseBumpTest):
67+
"""Test the core version bumping functionality"""
68+
5969
def test_successful_bump(self):
6070
"""Test happy path: reading current and updating to new version."""
6171
self.create_mock_project("2.8.0")
@@ -75,12 +85,14 @@ def test_missing_files(self):
7585

7686

7787
class TestCIEnvironment(unittest.TestCase):
88+
"""Test behavior in CI environment"""
89+
7890
def test_colors_disabled_in_ci(self):
7991
"""Verify reload() functionality with registered sys.modules."""
8092
import importlib
81-
import os
8293

8394
os.environ["CI"] = "true"
95+
# Reloading now works because __spec__ was assigned during import
8496
importlib.reload(bump_version)
8597
try:
8698
self.assertEqual(bump_version.Colors.RED, "")

0 commit comments

Comments
 (0)