Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions heart/smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,50 @@
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")
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")
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
Expand Down
Loading