Unit-test the patch cut's changelog edits in-process

This commit is contained in:
Lilleman
2026-11-24 14:29:51 +01:00
parent 459a3d5f54
commit 39cb2f4d9e
2 changed files with 32 additions and 4 deletions
+10 -4
View File
@@ -112,10 +112,7 @@ def tag_patch(clone: str, base: str, commits: list[str], entries: list[str], nam
if isinstance(applied := apply_commit(clone, worktree, commit, base), Failure):
return applied
changelog = worktree / "CHANGELOG.md"
text = changelog.read_text() if changelog.exists() else "# Changelog\n"
section_text = f"## {named}\n\n" + "\n".join(entries) + "\n\n"
headed, count = re.subn(r"^## ", lambda _: section_text + "## ", text, count=1, flags=re.M)
changelog.write_text(headed if count else text.rstrip("\n") + "\n\n" + section_text.rstrip("\n") + "\n")
changelog.write_text(headed(changelog.read_text() if changelog.exists() else "# Changelog\n", entries, named))
if isinstance(committed := git("commit", "--quiet", "--all", "--message", title, cwd=str(worktree)), Failure):
return committed
said(f"applied {len(commits)} commit(s) on {base}; testing them with ci.sh")
@@ -135,6 +132,15 @@ def tag_patch(clone: str, base: str, commits: list[str], entries: list[str], nam
shutil.rmtree(worktree.parent, ignore_errors=True)
def headed(changelog: str, entries: list[str], named: str) -> str:
"""changelog with a ## named section holding entries before its first ## section, or at its end where it has none."""
added = f"## {named}\n\n" + "\n".join(entries) + "\n"
first = re.search(r"^## ", changelog, flags=re.M)
if first is None:
return changelog.rstrip("\n") + "\n\n" + added
return changelog[:first.start()] + added + "\n" + changelog[first.start():]
def apply_commit(clone: str, worktree: pathlib.Path, commit: str, base: str) -> Failure | None:
"""Apply commit's change, but for LEFT_AS_TAGGED, to worktree's index and files, merging three ways where the tag base's files differ from commit's parent."""
diff = git("diff", "--binary", f"{commit}^", commit, "--", ".", *(f":(exclude){path}" for path in LEFT_AS_TAGGED), cwd=clone)
+22
View File
@@ -10,6 +10,7 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from failure import Failure
from test_env import isolated_env
from deploy_release import patch
TOOL = pathlib.Path(__file__).resolve().parents[1] / "deploy-release"
@@ -611,5 +612,26 @@ class Cut(unittest.TestCase):
class PatchChangelog(unittest.TestCase):
"""The changelog edits of a patch cut, read in-process, so a mutation tester reaches them."""
def test_heads_the_tags_changelog_with_the_patchs_section(self):
self.assertEqual(patch.headed("# Changelog\n\n## 0.1.0\n\n- First.\n", ["- Fixed.", "- Also."], "0.1.1"), "# Changelog\n\n## 0.1.1\n\n- Fixed.\n- Also.\n\n## 0.1.0\n\n- First.\n")
def test_ends_a_changelog_with_no_section_with_the_patchs_section(self):
self.assertEqual(patch.headed("# Changelog\n\n", ["- Fixed."], "0.1.1"), "# Changelog\n\n## 0.1.1\n\n- Fixed.\n")
def test_moves_the_patchs_entries_from_unreleased_to_a_section_after_it(self):
changelog = "# Changelog\n\n## Unreleased\n\n- Second.\n- Fixed.\n\n## 0.1.0\n\n- First.\n"
self.assertEqual(patch.moved_entries(changelog, ["- Fixed."], "0.1.1"), "# Changelog\n\n## Unreleased\n\n- Second.\n\n## 0.1.1\n\n- Fixed.\n\n## 0.1.0\n\n- First.\n")
def test_leaves_an_older_section_entry_that_reads_the_same(self):
changelog = "# Changelog\n\n## Unreleased\n\n- Fixed.\n\n## 0.1.0\n\n- Fixed.\n"
self.assertEqual(patch.moved_entries(changelog, ["- Fixed."], "0.1.1"), "# Changelog\n\n## Unreleased\n\n## 0.1.1\n\n- Fixed.\n\n## 0.1.0\n\n- Fixed.\n")
def test_puts_the_section_last_where_unreleased_is_the_last_section(self):
self.assertEqual(patch.moved_entries("# Changelog\n\n## Unreleased\n\n- Fixed.\n", ["- Fixed."], "0.1.1"), "# Changelog\n\n## Unreleased\n\n## 0.1.1\n\n- Fixed.\n")
if __name__ == "__main__":
unittest.main()