Skip to content

Commit de89d83

Browse files
committed
tests: add unit tests for version-bump script
1 parent dc6b18f commit de89d83

File tree

2 files changed

+306
-3
lines changed

2 files changed

+306
-3
lines changed

.github/workflows/tests.yml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
name: Unit Tests
1+
name: Tests
22

33
on:
44
push:
55
pull_request:
66

77
jobs:
8-
test:
8+
lua-tests:
9+
name: Lua Tests
910
runs-on: ubuntu-latest
1011

1112
steps:
@@ -24,5 +25,28 @@ jobs:
2425
- name: Run Luacheck
2526
run: luacheck ConsumableManager.lua Data.lua tests
2627

27-
- name: Run Tests
28+
- name: Run Lua Tests
2829
run: busted -o gtest tests/test_consumable_manager.lua
30+
31+
python-tests:
32+
name: Python Tests
33+
runs-on: ubuntu-latest
34+
35+
steps:
36+
- uses: actions/checkout@v4
37+
38+
- name: Set up Python
39+
uses: actions/setup-python@v5
40+
with:
41+
python-version: '3.x'
42+
43+
- name: Run bump-version tests
44+
run: python -m unittest tests/test_bump_version.py -v
45+
46+
- name: Test bump-version script syntax
47+
run: python -m py_compile scripts/bump-version.py
48+
49+
- name: Validate version format
50+
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"

