diff --git a/src/repo2rlenv/log_parsers/jest_parser.py b/src/repo2rlenv/log_parsers/jest_parser.py index 63264189..b6cd3291 100644 --- a/src/repo2rlenv/log_parsers/jest_parser.py +++ b/src/repo2rlenv/log_parsers/jest_parser.py @@ -115,6 +115,7 @@ # use it on Windows), so a √-only log is recognized by mocha's epilogue. _MOCHA_MARKER_RE = re.compile(r"^\s*(?:\d+ passing \(|✔ )", re.MULTILINE) _MOCHA_EPILOGUE_RE = re.compile(r"^\s*\d+ passing \(") +_MOCHA_PENDING_RE = re.compile(r"^\s*\d+ pending\b") _MOCHA_FAILING_RE = re.compile(r"^\s*(?P\d+) failing\b") _MOCHA_TEST_RE = re.compile( r"^(?P\s*)(?:(?P[✔✓√])|(?P\d+\))|(?P-))\s" @@ -129,7 +130,9 @@ def _parse_mocha(log: str) -> dict[str, TestStatus]: # dropped once a shallower line appears. suites: list[tuple[int, str]] = [] in_epilogue = False + in_failure_body = False failures_left = 0 + next_failure = 1 for raw in log.split("\n"): line = raw.rstrip() @@ -138,25 +141,64 @@ def _parse_mocha(log: str) -> dict[str, TestStatus]: continue indent = len(line) - len(stripped) - if _MOCHA_EPILOGUE_RE.match(line): - in_epilogue, failures_left = True, 0 + if not (in_epilogue and failures_left > 0) and _MOCHA_EPILOGUE_RE.match(line): + suites = [] + in_epilogue = True + in_failure_body = False + failures_left = 0 + next_failure = 1 continue + if in_epilogue: - failing = _MOCHA_FAILING_RE.match(line) - if failing: - # The epilogue repeats exactly this many failures, numbered - # from 1 at indent 2, each above its stack trace. - failures_left = int(failing.group("count")) + if not in_failure_body: + if _MOCHA_PENDING_RE.match(line): + continue + failing = _MOCHA_FAILING_RE.match(line) + if failing: + # The epilogue repeats exactly this many failures, numbered + # from 1 at indent 2, each above its stack trace. + failures_left = int(failing.group("count")) + next_failure = 1 + continue + if failures_left == 0: + # Summary had no failure blocks; this line starts the next run. + in_epilogue = False + if in_epilogue and failures_left > 0: + # Ignore error/diff/stack lines until the next expected failure + # header arrives at indent 2. + m = _MOCHA_TEST_RE.match(line) + if m and indent == 2 and m.group("failed") == f"{next_failure})": + failures_left -= 1 + next_failure += 1 + in_failure_body = True + suites = [] continue + if in_epilogue and in_failure_body: + # Inside the final failure block's body. Only a spec-reporter + # suite chain (even indents stepping by 2 from indent 2) or a + # root-level test at indent 2 can begin a subsequent mocha run. + expected_indent = suites[-1][0] + 2 if suites else 2 + m = _MOCHA_TEST_RE.match(line) + if m: + valid_restart = (indent in (2, expected_indent)) and ( + not m.group("failed") or m.group("failed") == "1)" + ) + if valid_restart: + in_epilogue = False + in_failure_body = False + else: + suites = [] + continue + else: + if indent >= 2 and indent % 2 == 0 and indent <= expected_indent: + suites = [(i, t) for i, t in suites if i < indent] + suites.append((indent, stripped)) + else: + suites = [] + continue m = _MOCHA_TEST_RE.match(line) if m: - if in_epilogue: - if m.group("failed") and indent == 2 and failures_left > 0: - failures_left -= 1 - continue - # Anything else is the first result of the next mocha run. - in_epilogue = False status: TestStatus = ( "PASSED" if m.group("passed") else ("FAILED" if m.group("failed") else "SKIPPED") ) diff --git a/src/repo2rlenv/pipelines/_pr_runtime_verifier.py b/src/repo2rlenv/pipelines/_pr_runtime_verifier.py index 7eb12e01..b453a640 100644 --- a/src/repo2rlenv/pipelines/_pr_runtime_verifier.py +++ b/src/repo2rlenv/pipelines/_pr_runtime_verifier.py @@ -556,6 +556,7 @@ def parse_cargo_nextest(log: str) -> dict[str, str]: # use it on Windows), so a √-only log is recognized by mocha's epilogue. _MOCHA_MARKER_RE = re.compile(r"^\s*(?:\d+ passing \(|✔ )", re.MULTILINE) _MOCHA_EPILOGUE_RE = re.compile(r"^\s*\d+ passing \(") +_MOCHA_PENDING_RE = re.compile(r"^\s*\d+ pending\b") _MOCHA_FAILING_RE = re.compile(r"^\s*(?P\d+) failing\b") _MOCHA_TEST_RE = re.compile( r"^(?P\s*)(?:(?P[✔✓√])|(?P\d+\))|(?P-))\s" @@ -569,28 +570,63 @@ def _parse_mocha(log: str) -> dict[str, str]: out: dict[str, str] = {} suites: list[tuple[int, str]] = [] in_epilogue = False + in_failure_body = False failures_left = 0 + next_failure = 1 for raw in log.split("\n"): line = raw.rstrip() stripped = line.lstrip() if not stripped: continue indent = len(line) - len(stripped) - if _MOCHA_EPILOGUE_RE.match(line): - in_epilogue, failures_left = True, 0 + if not (in_epilogue and failures_left > 0) and _MOCHA_EPILOGUE_RE.match(line): + suites = [] + in_epilogue = True + in_failure_body = False + failures_left = 0 + next_failure = 1 continue if in_epilogue: - failing = _MOCHA_FAILING_RE.match(line) - if failing: - failures_left = int(failing.group("count")) + if not in_failure_body: + if _MOCHA_PENDING_RE.match(line): + continue + failing = _MOCHA_FAILING_RE.match(line) + if failing: + failures_left = int(failing.group("count")) + next_failure = 1 + continue + if failures_left == 0: + in_epilogue = False + if in_epilogue and failures_left > 0: + m = _MOCHA_TEST_RE.match(line) + if m and indent == 2 and m.group("failed") == f"{next_failure})": + failures_left -= 1 + next_failure += 1 + in_failure_body = True + suites = [] continue + if in_epilogue and in_failure_body: + expected_indent = suites[-1][0] + 2 if suites else 2 + m = _MOCHA_TEST_RE.match(line) + if m: + valid_restart = (indent in (2, expected_indent)) and ( + not m.group("failed") or m.group("failed") == "1)" + ) + if valid_restart: + in_epilogue = False + in_failure_body = False + else: + suites = [] + continue + else: + if indent >= 2 and indent % 2 == 0 and indent <= expected_indent: + suites = [(i, t) for i, t in suites if i < indent] + suites.append((indent, stripped)) + else: + suites = [] + continue m = _MOCHA_TEST_RE.match(line) if m: - if in_epilogue: - if m.group("failed") and indent == 2 and failures_left > 0: - failures_left -= 1 - continue - in_epilogue = False status = PASSED if m.group("passed") else FAILED if m.group("failed") else SKIPPED describes = [title for ind, title in suites if ind < indent] _record_worst(out, " > ".join([*describes, m.group("name")]), status) diff --git a/tests/test_mocha_output.py b/tests/test_mocha_output.py index bc28a6bb..9be52abc 100644 --- a/tests/test_mocha_output.py +++ b/tests/test_mocha_output.py @@ -446,3 +446,154 @@ def test_mocha_windows_check_mark_is_read_as_mocha(parser): def test_jest_log_without_glyphs_is_untouched(parser): log = "FAIL b.test.js\n Suite\n \u2713 ok (1 ms)\n" assert parser(log) == {"b.test.js > Suite > ok": "PASSED"} + + +def test_epilogue_dash_and_numbered_lines_do_not_exit_epilogue(parser): + log = ( + " Parser\n" + " \u2714 passes\n" + " 1) first failure\n" + " 2) second failure\n" + "\n" + " 1 passing (8ms)\n" + " 2 failing\n" + "\n" + " 1) Parser\n" + " first failure:\n" + " AssertionError: validation failed:\n" + " - missing field\n" + " - bad type\n" + " 1) numbered detail\n" + " \u2714 quoted check\n" + " at Context. (test/parser.spec.js:4:12)\n" + "\n" + " 2) Parser\n" + " second failure:\n" + " Error: boom\n" + " - extra detail\n" + " at Context. (test/parser.spec.js:9:12)\n" + ) + assert parser(log) == { + "Parser > passes": "PASSED", + "Parser > first failure": "FAILED", + "Parser > second failure": "FAILED", + } + + +def test_final_failure_block_diff_lines_are_ignored(parser): + log = ( + " Parser\n" + " \u2714 passes\n" + " 1) fails with diff\n" + "\n" + " 1 passing (5ms)\n" + " 1 failing\n" + "\n" + " 1) Parser\n" + " fails with diff:\n" + "\n" + " AssertionError: expected strings to match\n" + " + expected - actual\n" + "\n" + " - actual line\n" + " + expected line\n" + " 2) stray numbered item\n" + "\n" + " at Context. (test/parser.spec.js:12:5)\n" + ) + assert parser(log) == { + "Parser > passes": "PASSED", + "Parser > fails with diff": "FAILED", + } + + +def test_epilogue_diff_and_pending_summary_do_not_leak_suites(parser): + deep_equal_then_root = ( + " API\n" + " 1) fails hard\n" + "\n" + " 0 passing (5ms)\n" + " 1 failing\n" + "\n" + " 1) API\n" + " fails hard:\n" + " AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:\n" + " {\n" + " nested: {\n" + "+ a: 1\n" + "- a: 2\n" + " }\n" + " }\n" + " at Context. (test/api.spec.js:5:10)\n" + "\n" + "\n" + " \u2714 root test in second run\n" + " API2\n" + " \u2714 works\n" + "\n" + " 2 passing (2ms)\n" + ) + assert parser(deep_equal_then_root) == { + "API > fails hard": "FAILED", + "root test in second run": "PASSED", + "API2 > works": "PASSED", + } + + pending_then_second_run = ( + " Math\n" + " \u2714 adds\n" + " - skipped\n" + "\n" + " 1 passing (4ms)\n" + " 1 pending\n" + "\n" + " \u2714 root after pending\n" + " API\n" + " \u2714 handles request\n" + "\n" + " 2 passing (2ms)\n" + ) + assert parser(pending_then_second_run) == { + "Math > adds": "PASSED", + "Math > skipped": "SKIPPED", + "root after pending": "PASSED", + "API > handles request": "PASSED", + } + + +def test_second_run_nested_suite_after_epilogue_diffs(parser): + log = ( + " Parser\n" + " 1) first\n" + " 2) second\n" + "\n" + " 0 passing (6ms)\n" + " 2 failing\n" + "\n" + " 1) Parser\n" + " first:\n" + " AssertionError: bad\n" + " - first diff\n" + " at Context. (test/p.spec.js:4:5)\n" + "\n" + " 2) Parser\n" + " second:\n" + " AssertionError: worse\n" + " - second diff\n" + " at Context. (test/p.spec.js:8:5)\n" + "\n" + "\n" + " API\n" + " GET /items\n" + " 1) rejects invalid\n" + " \u2714 accepts valid\n" + "\n" + " 1 passing (3ms)\n" + " 1 failing\n" + ) + assert parser(log) == { + "Parser > first": "FAILED", + "Parser > second": "FAILED", + "API > GET /items > rejects invalid": "FAILED", + "API > GET /items > accepts valid": "PASSED", + }