Skip to content

Commit 996332b

Browse files
Combine version bump tests using pytest.param and add changelog entry update functionality
Co-authored-by: saulshanabrook <[email protected]>
1 parent 30a865b commit 996332b

File tree

2 files changed

+40
-107
lines changed

2 files changed

+40
-107
lines changed

modify_changelog.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def update_changelog_version(file_path: Path, new_version: str) -> None:
8686

8787

8888
def update_changelog_pr(file_path: Path, pr_number: str, pr_title: str, pr_url: str) -> bool:
89-
"""Update the changelog with the new PR entry. Returns True if successful, False if entry already exists."""
89+
"""Update the changelog with the new PR entry. If entry exists, update it; otherwise add new entry."""
9090

9191
# Read the current changelog
9292
with open(file_path, 'r', encoding='utf-8') as f:
@@ -106,23 +106,29 @@ def update_changelog_pr(file_path: Path, pr_number: str, pr_title: str, pr_url:
106106
# Create the new entry
107107
new_entry = f"- {pr_title} [#{pr_number}]({pr_url})\n"
108108

109-
# Check if this PR entry already exists to avoid duplicates
110-
for line in lines[content_start:]:
109+
# Check if this PR entry already exists and update it if so
110+
existing_entry_index = None
111+
for i, line in enumerate(lines[content_start:], start=content_start):
111112
if f"[#{pr_number}]" in line:
112-
print(f"Changelog entry for PR #{pr_number} already exists")
113-
return False
113+
existing_entry_index = i
114+
break
114115
# Stop checking when we reach the next section
115116
if line.startswith("## ") and not line.strip() == "## UNRELEASED":
116117
break
117118

118-
# Insert the new entry at the beginning of the unreleased content
119-
lines.insert(content_start, new_entry)
119+
if existing_entry_index is not None:
120+
# Update existing entry
121+
lines[existing_entry_index] = new_entry
122+
print(f"Updated changelog entry for PR #{pr_number}: {pr_title}")
123+
else:
124+
# Insert the new entry at the beginning of the unreleased content
125+
lines.insert(content_start, new_entry)
126+
print(f"Added changelog entry for PR #{pr_number}: {pr_title}")
120127

121128
# Write the updated changelog
122129
with open(file_path, 'w', encoding='utf-8') as f:
123130
f.writelines(lines)
124131

125-
print(f"Added changelog entry for PR #{pr_number}: {pr_title}")
126132
return True
127133

128134

python/tests/test_modify_changelog.py

Lines changed: 26 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,31 @@ def test_update_changelog_subcommand_help():
3737
assert "Pull request title" in result.stdout
3838

3939

40-
def test_bump_version_patch():
41-
"""Test version bumping with patch increment."""
40+
@pytest.mark.parametrize("start_version,bump_type,expected_version", [
41+
pytest.param("1.2.3", "patch", "1.2.4", id="patch_bump"),
42+
pytest.param("1.2.3", "minor", "1.3.0", id="minor_bump"),
43+
pytest.param("1.2.3", "major", "2.0.0", id="major_bump"),
44+
])
45+
def test_bump_version(start_version, bump_type, expected_version):
46+
"""Test version bumping with different increment types."""
4247
with tempfile.TemporaryDirectory() as temp_dir:
4348
temp_path = Path(temp_dir)
4449

4550
# Create mock Cargo.toml
46-
cargo_content = '''[package]
51+
cargo_content = f'''[package]
4752
name = "test-package"
48-
version = "1.2.3"
53+
version = "{start_version}"
4954
edition = "2021"
5055
'''
5156
cargo_path = temp_path / "Cargo.toml"
5257
cargo_path.write_text(cargo_content)
5358

5459
# Create mock changelog
55-
changelog_content = '''# Changelog
60+
changelog_content = f'''# Changelog
5661
5762
## UNRELEASED
5863
59-
## 1.2.3 (2024-01-01)
64+
## {start_version} (2024-01-01)
6065
6166
- Some old change
6267
'''
@@ -68,104 +73,20 @@ def test_bump_version_patch():
6873
# Run the script
6974
result = subprocess.run([sys.executable,
7075
str(Path(__file__).parent.parent.parent / "modify_changelog.py"),
71-
"bump_version", "patch"],
76+
"bump_version", bump_type],
7277
capture_output=True, text=True, cwd=temp_path)
7378

7479
assert result.returncode == 0
75-
assert result.stdout.strip() == "1.2.4"
80+
assert result.stdout.strip() == expected_version
7681

7782
# Check Cargo.toml was updated
7883
updated_cargo = cargo_path.read_text()
79-
assert 'version = "1.2.4"' in updated_cargo
84+
assert f'version = "{expected_version}"' in updated_cargo
8085