tests/test_bump_version.py

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Unit tests for bump-version.py
4+
"""
5+
6+
import os
7+
import shutil
8+
import sys
9+
import tempfile
10+
import unittest
11+
12+
# Add scripts directory to path
13+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "scripts"))
14+
15+
# Import the module (we need to refactor bump-version.py slightly to be testable)
16+
import importlib.util
17+
18+
spec = importlib.util.spec_from_file_location("bump_version", "scripts/bump-version.py")
19+
bump_version = importlib.util.module_from_spec(spec)
20+
spec.loader.exec_module(bump_version)
21+
22+
23+
class TestVersionValidation(unittest.TestCase):
24+
"""Test version format validation"""
25+
26+
def test_valid_versions(self):
27+
"""Test that valid semantic versions are accepted"""
28+
valid_versions = ["1.0.0", "2.9.0", "10.20.30", "0.0.1", "99.99.99"]
29+
for version in valid_versions:
30+
with self.subTest(version=version):
31+
self.assertTrue(bump_version.validate_version(version))
32+
33+
def test_invalid_versions(self):
34+
"""Test that invalid versions are rejected"""
35+
invalid_versions = [
36+
"1.0", # Missing patch
37+
"1", # Only major
38+
"v1.0.0", # With v prefix
39+
"1.0.0-beta", # With suffix
40+
"1.0.0.0", # Too many parts
41+
"a.b.c", # Non-numeric
42+
"1.0.x", # Contains non-digit
43+
"", # Empty
44+
"1.0.", # Trailing dot
45+
".1.0.0", # Leading dot
46+
]
47+
for version in invalid_versions:
48+
with self.subTest(version=version):
49+
self.assertFalse(bump_version.validate_version(version))
50+
51+
52+
class TestFileUpdates(unittest.TestCase):
53+
"""Test file update operations"""
54+
55+
def setUp(self):
56+
"""Create temporary directory with test files"""
57+
self.test_dir = tempfile.mkdtemp()
58+
self.original_cwd = os.getcwd()
59+
os.chdir(self.test_dir)
60+
61+
# Create test .toc file
62+
self.toc_content = """## Interface: 30300
63+
## Title: Consumable Manager
64+
## Version: 2.8.0
65+
## Author: Test Author
66+
## SavedVariables: ConsumableManagerDB
67+
68+
ConsumableManager.lua
69+
Data.lua
70+
"""
71+
with open("ConsumableManager.toc", "w") as f:
72+
f.write(self.toc_content)
73+
74+
# Create test .lua file
75+
self.lua_content = """--------------------------------------------------------------------------------
76+
-- ConsumableManager
77+
-- Version: 2.8.0
78+
-- Purpose: Test addon
79+
--------------------------------------------------------------------------------
80+
81+
local function DisplayAddonStatus()
82+
local version = HexColor("v2.8.0", "888888")
83+
print(version)
84+
end
85+
"""
86+
with open("ConsumableManager.lua", "w") as f:
87+
f.write(self.lua_content)
88+
89+
def tearDown(self):
90+
"""Clean up temporary directory"""
91+
os.chdir(self.original_cwd)
92+
shutil.rmtree(self.test_dir)
93+
94+
def test_get_current_version(self):
95+
"""Test reading current version from .toc file"""
96+
version = bump_version.get_current_version()
97+
self.assertEqual(version, "2.8.0")
98+
99+
def test_get_current_version_missing_file(self):
100+
"""Test handling of missing .toc file"""
101+
os.remove("ConsumableManager.toc")
102+
version = bump_version.get_current_version()
103+
self.assertEqual(version, "unknown")
104+
105+
def test_update_toc_file(self):
106+
"""Test updating version in .toc file"""
107+
result = bump_version.bump_version("2.9.0")
108+
self.assertEqual(result, 0) # Success
109+
110+
with open("ConsumableManager.toc", "r") as f:
111+
content = f.read()
112+
113+
self.assertIn("## Version: 2.9.0", content)
114+
self.assertNotIn("## Version: 2.8.0", content)
115+
116+
def test_update_lua_file_header(self):
117+
"""Test updating version in .lua file header"""
118+
result = bump_version.bump_version("2.9.0")
119+
self.assertEqual(result, 0)
120+
121+
with open("ConsumableManager.lua", "r") as f:
122+
content = f.read()
123+
124+
self.assertIn("-- Version: 2.9.0", content)
125+
self.assertNotIn("-- Version: 2.8.0", content)
126+
127+
def test_update_lua_file_status_display(self):
128+
"""Test updating version in status display"""
129+
result = bump_version.bump_version("2.9.0")
130+
self.assertEqual(result, 0)
131+
132+
with open("ConsumableManager.lua", "r") as f:
133+
content = f.read()
134+
135+
self.assertIn('local version = HexColor("v2.9.0"', content)
136+
self.assertNotIn('local version = HexColor("v2.8.0"', content)
137+
138+
def test_update_multiple_versions(self):
139+
"""Test updating version multiple times"""
140+
# First update
141+
result = bump_version.bump_version("2.9.0")
142+
self.assertEqual(result, 0)
143+
144+
# Second update
145+
result = bump_version.bump_version("3.0.0")
146+
self.assertEqual(result, 0)
147+
148+
with open("ConsumableManager.toc", "r") as f:
149+
toc_content = f.read()
150+
with open("ConsumableManager.lua", "r") as f:
151+
lua_content = f.read()
152+
153+
self.assertIn("## Version: 3.0.0", toc_content)
154+
self.assertIn("-- Version: 3.0.0", lua_content)
155+
self.assertIn('local version = HexColor("v3.0.0"', lua_content)
156+
157+
def test_missing_toc_file(self):
158+
"""Test handling when .toc file is missing"""
159+
os.remove("ConsumableManager.toc")
160+
result = bump_version.bump_version("2.9.0")
161+
self.assertEqual(result, 2) # Error code
162+
163+
def test_missing_lua_file(self):
164+
"""Test handling when .lua file is missing"""
165+
os.remove("ConsumableManager.lua")
166+
result = bump_version.bump_version("2.9.0")
167+
self.assertEqual(result, 2) # Error code
168+
169+
def test_update_preserves_other_content(self):
170+
"""Test that updating version doesn't modify other content"""
171+
result = bump_version.bump_version("2.9.0")
172+
self.assertEqual(result, 0)
173+
174+
with open("ConsumableManager.toc", "r") as f:
175+
content = f.read()
176+
177+
# Check that other fields are preserved
178+
self.assertIn("## Interface: 30300", content)
179+
self.assertIn("## Title: Consumable Manager", content)
180+
self.assertIn("## Author: Test Author", content)
181+
self.assertIn("ConsumableManager.lua", content)
182+
183+
184+
class TestUpdateFile(unittest.TestCase):
185+
"""Test the update_file helper function"""
186+
187+
def test_update_file_no_changes(self):
188+
"""Test update_file when content is identical"""
189+
content = "test content"
190+
result = bump_version.update_file("test.txt", content, content)
191+
self.assertTrue(result)
192+
193+
def test_update_file_with_changes(self):
194+
"""Test update_file when content changes"""
195+
with tempfile.NamedTemporaryFile(mode="w", delete=False) as f:
196+
f.write("old content")
197+
temp_file = f.name
198+
199+
try:
200+
result = bump_version.update_file(temp_file, "old content", "new content")
201+
self.assertTrue(result)
202+
203+
with open(temp_file, "r") as f:
204+
content = f.read()
205+
self.assertEqual(content, "new content")
206+
finally:
207+
os.unlink(temp_file)
208+
209+
210+
class TestCIEnvironment(unittest.TestCase):
211+
"""Test behavior in CI environment"""
212+
213+
def test_colors_disabled_in_ci(self):
214+
"""Test that colors are disabled when CI env var is set"""
215+
# Set CI environment variable
216+
os.environ["CI"] = "true"
217+
218+
# Reload module to pick up environment variable
219+
importlib.reload(bump_version)
220+
221+
# Colors should be empty strings
222+
self.assertEqual(bump_version.Colors.RED, "")
223+
self.assertEqual(bump_version.Colors.GREEN, "")
224+
self.assertEqual(bump_version.Colors.YELLOW, "")
225+
self.assertEqual(bump_version.Colors.NC, "")
226+
227+
# Clean up
228+
del os.environ["CI"]
229+
importlib.reload(bump_version)
230+
231+
232+
class TestEdgeCases(unittest.TestCase):
233+
"""Test edge cases and error conditions"""
234+
235+
def setUp(self):
236+
"""Create temporary directory"""
237+
self.test_dir = tempfile.mkdtemp()
238+
self.original_cwd = os.getcwd()
239+
os.chdir(self.test_dir)
240+
241+
def tearDown(self):
242+
"""Clean up"""
243+
os.chdir(self.original_cwd)
244+
shutil.rmtree(self.test_dir)
245+
246+
def test_empty_toc_file(self):
247+
"""Test with empty .toc file"""
248+
with open("ConsumableManager.toc", "w") as f:
249+
f.write("")
250+
with open("ConsumableManager.lua", "w") as f:
251+
f.write("-- Version: 1.0.0")
252+
253+
version = bump_version.get_current_version()
254+
self.assertEqual(version, "unknown")
255+
256+
def test_malformed_toc_file(self):
257+
"""Test with malformed .toc file"""
258+
with open("ConsumableManager.toc", "w") as f:
259+
f.write("garbage content\nno version here\n")
260+
with open("ConsumableManager.lua", "w") as f:
261+
f.write("-- Version: 1.0.0")
262+
263+
version = bump_version.get_current_version()
264+
self.assertEqual(version, "unknown")
265+
266+
def test_version_with_spaces(self):
267+
"""Test .toc with spaces around version"""
268+
with open("ConsumableManager.toc", "w") as f:
269+
f.write("## Version: 2.8.0 \n")
270+
with open("ConsumableManager.lua", "w") as f:
271+
f.write("-- Version: 2.8.0")
272+
273+
version = bump_version.get_current_version()
274+
self.assertEqual(version, "2.8.0")
275+
276+
277+
if __name__ == "__main__":
278+
# Run tests with verbose output
279+
unittest.main(verbosity=2)

0 commit comments

Comments
 (0)