diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cc905cb1bc..a65778677e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,6 +33,9 @@ jobs: steps: - uses: actions/checkout@v4 + - name: PR smoke-counter tests + run: python -B -m unittest discover -s scripts -p test_pr_check_smoke_counters.py -v + - name: Rust (stable, MSVC) run: | rustup toolchain install stable --profile minimal diff --git a/scripts/pr_check.py b/scripts/pr_check.py index b834254cd5..817d902720 100644 --- a/scripts/pr_check.py +++ b/scripts/pr_check.py @@ -88,7 +88,10 @@ def check(body, head, files, is_main_ancestor): if clean: pr = clean[-1] for key in ("device removals", "crashes"): - if (number(pr.get(key)) or 0) > 0: + value = pr.get(key, "") + if not re.fullmatch(r"[0-9]+", value): + problems.append(f"the PR's regression run needs a nonnegative integer {key}, got: {value or 'missing'}") + elif value.strip("0"): problems.append(f"the PR's regression run shows {key}: {pr.get(key)}") if pr.get("exit", "").startswith("killed"): problems.append("the PR's regression run did not end by itself (killed)") diff --git a/scripts/test_pr_check_smoke_counters.py b/scripts/test_pr_check_smoke_counters.py new file mode 100644 index 0000000000..0fbea668f8 --- /dev/null +++ b/scripts/test_pr_check_smoke_counters.py @@ -0,0 +1,67 @@ +"""Synthetic checker inputs, not measured title runs.""" +import unittest + +import pr_check + + +HEAD = "a" * 40 +MAIN = "b" * 40 +COUNTERS = ("device removals", "crashes") +RUNTIME = [("crates/xcl/src/lib.rs", "modified", False, lambda: 1)] +SECTIONS = "**Tested**\nSynthetic input.\n**Not tested**\nNo title run.\n**AI use**\nNone.\n" + + +def block(commit, overrides=None): + fields = {"commit": commit, "title": "synthetic-counter-fixture", "seconds": "60", + "exit": "0x0", "frames shown": "100 (PresentX 100)", "peak commit": "100 MB", + "device removals": "0", "crashes": "0"} + fields.update(overrides or {}) + lines = ["```", "xcl-smoke v1"] + lines.extend(f"{key}: {value}" for key, value in fields.items() if value is not None) + return "\n".join(lines + ["```", ""]) + + +def problems(pr=None, main=None): + body = SECTIONS + block(MAIN, main) + block(HEAD, pr) + return pr_check.check(body, HEAD, RUNTIME, lambda sha: sha == MAIN) + + +class SmokeCounterTests(unittest.TestCase): + def test_zero_counters_are_accepted(self): + for value in ("0", "000", " 0 ", "0" * 5000): + with self.subTest(value=value[:20]): + self.assertEqual(problems(dict.fromkeys(COUNTERS, value)), []) + + def test_each_positive_counter_is_refused(self): + for key in COUNTERS: + for value in ("1", "0002", "9" * 5000): + with self.subTest(key=key, value=value[:20]): + self.assertTrue(any(f"shows {key}:" in p for p in problems({key: value}))) + + def test_each_missing_or_empty_counter_is_refused(self): + for key in COUNTERS: + for value in (None, "", " "): + with self.subTest(key=key, value=value): + self.assertTrue(any(key in p for p in problems({key: value}))) + + def test_each_malformed_counter_is_refused(self): + for key in COUNTERS: + for value in ("unknown", "n/a", "-1", "+0", "0x0", "0.0", "0 trailing", "\u0660"): + with self.subTest(key=key, value=value): + self.assertTrue(any(key in p for p in problems({key: value}))) + + def test_a_crashing_main_baseline_remains_allowed(self): + self.assertEqual(problems(main={"exit": "0xc0000005", "crashes": "2", "device removals": "1"}), []) + + def test_runtime_changes_still_require_both_reports(self): + result = pr_check.check(SECTIONS, HEAD, RUNTIME, lambda sha: sha == MAIN) + self.assertTrue(any(HEAD[:12] in p for p in result)) + self.assertTrue(any("commit of main" in p for p in result)) + + def test_scripts_do_not_require_a_title_report(self): + files = [("scripts/pr_check.py", "modified", False, lambda: 1)] + self.assertEqual(pr_check.check(SECTIONS, HEAD, files, lambda sha: False), []) + + +if __name__ == "__main__": + unittest.main()