From f21f9543a5e2c6ff04129f5504b3c86c3dd5c844 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Sat, 17 Oct 2026 11:06:50 -0700 Subject: [PATCH 1/2] fix: preserve indexed files during smoke output cleanup --- heart/smoke.py | 32 +++++++++++++++++++++++++++----- tests/test_smoke.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/heart/smoke.py b/heart/smoke.py index c0ce73e6..11558720 100644 --- a/heart/smoke.py +++ b/heart/smoke.py @@ -557,11 +557,33 @@ def _wipe_output(workspace: Path) -> None: output = workspace / "output" if not output.is_dir() or output.is_symlink(): return - for child in output.iterdir(): - if child.is_symlink() or child.is_file(): - child.unlink() - elif child.is_dir(): - shutil.rmtree(child) + # Read the index before deleting anything. A failed git read must not turn + # an unknown tracked set into permission to wipe user-controlled files. + result = subprocess.run( + ["git", "ls-files", "-z", "--", "output/"], + cwd=workspace, capture_output=True, check=True, + ) + tracked = { + workspace / os.fsdecode(name) + for name in result.stdout.split(b"\0") if name + } + protected = tracked | {parent for path in tracked for parent in path.parents} + + def remove_untracked(directory: Path) -> None: + for child in directory.iterdir(): + if child in tracked: + continue + if child in protected: + # A tracked descendant can have a symlink in its place in the + # working tree. Preserve it; never follow it outside output/. + if child.is_dir() and not child.is_symlink(): + remove_untracked(child) + elif child.is_symlink() or child.is_file(): + child.unlink() + elif child.is_dir(): + shutil.rmtree(child) + + remove_untracked(output) def _load_nonempty_lines(path: Path) -> list[str]: diff --git a/tests/test_smoke.py b/tests/test_smoke.py index 102231fd..1daaa50b 100644 --- a/tests/test_smoke.py +++ b/tests/test_smoke.py @@ -10,6 +10,48 @@ from heart import smoke +def test_wipe_output_preserves_index_and_removes_generated_files(tmp_path): + def git(*args): + return subprocess.run( + ["git", *args], cwd=tmp_path, check=True, + capture_output=True, text=True, + ).stdout + + git("init") + output = tmp_path / "output" + (output / "nested").mkdir(parents=True) + (output / ".gitignore").write_text("*\n") + sentinel = output / "nested" / "keep\n this.txt" + sentinel.write_text("tracked\n") + git("add", "-f", "output") + before = git("status", "--short") + (output / "nested" / "generated.txt").write_text("stale") + (output / "generated").mkdir() + (output / "generated" / "result").write_text("stale") + outside = tmp_path / "outside" + outside.mkdir() + (outside / "safe").write_text("safe") + (output / "link").symlink_to(outside, target_is_directory=True) + smoke._wipe_output(tmp_path) + assert sentinel.read_text() == "tracked\n" + assert (output / ".gitignore").exists() + assert sorted(p.name for p in output.iterdir()) == [".gitignore", "nested"] + assert list((output / "nested").iterdir()) == [sentinel] + assert (outside / "safe").read_text() == "safe" + # Ignore the deliberately untracked external fixture in the status check. + assert git("status", "--short", "--", "output") == before + + +def test_wipe_output_fails_closed_without_git_index(tmp_path): + output = tmp_path / "output" + output.mkdir() + sentinel = output / "keep" + sentinel.write_text("safe") + with pytest.raises(subprocess.CalledProcessError): + smoke._wipe_output(tmp_path) + assert sentinel.read_text() == "safe" + + # Fixture repo and root names are deliberately synthetic (LibraryA/LibraryB # chains under an `organism` root): nothing in smoke.py matches a spec against # a real repo list, so a real name here would be an instance fact in organ code From 76fde191cfd5199b367e85b7f3e0927cbe692d4c Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Sat, 17 Oct 2026 11:14:16 -0700 Subject: [PATCH 2/2] test: assert cleanup leaves a committed fixture clean --- tests/test_smoke.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_smoke.py b/tests/test_smoke.py index 1daaa50b..31068279 100644 --- a/tests/test_smoke.py +++ b/tests/test_smoke.py @@ -24,7 +24,9 @@ def git(*args): sentinel = output / "nested" / "keep\n this.txt" sentinel.write_text("tracked\n") git("add", "-f", "output") + git("-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-m", "fixture") before = git("status", "--short") + assert before == "" (output / "nested" / "generated.txt").write_text("stale") (output / "generated").mkdir() (output / "generated" / "result").write_text("stale")