From 5310ccce9ff14d9e0faabb4b2af2b770c27b42ff Mon Sep 17 00:00:00 2001 From: luckyteagirl <290248872+luckyteagirl@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:53:26 -0800 Subject: [PATCH] Scripts: replace coverage baselines atomically Stage the complete coverage report beside the destination before replacing it. If the replacement fails, keep the previous baseline and clean up the temporary file. Preserve the existing baseline mode on POSIX. Assisted-by: Codex (GPT-6.2-Sol) --- scripts/coverage_gate.py | 32 ++++++++++++++++++++++++-- scripts/test_coverage_gate.py | 43 ++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/scripts/coverage_gate.py b/scripts/coverage_gate.py index e7ed84d66a..7cf0b585d0 100644 --- a/scripts/coverage_gate.py +++ b/scripts/coverage_gate.py @@ -14,8 +14,11 @@ """ import argparse import json +import os +import stat import subprocess import sys +import tempfile from pathlib import Path DEFAULT_BASELINE = Path(__file__).resolve().parent.parent / "tests" / "data" / "coverage-baseline.json" @@ -51,6 +54,28 @@ def compare(baseline, run): return bad, notes +def write_baseline(path, stages): + data = json.dumps({"stages": stages}, indent=1, sort_keys=True) + "\n" + path.parent.mkdir(parents=True, exist_ok=True) + path = path.resolve() + temp_path = None + try: + with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=path.parent, delete=False) as temp: + temp_path = Path(temp.name) + temp.write(data) + if os.name == "posix": + try: + mode = stat.S_IMODE(path.stat().st_mode) + except FileNotFoundError: + pass + else: + temp_path.chmod(mode) + os.replace(temp_path, path) + finally: + if temp_path is not None: + temp_path.unlink(missing_ok=True) + + def main(argv=None): ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) ap.add_argument("--xcl", type=Path, help="xcl binary (required unless --run-json)") @@ -75,8 +100,11 @@ def main(argv=None): print(f"{e}\n{proc.stdout[-2000:]}\n{proc.stderr[-2000:]}", file=sys.stderr) return 2 if args.update: - args.baseline.parent.mkdir(parents=True, exist_ok=True) - args.baseline.write_text(json.dumps({"stages": run["stages"]}, indent=1, sort_keys=True) + "\n", encoding="utf-8") + try: + write_baseline(args.baseline, run["stages"]) + except OSError as e: + print(f"could not write coverage baseline {args.baseline}: {e}", file=sys.stderr) + return 2 print(f"baseline written: {args.baseline}") return 0 if not args.baseline.is_file(): diff --git a/scripts/test_coverage_gate.py b/scripts/test_coverage_gate.py index 314e3916e8..a375a939de 100644 --- a/scripts/test_coverage_gate.py +++ b/scripts/test_coverage_gate.py @@ -1,8 +1,14 @@ +from contextlib import redirect_stderr, redirect_stdout +import io import json +import os +import stat +import tempfile import unittest from pathlib import Path +from unittest.mock import patch -from coverage_gate import compare, sweep_json +from coverage_gate import compare, main, sweep_json, write_baseline STAGES = { "CS": {"total": 100, "translated": 95, "unsupported": 5, "validator_failed": 0, "panicked": 0, "timeout": 0}, @@ -68,5 +74,40 @@ def test_raises_without_a_report(self): sweep_json("no json here") +class UpdateTests(unittest.TestCase): + @unittest.skipUnless(os.name == "posix", "requires POSIX file modes") + def test_replacement_preserves_the_previous_baseline_mode(self): + with tempfile.TemporaryDirectory() as tmp: + baseline = Path(tmp) / "baseline.json" + baseline.touch() + baseline.chmod(0o664) + + write_baseline(baseline, STAGES) + + self.assertEqual(stat.S_IMODE(baseline.stat().st_mode), 0o664) + + def test_failed_replacement_preserves_the_previous_baseline(self): + with tempfile.TemporaryDirectory() as tmp: + directory = Path(tmp) + baseline = directory / "baseline.json" + report = directory / "sweep.json" + previous = json.dumps(run(PS={"translated": 190})).encode("utf-8") + baseline.write_bytes(previous) + report.write_text(json.dumps(run()), encoding="utf-8") + args = ["--baseline", str(baseline), "--run-json", str(report), "--update"] + + stderr = io.StringIO() + with patch("os.replace", side_effect=OSError("simulated replace failure")), redirect_stderr(stderr): + self.assertEqual(main(args), 2) + self.assertIn("simulated replace failure", stderr.getvalue()) + self.assertEqual(baseline.read_bytes(), previous) + self.assertCountEqual(directory.iterdir(), [baseline, report]) + + with redirect_stdout(io.StringIO()): + self.assertEqual(main(args), 0) + self.assertEqual(json.loads(baseline.read_text(encoding="utf-8")), run()) + self.assertCountEqual(directory.iterdir(), [baseline, report]) + + if __name__ == "__main__": unittest.main()