Skip to content

Commit 21bc57c

Browse files
committed
chore(release): raise error if no smaller version in changelog
Updates _find_insertion_point to raise a ValueError if it cannot find an existing version in CHANGELOG.md that is smaller than the new version being inserted. This replaces the previous behavior of appending to the end of the file, which should not occur in practice. Also adds a test case to verify this error behavior.
1 parent ba62655 commit 21bc57c

2 files changed

Lines changed: 43 additions & 10 deletions

File tree

tests/tools/private/release/changelog_news_test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,40 @@ def test_update_changelog_insertion_point(self):
485485
self.assertTrue(idx_2_2_0 < idx_2_1_0 < idx_2_0_0)
486486
self.assertIn("Fix in 2.1.0", new_content)
487487

488+
def test_update_changelog_insertion_point_too_small(self):
489+
# Arrange
490+
changelog = """# Changelog
491+
492+
{#unreleased}
493+
## Unreleased
494+
495+
[unreleased]: https://github.com/bazel-contrib/rules_python/releases/tag/unreleased
496+
497+
{#v2-0-0}
498+
## [2.0.0] - 2026-04-09
499+
500+
[2.0.0]: https://github.com/bazel-contrib/rules_python/releases/tag/2.0.0
501+
"""
502+
changelog_path = self.tmpdir / "CHANGELOG.md"
503+
changelog_path.write_text(changelog)
504+
505+
news_dir = self.tmpdir / "news"
506+
news_dir.mkdir()
507+
(news_dir / "123.fixed.md").write_text("Fix in 1.0.0")
508+
509+
# Act & Assert
510+
with self.assertRaises(ValueError) as ctx:
511+
changelog_news.update_changelog(
512+
"1.0.0",
513+
"2026-01-01",
514+
changelog_path=changelog_path,
515+
news_dir=news_dir,
516+
)
517+
self.assertIn(
518+
"Could not find a version in CHANGELOG.md smaller than 1.0.0",
519+
str(ctx.exception),
520+
)
521+
488522

489523
if __name__ == "__main__":
490524
unittest.main()

tools/private/release/changelog_news.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def _find_insertion_point(changelog_content, new_version):
123123
if parsed_ver < parsed_new_ver:
124124
return m.start()
125125

126-
return len(changelog_content)
126+
raise ValueError(
127+
f"Could not find a version in CHANGELOG.md smaller than {new_version} to insert before."
128+
)
127129

128130

129131
def _add_news_to_changelog(input_path, output_path, version, entries, release_date):
@@ -275,15 +277,12 @@ def _add_news_to_changelog(input_path, output_path, version, entries, release_da
275277
# Find insertion point
276278
insertion_point = _find_insertion_point(changelog_content, version)
277279

278-
if insertion_point == len(changelog_content):
279-
new_content = changelog_content.rstrip() + "\n\n" + new_release_block + "\n"
280-
else:
281-
new_content = (
282-
changelog_content[:insertion_point]
283-
+ new_release_block
284-
+ "\n\n"
285-
+ changelog_content[insertion_point:]
286-
)
280+
new_content = (
281+
changelog_content[:insertion_point]
282+
+ new_release_block
283+
+ "\n\n"
284+
+ changelog_content[insertion_point:]
285+
)
287286
output_path.write_text(new_content, encoding="utf-8")
288287

289288

0 commit comments

Comments
 (0)