1+ #!/usr/bin/env python3
2+ """
3+ Simple test for the changelog update script.
4+ """
5+
6+ import os
7+ import tempfile
8+ import sys
9+ from pathlib import Path
10+
11+ # Add the scripts directory to the path
12+ sys .path .insert (0 , os .path .join (os .path .dirname (__file__ ), '..' , 'scripts' ))
13+
14+ from update_changelog import update_changelog , find_unreleased_section
15+
16+
17+ def test_find_unreleased_section ():
18+ """Test finding the unreleased section."""
19+ lines = [
20+ "# Changelog\n " ,
21+ "\n " ,
22+ "## UNRELEASED\n " ,
23+ "\n " ,
24+ "- Some existing entry\n " ,
25+ "\n " ,
26+ "## 1.0.0\n " ,
27+ "- Released version\n "
28+ ]
29+
30+ unreleased_start , content_start = find_unreleased_section (lines )
31+ assert unreleased_start == 2 , f"Expected unreleased_start=2, got { unreleased_start } "
32+ assert content_start == 4 , f"Expected content_start=4, got { content_start } "
33+ print ("✓ find_unreleased_section test passed" )
34+
35+
36+ def test_update_changelog ():
37+ """Test updating the changelog."""
38+ # Create a temporary changelog file
39+ changelog_content = """# Changelog
40+
41+ ## UNRELEASED
42+
43+ - Existing entry
44+
45+ ## 1.0.0
46+
47+ - Released version
48+ """
49+
50+ with tempfile .NamedTemporaryFile (mode = 'w' , suffix = '.md' , delete = False ) as f :
51+ f .write (changelog_content )
52+ temp_path = f .name
53+
54+ try :
55+ # Update the changelog
56+ result = update_changelog (temp_path , "999" , "Test PR" , "https://example.com/pr/999" )
57+ assert result == True , "update_changelog should return True on success"
58+
59+ # Read the updated content
60+ with open (temp_path , 'r' ) as f :
61+ updated_content = f .read ()
62+
63+ # Check that the entry was added
64+ assert "- Test PR [#999](https://example.com/pr/999)" in updated_content
65+
66+ # Check that it was added in the right place (after UNRELEASED)
67+ lines = updated_content .split ('\n ' )
68+ unreleased_idx = lines .index ("## UNRELEASED" )
69+ entry_idx = None
70+ for i , line in enumerate (lines ):
71+ if "Test PR [#999]" in line :
72+ entry_idx = i
73+ break
74+
75+ assert entry_idx is not None , "Entry should be found"
76+ assert entry_idx > unreleased_idx , "Entry should be after UNRELEASED section"
77+
78+ # Test duplicate detection
79+ result2 = update_changelog (temp_path , "999" , "Test PR" , "https://example.com/pr/999" )
80+ assert result2 == False , "update_changelog should return False for duplicates"
81+
82+ print ("✓ update_changelog test passed" )
83+
84+ finally :
85+ # Clean up
86+ os .unlink (temp_path )
87+
88+
89+ def main ():
90+ """Run all tests."""
91+ print ("Running changelog automation tests..." )
92+
93+ test_find_unreleased_section ()
94+ test_update_changelog ()
95+
96+ print ("✓ All tests passed!" )
97+
98+
99+ if __name__ == '__main__' :
100+ main ()
0 commit comments