From 85da9d4ebe8d313dc4b61753636009dc14dcb241 Mon Sep 17 00:00:00 2001 From: fei <204683769+feiiiiii5@users.noreply.github.com> Date: Sun, 29 Nov 2026 08:38:06 +0800 Subject: [PATCH] fix: complete file-change journal on mutation errors Assisted-by: Claude Code (model: Sonnet 5, autonomous) Co-Authored-By: Claude Sonnet 5 --- src/specify_cli/integrations/_file_changes.py | 6 ++++-- .../integrations/test_file_changes.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/specify_cli/integrations/_file_changes.py b/src/specify_cli/integrations/_file_changes.py index a9b0888b89..f850e077f2 100644 --- a/src/specify_cli/integrations/_file_changes.py +++ b/src/specify_cli/integrations/_file_changes.py @@ -69,8 +69,10 @@ def after_file_change(path: Path) -> None: def changing_file(path: Path, *, removal: bool = False): """Journal a host mutation without claiming the file for uninstall.""" before_file_change(path, removal=removal) - yield - after_file_change(path) + try: + yield + finally: + after_file_change(path) def write_text(path: Path, content: str, *, encoding: str = "utf-8", errors: str | None = None) -> int: diff --git a/tests/specify_cli/integrations/test_file_changes.py b/tests/specify_cli/integrations/test_file_changes.py index 00cd91dbbb..77bbdbed07 100644 --- a/tests/specify_cli/integrations/test_file_changes.py +++ b/tests/specify_cli/integrations/test_file_changes.py @@ -191,3 +191,19 @@ def after_change(path): assert not (tmp_path / "retained" / "SKILL.md").exists() elif swap_at in ("after_unlink", "before_rmdir_self") or not descriptor_io: assert directory.is_dir() + + +def test_changing_file_reports_completion_when_mutation_raises(tmp_path): + path = tmp_path / "file.txt" + observed = [] + token = _file_changes.file_change_observer.set(lambda *args: observed.append(args)) + try: + with pytest.raises(RuntimeError, match="mutation failed"), _file_changes.changing_file( + path + ): + path.write_text("partial") + raise RuntimeError("mutation failed") + finally: + _file_changes.file_change_observer.reset(token) + + assert observed == [(path, True, False), (path, False, False)]