diff --git a/tests/test_first_light.py b/tests/test_first_light.py index 3c6ac6a9f..5bd8acd59 100644 --- a/tests/test_first_light.py +++ b/tests/test_first_light.py @@ -1037,6 +1037,29 @@ def test_harness_env_passes_only_the_manifests_constants(self): else: os.environ[k] = v + def test_write_json_atomic_and_replace_failure_preserves_existing(self): + import first_light + import tempfile + from unittest import mock + + with tempfile.TemporaryDirectory() as tmp: + out = os.path.join(tmp, "manifest.json") + first_light._write_json_atomic(out, {"ok": True}) + self.assertEqual(os.listdir(tmp), ["manifest.json"]) + with open(out, "rb") as f: + raw = f.read() + self.assertTrue(raw.endswith(b"\n")) + self.assertNotIn(b"\r\n", raw) + + with open(out, "w", encoding="utf-8", newline="\n") as f: + f.write('{"keep": true}\n') + with mock.patch.object(first_light, "replace_retry", side_effect=OSError("replace denied")): + with self.assertRaises(OSError): + first_light._write_json_atomic(out, {"ok": False}) + self.assertEqual(os.listdir(tmp), ["manifest.json"]) + with open(out, encoding="utf-8") as f: + self.assertEqual(f.read(), '{"keep": true}\n') + if __name__ == "__main__": sys.exit(unittest.main()) diff --git a/tools/first_light.py b/tools/first_light.py index 75d14d7af..8eea433db 100644 --- a/tools/first_light.py +++ b/tools/first_light.py @@ -45,14 +45,47 @@ import struct import subprocess import sys +import tempfile TOOLS = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(TOOLS) sys.path.insert(0, os.path.join(ROOT, "runtime", "test", "shaders")) from compute_fixture import find_test_program # noqa: E402 sys.path.insert(0, TOOLS) # python -I leaves the script's own folder off sys.path +from file_mode import published_mode # noqa: E402 +from replace_retry import replace as replace_retry # noqa: E402 from stale_build import newer_sources # noqa: E402 + +def _write_json_atomic(path: str, payload: dict) -> None: + dest = os.path.realpath(path) + parent = os.path.dirname(dest) or "." + os.makedirs(parent, exist_ok=True) + fd, tmp = tempfile.mkstemp(dir=parent, prefix=".first_light-", suffix=".tmp") + try: + try: + f = os.fdopen(fd, "w", encoding="utf-8", newline="\n") + except BaseException: + os.close(fd) + raise + with f: + json.dump(payload, f, indent=1) + f.write("\n") + f.flush() + os.fsync(f.fileno()) + os.chmod(tmp, published_mode(dest)) + replace_retry(tmp, dest) + except BaseException: + try: + os.chmod(tmp, 0o600) + except OSError: + pass + try: + os.unlink(tmp) + except OSError: + pass + raise + DEFAULT_VS = "VS_main_170e32e6_Wrapped" DEFAULT_PS = "$PS_Rage_Untextured_14f8529b_Wrapped" DEFAULT_TEXTURED_PS = "PS_Copy_3ea4b38a_Wrapped" @@ -846,8 +879,7 @@ def run_draw(args, manifest: dict, out_dir: str) -> dict: os.unlink(path) if "result" in manifest: del manifest["result"] - with open(manifest_path, "w", encoding="utf-8", newline="\n") as f: - json.dump(manifest, f, indent=1) + _write_json_atomic(manifest_path, manifest) subprocess.run(cmd, check=True, env=env) if not os.path.isfile(raw): raise SystemExit(f"the harness exited successfully but did not write {raw}") @@ -868,8 +900,7 @@ def run_draw(args, manifest: dict, out_dir: str) -> dict: "ratio": result["ratio"], "ppm": ppm, } - with open(manifest_path, "w", encoding="utf-8", newline="\n") as f: - json.dump(manifest, f, indent=1) + _write_json_atomic(manifest_path, manifest) return result