Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

check.sh: keep a failing step's log and print its path (#2222)

Eyal Golanbc97bed91e03
diff --git a/scripts/check.sh b/scripts/check.sh
index 596656768..3bb9053ea 100755
--- a/scripts/check.sh
+++ b/scripts/check.sh
@@ -2,7 +2,8 @@
# One command for "does my change pass?": the same checks CI runs, in order, cheapest first. Needs dotnet and uv, not
# Unity (Unity edit-mode tests are not run by CI either; CONTRIBUTING.md, "What CI runs").
# Where a Unity editor and a unity/Library exist, it also compiles unity/ with dotnet (scripts/unity_compile/).
-# Stops at the first failure, prints the last lines of that step's output and exits non-zero.
+# Stops at the first failure, prints the last lines of that step's output and exits non-zero. The failing step's
+# whole log is kept in .superpowers/check-logs/ (git ignores it), and its path printed; passing runs keep nothing.
#
# Usage: scripts/check.sh [BASE_REF] BASE_REF defaults to $CHECK_BASE, then origin/main.
# The changelog step compares BASE_REF...HEAD, so it sees committed changes only (as CI does). Set
@@ -31,6 +32,11 @@ run() {
echo "FAILED: $name"
echo "-- last $tail_lines lines of: $*"
tail -n "$tail_lines" "$log"
+ # Keep the whole log (the trap deletes the temp one), so a flaky failure's message is not lost.
+ local slug kept
+ slug="$(printf '%s' "$name" | sed 's/[^A-Za-z0-9._-]\{1,\}/-/g; s/^-//; s/-$//')"
+ kept="$PWD/.superpowers/check-logs/$slug-$(date -u +%Y%m%dT%H%M%SZ).log"
+ if mkdir -p "${kept%/*}" && cp "$log" "$kept"; then echo "kept the failing step's log: $kept"; fi
exit 1
fi
tail -n 3 "$log"
diff --git a/scripts/oss/tests/test_check_script.py b/scripts/oss/tests/test_check_script.py
new file mode 100644
index 000000000..3422e0d07
--- /dev/null
+++ b/scripts/oss/tests/test_check_script.py
@@ -0,0 +1,53 @@
+"""scripts/check.sh keeps a failing step's log: a flaky test's message was lost when the temp log was deleted."""
+import os
+import shutil
+import stat
+import subprocess
+from pathlib import Path
+
+import pytest
+from conftest import commit_all
+
+ROOT = Path(__file__).resolve().parents[3]
+
+pytestmark = pytest.mark.skipif(shutil.which("bash") is None, reason="check.sh needs bash")
+
+
+def _run_check(make_repo, tmp_path, uv_exit: int):
+ # A copy of check.sh in a throwaway repo, with stand-in uv and dotnet on PATH so no real step runs.
+ repo = make_repo({
+ "scripts/check.sh": (ROOT / "scripts/check.sh").read_text(encoding="utf-8"),
+ "unity/ProjectSettings/ProjectVersion.txt": "m_EditorVersion: 0.0.0f1\n",
+ })
+ commit_all(repo, "base")
+ fake = tmp_path / "bin"
+ fake.mkdir()
+ for tool, code in (("uv", uv_exit), ("dotnet", 0)):
+ p = fake / tool
+ p.write_text(f"#!/bin/sh\necho \"{tool} stand-in: the detail that used to be lost\"\nexit {code}\n")
+ p.chmod(p.stat().st_mode | stat.S_IXUSR)
+ env = {**os.environ, "PATH": f"{fake}{os.pathsep}{os.environ['PATH']}",
+ "CHECK_UNITY_CONTENTS": str(tmp_path / "no-unity")}
+ r = subprocess.run(["bash", "scripts/check.sh", "HEAD"], cwd=repo, env=env, capture_output=True, text=True)
+ return repo, r
+
+
+def test_a_failing_step_keeps_its_log_and_prints_the_path(make_repo, tmp_path):
+ repo, r = _run_check(make_repo, tmp_path, uv_exit=1)
+ assert r.returncode == 1, r.stdout + r.stderr
+ assert "FAILED: changelog (against HEAD)" in r.stdout
+ kept = list((repo / ".superpowers/check-logs").glob("changelog-against-HEAD-*.log"))
+ assert len(kept) == 1, kept
+ assert "uv stand-in: the detail that used to be lost" in kept[0].read_text(encoding="utf-8")
+ last = r.stdout.rstrip().splitlines()[-1]
+ prefix = "kept the failing step's log: "
+ assert last.startswith(prefix), r.stdout
+ assert Path(last[len(prefix):]).resolve() == kept[0].resolve()
+
+
+def test_a_passing_run_keeps_no_log(make_repo, tmp_path):
+ repo, r = _run_check(make_repo, tmp_path, uv_exit=0)
+ assert r.returncode == 0, r.stdout + r.stderr
+ assert r.stdout.rstrip().splitlines()[-1] == "ALL CHECKS PASSED"
+ assert "kept the failing step's log" not in r.stdout
+ assert not (repo / ".superpowers/check-logs").exists()