diff --git a/pkgs/mcpx/scripts/gotest-report.py b/pkgs/mcpx/scripts/gotest-report.py index 5792bff849..99dc1771e0 100755 --- a/pkgs/mcpx/scripts/gotest-report.py +++ b/pkgs/mcpx/scripts/gotest-report.py @@ -24,8 +24,11 @@ """ import collections +import datetime import json +import re import sys +from urllib.parse import urlsplit class StoredRecordError(ValueError): @@ -84,7 +87,50 @@ def parse_stored(raw): _EVENT_STRING_FIELDS = ("Package", "Test", "Action", "Output") -def read(path): +QUARANTINE_NOTICE = re.compile( + r"quarantined until (?P\d{4}-\d{2}-\d{2}) " + r"\((?P[^()\s]+)\)" +) +GITHUB_ISSUE_PATH = re.compile(r"/[^/\s?#]+/[^/\s?#]+/issues/[1-9]\d*") +# In the real `go test -json` stream, a test's skip message carries the +# Go-generated source location of the t.Skipf call site: +# "widgets_test.go:42: quarantined until ...". That prefix is part of +# the output format, not of the notice, so strip exactly that shape +# before validating the complete notice. +GO_SOURCE_PREFIX = re.compile(r"[^\s/:]+\.go:\d+: ") + + +def is_valid_quarantine_notice(reason, today): + match = QUARANTINE_NOTICE.fullmatch(GO_SOURCE_PREFIX.sub("", reason, count=1)) + if not match: + return False + + expiry_text = match.group("expiry") + try: + expiry = datetime.date.fromisoformat(expiry_text) + issue = urlsplit(match.group("issue")) + except ValueError: + return False + + return ( + expiry.isoformat() == expiry_text + and expiry >= today + and issue.scheme == "https" + and issue.netloc == "github.com" + and GITHUB_ISSUE_PATH.fullmatch(issue.path) is not None + and not issue.query + and not issue.fragment + ) + + +def utc_date(now): + parsed = datetime.datetime.fromisoformat(now.replace("Z", "+00:00")) + if parsed.tzinfo is None: + parsed = parsed.replace(tzinfo=datetime.timezone.utc) + return parsed.astimezone(datetime.timezone.utc).date() + + +def read(path, today): counts = collections.Counter() out = collections.defaultdict(list) skipped, failed, package_failed = [], [], [] @@ -122,7 +168,9 @@ def read(path): 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]) + quarantined = sorted( + k for k in skipped if is_valid_quarantine_notice(reasons[k], today) + ) counted = sorted(k for k in skipped if k not in quarantined) # A failing test also emits a package-level fail for its package; only # packages without their own test failure are independent package failures @@ -169,7 +217,7 @@ 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, package_failed = read( - path + path, utc_date(now) ) try: stored = parse_stored(stored_raw) diff --git a/pkgs/mcpx/scripts/test_gotest_report.py b/pkgs/mcpx/scripts/test_gotest_report.py index 4d233156e1..8569785afe 100644 --- a/pkgs/mcpx/scripts/test_gotest_report.py +++ b/pkgs/mcpx/scripts/test_gotest_report.py @@ -166,7 +166,7 @@ def test_quarantine_skip_remains_exempt_from_skip_record(self): "Action": "output", "Package": "example.test/mcpx/widgets", "Test": "TestQuarantined", - "Output": "quarantined until 2027-01-01 (https://github.com/example/1)\n", + "Output": "quarantined until 2027-01-01 (https://github.com/developing-today/code/issues/1973)\n", }, ], stored="", @@ -174,6 +174,86 @@ def test_quarantine_skip_remains_exempt_from_skip_record(self): self.assertEqual(result.returncode, 0) self.assertEqual(record["skipped_count"], 0) + def test_quarantine_skip_with_go_source_prefix_remains_exempt(self): + # The actual `go test -json` output format: Go prefixes the + # skip message with the t.Skipf call site ("file.go:line: "). + result, summary, record = self.run_report( + [ + { + "Action": "skip", + "Package": "example.test/mcpx/widgets", + "Test": "TestQuarantined", + }, + { + "Action": "output", + "Package": "example.test/mcpx/widgets", + "Test": "TestQuarantined", + "Output": "widgets_test.go:42: quarantined until 2027-01-01 (https://github.com/developing-today/code/issues/1973)\n", + }, + ], + stored="", + ) + self.assertEqual(result.returncode, 0) + self.assertEqual(record["skipped_count"], 0) + self.assertIn("1 quarantined under #1973", summary) + + def test_quarantine_expiring_today_remains_exempt(self): + result, summary, 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 2026-11-02 (https://github.com/developing-today/code/issues/1973)\n", + }, + ], + stored="", + ) + self.assertEqual(result.returncode, 0) + self.assertEqual(record["skipped_count"], 0) + self.assertIn("1 quarantined under #1973", summary) + + def test_invalid_quarantine_notices_count_against_skip_record(self): + invalid_notices = [ + "quarantined until banana (https://github.com/developing-today/code/issues/1973)", + "quarantined until 2026-02-30 (https://github.com/developing-today/code/issues/1973)", + "quarantined until 2000-01-01 (https://github.com/developing-today/code/issues/1973)", + "this mentions quarantined until 2027-01-01 (https://github.com/developing-today/code/issues/1973)", + "quarantined until 2027-01-01 (https://github.com.github.invalid/developing-today/code/issues/1973)", + "quarantined until 2027-01-01 (https://github.com/developing-today/code/pull/1973)", + "widgets_test.go:42: quarantined until 2000-01-01 (https://github.com/developing-today/code/issues/1973)", + "widgets_test.go:42: this mentions quarantined until 2027-01-01 (https://github.com/developing-today/code/issues/1973)", + ] + empty_record = json.dumps({"skipped_count": 0, "skipped": [], "run": "previous"}) + + for reason in invalid_notices: + with self.subTest(reason=reason): + result, summary, record = self.run_report( + [ + { + "Action": "skip", + "Package": "example.test/mcpx/widgets", + "Test": "TestQuarantined", + }, + { + "Action": "output", + "Package": "example.test/mcpx/widgets", + "Test": "TestQuarantined", + "Output": reason + "\n", + }, + ], + stored=empty_record, + ) + self.assertNotEqual(result.returncode, 0) + self.assertIn("More tests skipped than the record", summary) + self.assertNotIn("quarantined under #1973", summary) + self.assertIsNone(record) + def run_report_raw(self, event_lines, stored=STORED): with tempfile.TemporaryDirectory() as directory: