diff --git a/.github/workflows/auto-format.yml b/.github/workflows/auto-format.yml index 5b520ca1ab..3bba07d12d 100644 --- a/.github/workflows/auto-format.yml +++ b/.github/workflows/auto-format.yml @@ -56,7 +56,9 @@ jobs: if [[ "$GITHUB_EVENT_NAME" == "schedule" ]]; then git log --since="25 hours ago" --pretty=format: --name-only \ | sort -u | while IFS= read -r f; do - [[ -n "$f" && -f "$f" ]] && printf '%s\n' "$f" + if [[ -n "$f" && -f "$f" ]]; then + printf '%s\n' "$f" + fi done > "$changed_file" else base_sha="$BEFORE_SHA" diff --git a/scripts/tests/test_auto_format_workflow.py b/scripts/tests/test_auto_format_workflow.py new file mode 100644 index 0000000000..5c8ce84b49 --- /dev/null +++ b/scripts/tests/test_auto_format_workflow.py @@ -0,0 +1,53 @@ +"""Exercise the scheduled auto-format workflow using a real git history.""" + +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + + +WORKFLOW = Path(__file__).resolve().parents[2] / ".github/workflows/auto-format.yml" + + +class AutoFormatWorkflowTest(unittest.TestCase): + def test_deleted_final_path_does_not_abort_scheduled_filter(self): + # Execute the workflow's real run block, not a copy of its filter. + workflow = WORKFLOW.read_text() + block = workflow.split(" - name: Format changed files with treefmt and commit any fixes\n", 1)[1] + script = block.split(" run: |\n", 1)[1] + script = "\n".join(line[10:] for line in script.splitlines() if line.startswith(" ")) + + with tempfile.TemporaryDirectory() as temp: + root = Path(temp) + repo = root / "repo" + repo.mkdir() + subprocess.run(["git", "init", "-q", str(repo)], check=True) + def git(*args): + subprocess.run(["git", *args], cwd=repo, check=True, capture_output=True) + + git("config", "user.name", "Test") + git("config", "user.email", "test@example.com") + (repo / "a-existing").write_text("keep\n") + (repo / "z-deleted").write_text("delete\n") + git("add", ".") + git("commit", "-qm", "initial") + (repo / "z-deleted").unlink() + git("add", "-u") + git("commit", "-qm", "delete final path") + + bindir = root / "bin" + bindir.mkdir() + nix = bindir / "nix" + nix.write_text('#!/bin/sh\nprintf "%s\\n" "$@" > "$NIX_ARGS"\n') + nix.chmod(0o755) + args = root / "nix-args" + env = {**os.environ, "PATH": f"{bindir}:{os.environ['PATH']}", + "GITHUB_EVENT_NAME": "schedule", "NIX_ARGS": str(args)} + result = subprocess.run(["bash", "-c", script], cwd=repo, env=env, text=True, capture_output=True) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(args.read_text().splitlines(), ["run", ".#format", "--", "a-existing"]) + + +if __name__ == "__main__": + unittest.main()