8186
# Check changelog was updated
8287
updated_changelog = changelog_path.read_text()
8388
assert "## UNRELEASED" in updated_changelog
84-
assert "## 1.2.4 (" in updated_changelog
85-
86-
87-
def test_bump_version_minor():
88-
"""Test version bumping with minor increment."""
89-
with tempfile.TemporaryDirectory() as temp_dir:
90-
temp_path = Path(temp_dir)
91-
92-
# Create mock Cargo.toml
93-
cargo_content = '''[package]
94-
name = "test-package"
95-
version = "1.2.3"
96-
edition = "2021"
97-
'''
98-
cargo_path = temp_path / "Cargo.toml"
99-
cargo_path.write_text(cargo_content)
100-
101-
# Create mock changelog
102-
changelog_content = '''# Changelog
103-
104-
## UNRELEASED
105-
106-
## 1.2.3 (2024-01-01)
107-
108-
- Some old change
109-
'''
110-
docs_dir = temp_path / "docs"
111-
docs_dir.mkdir()
112-
changelog_path = docs_dir / "changelog.md"
113-
changelog_path.write_text(changelog_content)
114-
115-
# Run the script
116-
result = subprocess.run([sys.executable,
117-
str(Path(__file__).parent.parent.parent / "modify_changelog.py"),
118-
"bump_version", "minor"],
119-
capture_output=True, text=True, cwd=temp_path)
120-
121-
assert result.returncode == 0
122-
assert result.stdout.strip() == "1.3.0"
123-
124-
# Check Cargo.toml was updated
125-
updated_cargo = cargo_path.read_text()
126-
assert 'version = "1.3.0"' in updated_cargo
127-
128-
129-
def test_bump_version_major():
130-
"""Test version bumping with major increment."""
131-
with tempfile.TemporaryDirectory() as temp_dir:
132-
temp_path = Path(temp_dir)
133-
134-
# Create mock Cargo.toml
135-
cargo_content = '''[package]
136-
name = "test-package"
137-
version = "1.2.3"
138-
edition = "2021"
139-
'''
140-
cargo_path = temp_path / "Cargo.toml"
141-
cargo_path.write_text(cargo_content)
142-
143-
# Create mock changelog
144-
changelog_content = '''# Changelog
145-
146-
## UNRELEASED
147-
148-
## 1.2.3 (2024-01-01)
149-
150-
- Some old change
151-
'''
152-
docs_dir = temp_path / "docs"
153-
docs_dir.mkdir()
154-
changelog_path = docs_dir / "changelog.md"
155-
changelog_path.write_text(changelog_content)
156-
157-
# Run the script
158-
result = subprocess.run([sys.executable,
159-
str(Path(__file__).parent.parent.parent / "modify_changelog.py"),
160-
"bump_version", "major"],
161-
capture_output=True, text=True, cwd=temp_path)
162-
163-
assert result.returncode == 0
164-
assert result.stdout.strip() == "2.0.0"
165-
166-
# Check Cargo.toml was updated
167-
updated_cargo = cargo_path.read_text()
168-
assert 'version = "2.0.0"' in updated_cargo
89+
assert f"## {expected_version} (" in updated_changelog
16990

17091

17192
def test_update_changelog_new_entry():
@@ -202,7 +123,7 @@ def test_update_changelog_new_entry():
202123

203124

204125
def test_update_changelog_duplicate_entry():
205-
"""Test that duplicate PR entries are not added."""
126+
"""Test that modifying PR title updates the existing changelog entry instead of making a new one."""
206127
with tempfile.TemporaryDirectory() as temp_dir:
207128
temp_path = Path(temp_dir)
208129

@@ -222,14 +143,20 @@ def test_update_changelog_duplicate_entry():
222143
changelog_path = docs_dir / "changelog.md"
223144
changelog_path.write_text(changelog_content)
224145

225-
# Run the script
146+
# Run the script with updated title for same PR
226147
result = subprocess.run([sys.executable,
227148
str(Path(__file__).parent.parent.parent / "modify_changelog.py"),
228-
"update_changelog", "123", "Fix important bug"],
149+
"update_changelog", "123", "Fix critical security bug"],
229150
capture_output=True, text=True, cwd=temp_path)
230151

231-
assert result.returncode == 1
232-
assert "Changelog entry for PR #123 already exists" in result.stdout
152+
assert result.returncode == 0
153+
assert "Updated changelog entry for PR #123: Fix critical security bug" in result.stdout
154+
155+
# Check that the changelog was updated, not duplicated
156+
updated_changelog = changelog_path.read_text()
157+
assert "- Fix critical security bug [#123](https://github.com/egraphs-good/egglog-python/pull/123)" in updated_changelog
158+
assert "- Fix important bug [#123]" not in updated_changelog # Old entry should be gone
159+
assert updated_changelog.count("[#123]") == 1 # Should only have one entry for PR 123
233160

234161

235162
def test_update_changelog_missing_file():

0 commit comments

Comments
 (0)