From cc2f28a60f5ef079af7b9f359e5887a9f22f793a Mon Sep 17 00:00:00 2001 From: Sebastian Kondratowicz Date: Wed, 25 Nov 2026 12:48:01 +0100 Subject: [PATCH] Scripts: reject failed sweeps before updating the coverage baseline A failed xcl process can leave a valid JSON report on stdout. Check its exit status before accepting the report so a partial result cannot pass the gate or replace a known-good baseline. Fixes #2260. --- scripts/coverage_gate.py | 3 +++ scripts/test_coverage_gate.py | 26 +++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/scripts/coverage_gate.py b/scripts/coverage_gate.py index e7ed84d66..00703357d 100644 --- a/scripts/coverage_gate.py +++ b/scripts/coverage_gate.py @@ -69,6 +69,9 @@ def main(argv=None): if args.threads: cmd += ["--threads", str(args.threads)] proc = subprocess.run(cmd, capture_output=True, text=True) + if proc.returncode != 0: + print(f"sweep exited with code {proc.returncode}\n{proc.stderr[-2000:]}", file=sys.stderr) + return 2 try: run = sweep_json(proc.stdout) except ValueError as e: diff --git a/scripts/test_coverage_gate.py b/scripts/test_coverage_gate.py index 314e3916e..faee2f0c2 100644 --- a/scripts/test_coverage_gate.py +++ b/scripts/test_coverage_gate.py @@ -1,8 +1,13 @@ +import io import json +import subprocess +import tempfile import unittest +from contextlib import redirect_stderr from pathlib import Path +from unittest.mock import patch -from coverage_gate import compare, sweep_json +from coverage_gate import compare, main, sweep_json STAGES = { "CS": {"total": 100, "translated": 95, "unsupported": 5, "validator_failed": 0, "panicked": 0, "timeout": 0}, @@ -68,5 +73,24 @@ def test_raises_without_a_report(self): sweep_json("no json here") +class FailedSweepTests(unittest.TestCase): + def test_nonzero_exit_rejects_valid_report_and_preserves_baseline(self): + report = json.dumps(run(CS={"total": 95, "unsupported": 0})) + failed = subprocess.CompletedProcess(["xcl"], 7, stdout=report, stderr="worker failed") + with tempfile.TemporaryDirectory() as directory: + baseline = Path(directory) / "baseline.json" + original = json.dumps(run()).encode("utf-8") + for options in ([], ["--update"]): + with self.subTest(options=options), patch("coverage_gate.subprocess.run", return_value=failed): + baseline.write_bytes(original) + errors = io.StringIO() + with redirect_stderr(errors): + code = main(["--xcl", "xcl", "--rpf", "pack", "--baseline", str(baseline), *options]) + self.assertEqual(baseline.read_bytes(), original) + self.assertEqual(code, 2) + self.assertIn("7", errors.getvalue()) + self.assertIn("worker failed", errors.getvalue()) + + if __name__ == "__main__": unittest.main()