diff --git a/.github/workflows/mcpx.yml b/.github/workflows/mcpx.yml index ca1974ef67..9c3d6cf91a 100644 --- a/.github/workflows/mcpx.yml +++ b/.github/workflows/mcpx.yml @@ -202,6 +202,7 @@ jobs: RUN: ${{ github.run_id }}/${{ github.run_attempt }} run: | rc=0 + python3 scripts/test_gotest_report.py || rc=1 go test ./... -count=1 -json >"$RUNNER_TEMP/gotest.json" || rc=$? # Readable output for the log, from the same run. Package-level # output first, then the full output of every failing test, so a @@ -234,7 +235,7 @@ jobs: python3 scripts/gotest-report.py "$RUNNER_TEMP/gotest.json" \ "$(jq -r '.MCPX_GO_TESTS // ""' <<<"$VARS")" "$RUN" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ "$GITHUB_STEP_SUMMARY" "$RUNNER_TEMP/gotest-record.json" || rc=1 - if [ -f "$RUNNER_TEMP/gotest-record.json" ] && [ "$ON_MAIN" = true ]; then + if [ -f "$RUNNER_TEMP/gotest-record.json" ] && [ "$ON_MAIN" = true ] && [ "$rc" -eq 0 ]; then if [ -n "$GH_TOKEN" ]; then gh variable set MCPX_GO_TESTS --repo "$GITHUB_REPOSITORY" --body "$(cat "$RUNNER_TEMP/gotest-record.json")" echo "- saved \`MCPX_GO_TESTS\`" >>"$GITHUB_STEP_SUMMARY" diff --git a/pkgs/mcpx/scripts/gotest-report.py b/pkgs/mcpx/scripts/gotest-report.py index 2df22ee889..32151ce83b 100755 --- a/pkgs/mcpx/scripts/gotest-report.py +++ b/pkgs/mcpx/scripts/gotest-report.py @@ -18,7 +18,7 @@ with its own guard test; counting it here would make the documented quarantine path unusable the first time anyone used it. -Exits 1 when any test failed or more tests were skipped than the record. +Exits 1 when any test/package failed or more tests were skipped than the record. is written when the record should change; saving it is the workflow's decision, on main only. """ @@ -30,35 +30,48 @@ def read(path): counts = collections.Counter() out = collections.defaultdict(list) - skipped, failed = [], [] + skipped, failed, package_failed = [], [], [] + failed_packages = set() for line in open(path, errors="replace"): try: e = json.loads(line) except ValueError: continue test = e.get("Test") + act = e.get("Action") if not test: + if act == "fail": + package_failed.append(e.get("Package") or "(unknown package)") continue key = f"{e.get('Package', '').rsplit('/', 1)[-1]}.{test}" - act = e.get("Action") if act == "output": out[key].append(e.get("Output", "")) elif act in ("pass", "fail", "skip"): counts[act] += 1 (skipped if act == "skip" else failed if act == "fail" else []).append(key) + if act == "fail": + failed_packages.add(e.get("Package") or "(unknown package)") reasons = {} for k in skipped: lines = [o.strip() for o in out[k] if o.strip() and not o.lstrip().startswith(("=== ", "--- "))] reasons[k] = (lines[-1] if lines else "(no reason given)")[:200] quarantined = sorted(k for k in skipped if "quarantined until " in reasons[k]) counted = sorted(k for k in skipped if k not in quarantined) - return counts, counted, sorted(failed), reasons, quarantined, sorted(skipped) + # A failing test also emits a package-level fail for its package; only + # packages without their own test failure are independent package failures + # (build failures, TestMain failures). This keeps one failing test at one + # failure instead of double-counting the package event. + isolated = sorted(set(package_failed) - failed_packages) + return counts, counted, sorted(failed), reasons, quarantined, sorted(skipped), isolated def decide(skipped, passed, failed_n, stored, run, now): """Returns (new_record_or_None, rose, new_names, gone_names).""" cur = {"timestamp": now, "run": run, "passed_count": passed, "failed_count": failed_n, "skipped_count": len(skipped), "skipped": skipped} + # Never let an incomplete or failing run lower the main-branch ceiling. + if failed_n: + return None, False, [], [] if not stored: return cur, False, [], [] prev = set(stored.get("skipped", [])) @@ -72,18 +85,22 @@ def decide(skipped, passed, failed_n, stored, run, now): def main(path, stored_raw, run, now, summary, new_file): - counts, skipped, failed, reasons, quarantined, all_skipped = read(path) + counts, skipped, failed, reasons, quarantined, all_skipped, package_failed = read(path) stored = json.loads(stored_raw) if stored_raw.strip() else None - new, rose, new_names, gone = decide(skipped, counts["pass"], counts["fail"], stored, run, now) + failed_n = counts["fail"] + len(package_failed) + new, rose, new_names, gone = decide(skipped, counts["pass"], failed_n, stored, run, now) ceiling = stored["skipped_count"] if stored else None with open(summary, "a") as f: f.write("## Go tests\n\n| passed | failed | skipped | record on main |\n| --- | --- | --- | --- |\n") - f.write(f"| {counts['pass']} | {counts['fail']} | **{len(skipped)}** | " + f.write(f"| {counts['pass']} | {failed_n} | **{len(skipped)}** | " f"{ceiling if ceiling is not None else 'none yet'}" f"{' (run ' + stored['run'] + ')' if stored else ''} |\n\n") if failed: f.write("> [!CAUTION]\n> # Failed tests\n" + "".join(f"> - {k}\n" for k in failed) + "\n") + if package_failed: + f.write("> [!CAUTION]\n> # Failed packages\n" + + "".join(f"> - `{package}`\n" for package in package_failed) + "\n") if rose: f.write(f"> [!CAUTION]\n> # More tests skipped than the record: {len(skipped)} against {ceiling}\n" "> A skipped test passes nothing. Newly skipped:\n") @@ -108,8 +125,10 @@ def main(path, stored_raw, run, now, summary, new_file): f.write(f"| `{k}` | {reasons[k].replace('|', chr(92) + '|')} |\n") f.write("\n\n") - print(f"go tests: {counts['pass']} passed, {counts['fail']} failed, {len(skipped)} skipped " + print(f"go tests: {counts['pass']} passed, {failed_n} failed, {len(skipped)} skipped " f"(record: {ceiling if ceiling is not None else 'none yet'})") + for package in package_failed: + print(f" package fail {package}") for k in all_skipped: print(f" skip {k} -- {reasons[k]}") if rose: @@ -118,7 +137,7 @@ def main(path, stored_raw, run, now, summary, new_file): if new is not None: with open(new_file, "w") as f: json.dump(new, f, separators=(",", ":")) - return 1 if (failed or rose) else 0 + return 1 if (failed or package_failed or rose) else 0 if __name__ == "__main__": diff --git a/pkgs/mcpx/scripts/test_gotest_report.py b/pkgs/mcpx/scripts/test_gotest_report.py new file mode 100644 index 0000000000..cd07f4fa9c --- /dev/null +++ b/pkgs/mcpx/scripts/test_gotest_report.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 +"""Synthetic go test -json regressions for gotest-report.py.""" +import json +import pathlib +import subprocess +import tempfile +import unittest + + +REPORTER = pathlib.Path(__file__).with_name("gotest-report.py") +STORED = json.dumps({ + "timestamp": "2026-01-01T00:00:00Z", + "run": "previous", + "passed_count": 10, + "failed_count": 0, + "skipped_count": 2, + "skipped": ["widgets.TestA", "widgets.TestB"], +}) + + +class GoTestReportTests(unittest.TestCase): + def run_report(self, events, stored=STORED): + with tempfile.TemporaryDirectory() as directory: + root = pathlib.Path(directory) + event_file = root / "events.json" + summary = root / "summary.md" + record = root / "record.json" + event_file.write_text("".join(json.dumps(event) + "\n" for event in events)) + result = subprocess.run( + ["python3", str(REPORTER), str(event_file), stored, "run/1", + "2026-11-02T00:00:00Z", str(summary), str(record)], + capture_output=True, text=True, check=False, + ) + return result, summary.read_text(), json.loads(record.read_text()) if record.exists() else None + + def test_build_failure_without_test_is_reported_and_does_not_lower_record(self): + result, summary, record = self.run_report([ + {"Action": "output", "Package": "example.test/mcpx/widgets", "Output": "undefined: missing\n"}, + {"Action": "fail", "Package": "example.test/mcpx/widgets"}, + ]) + self.assertNotEqual(result.returncode, 0) + self.assertIn("example.test/mcpx/widgets", result.stdout) + self.assertIn("example.test/mcpx/widgets", summary) + self.assertIsNone(record) + + def test_testmain_failure_without_test_is_reported_and_does_not_lower_record(self): + result, summary, record = self.run_report([ + {"Action": "output", "Package": "example.test/mcpx/daemon", "Output": "TestMain: startup failed\n"}, + {"Action": "fail", "Package": "example.test/mcpx/daemon"}, + ]) + self.assertNotEqual(result.returncode, 0) + self.assertIn("example.test/mcpx/daemon", result.stdout) + self.assertIn("Failed packages", summary) + self.assertIsNone(record) + + def test_test_failure_prevents_record_even_when_skip_count_falls(self): + result, _, record = self.run_report([ + {"Action": "fail", "Package": "example.test/mcpx/widgets", "Test": "TestBroken"}, + {"Action": "pass", "Package": "example.test/mcpx/widgets", "Test": "TestGood"}, + ]) + self.assertNotEqual(result.returncode, 0) + self.assertIsNone(record) + + def test_test_failure_plus_package_event_counts_once(self): + result, summary, record = self.run_report([ + {"Action": "fail", "Package": "example.test/mcpx/widgets", "Test": "TestBroken"}, + {"Action": "fail", "Package": "example.test/mcpx/widgets"}, + {"Action": "pass", "Package": "example.test/mcpx/widgets", "Test": "TestGood"}, + ]) + self.assertNotEqual(result.returncode, 0) + self.assertIn("1 failed", result.stdout) + self.assertNotIn("Failed packages", summary) + self.assertIsNone(record) + + def test_clean_pass_stream_can_write_lower_record(self): + result, _, record = self.run_report([ + {"Action": "pass", "Package": "example.test/mcpx/widgets", "Test": "TestGood"}, + ]) + self.assertEqual(result.returncode, 0) + self.assertEqual(record["skipped_count"], 0) + + def test_skip_increase_fails_without_record(self): + result, _, record = self.run_report([ + {"Action": "skip", "Package": "example.test/mcpx/widgets", "Test": "TestNew"}, + {"Action": "output", "Package": "example.test/mcpx/widgets", "Test": "TestNew", + "Output": " skip reason\n"}, + ], stored=json.dumps({"skipped_count": 0, "skipped": [], "run": "previous"})) + self.assertNotEqual(result.returncode, 0) + self.assertIsNone(record) + + def test_quarantine_skip_remains_exempt_from_skip_record(self): + result, _, record = self.run_report([ + {"Action": "skip", "Package": "example.test/mcpx/widgets", "Test": "TestQuarantined"}, + {"Action": "output", "Package": "example.test/mcpx/widgets", "Test": "TestQuarantined", + "Output": "quarantined until 2027-01-01 (https://github.com/example/1)\n"}, + ], stored="") + self.assertEqual(result.returncode, 0) + self.assertEqual(record["skipped_count"], 0) + + +if __name__ == "__main__": + unittest.main()