|
| 1 | +"""Normalization of raw test-runner output into a stable failure fingerprint. |
| 2 | +
|
| 3 | +The requirement here is *stability*, not semantic perfection: the same underlying failure |
| 4 | +must produce the same fingerprint across attempts, even though the raw output differs in |
| 5 | +paths, line numbers, timings and object addresses between runs. |
| 6 | +
|
| 7 | +Everything in this module is a pure function. Console-clearing escape codes are already |
| 8 | +stripped upstream by ``render_machine.render_utils._sanitize_script_output``; this module |
| 9 | +handles the run-to-run variation that survives that step. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import hashlib |
| 15 | +import re |
| 16 | +from typing import Optional |
| 17 | + |
| 18 | +FINGERPRINT_LENGTH = 12 |
| 19 | +SIGNATURE_MAX_LINES = 5 |
| 20 | +EXCERPT_MAX_CHARS = 1500 |
| 21 | + |
| 22 | +# Applied in order. Earlier patterns must consume the structured tokens (timestamps, |
| 23 | +# UUIDs, hashes, paths) before the generic number scrubber reaches them, otherwise the |
| 24 | +# generic pattern shreds them into unrecognisable fragments. |
| 25 | +_SCRUBBERS: list[tuple[re.Pattern, str]] = [ |
| 26 | + # Residual ANSI escape sequences (colour codes, cursor movement). |
| 27 | + (re.compile(r"\x1b\[[0-9;?]*[a-zA-Z]"), ""), |
| 28 | + # ISO-8601 and common log timestamps. |
| 29 | + ( |
| 30 | + re.compile(r"\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:[.,]\d+)?(?:Z|[+-]\d{2}:?\d{2})?"), |
| 31 | + "<TS>", |
| 32 | + ), |
| 33 | + (re.compile(r"\b\d{2}:\d{2}:\d{2}(?:[.,]\d+)?\b"), "<TS>"), |
| 34 | + (re.compile(r"\b\d{4}-\d{2}-\d{2}\b"), "<DATE>"), |
| 35 | + # UUIDs before hashes, hashes before addresses, all before generic numbers. |
| 36 | + ( |
| 37 | + re.compile(r"\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b"), |
| 38 | + "<UUID>", |
| 39 | + ), |
| 40 | + (re.compile(r"\b0x[0-9a-fA-F]+\b"), "<ADDR>"), |
| 41 | + (re.compile(r"\b[0-9a-f]{7,40}\b"), "<HEX>"), |
| 42 | + # Paths collapse to their basename: the file name carries signal, the directory |
| 43 | + # prefix (build folder, temp dir, CI workspace) changes on every run. This must run |
| 44 | + # before any temp-directory handling, otherwise the prefix rule would swallow the |
| 45 | + # basename along with the prefix. |
| 46 | + (re.compile(r"(?:[A-Za-z]:)?(?:\\[^\s\\:,)\"']+)+\\([^\s\\:,)\"']+)"), r"\1"), |
| 47 | + (re.compile(r"/(?:[^\s/:,)\"']+/)+([^\s/:,)\"']+)"), r"\1"), |
| 48 | + # Randomly named temp files/dirs that survived as a basename (pytest tmpdir and friends). |
| 49 | + (re.compile(r"\btmp[0-9a-z_]{4,}\b", re.IGNORECASE), "<TMP>"), |
| 50 | + # Elapsed times and durations. |
| 51 | + ( |
| 52 | + re.compile(r"\b\d+(?:[.,]\d+)?\s?(?:ms|us|ns|s|sec|secs|seconds|m|min|mins|h|hrs)\b"), |
| 53 | + "<DUR>", |
| 54 | + ), |
| 55 | + # file.py:123:45 style source references. |
| 56 | + (re.compile(r":\d+(?::\d+)?\b"), ":<N>"), |
| 57 | + # Anything numeric that survived the above. |
| 58 | + (re.compile(r"\b\d+(?:[.,]\d+)?\b"), "<N>"), |
| 59 | + # Collapse whitespace runs so indentation changes do not alter the fingerprint. |
| 60 | + (re.compile(r"[ \t]+"), " "), |
| 61 | +] |
| 62 | + |
| 63 | +# Lines matching any of these carry the actual failure. Ordered scanning of these markers |
| 64 | +# is what keeps the signature focused on the error rather than on runner chatter. |
| 65 | +_FAILURE_MARKERS: list[re.Pattern] = [ |
| 66 | + re.compile(r"\bassert(?:ion)?\s*(?:error|failed)?\b", re.IGNORECASE), |
| 67 | + re.compile(r"\b\w*(?:Error|Exception)\b"), |
| 68 | + re.compile(r"^\s*E\s{2,}"), # pytest's error continuation lines |
| 69 | + re.compile(r"\bFAILED?\b"), |
| 70 | + re.compile(r"\bFAILURE!?\b", re.IGNORECASE), |
| 71 | + re.compile(r"\bTraceback\b"), |
| 72 | + re.compile(r"^\s*panic:"), # go |
| 73 | + re.compile(r"\bexpected\b.*\b(?:but|actual|got|received)\b", re.IGNORECASE), |
| 74 | + re.compile(r"^\s*×"), # vitest / jest failure bullets |
| 75 | +] |
| 76 | + |
| 77 | + |
| 78 | +def normalize_output(raw_output: str) -> str: |
| 79 | + """Scrub every run-to-run varying token out of test-runner output.""" |
| 80 | + normalized = raw_output |
| 81 | + for pattern, replacement in _SCRUBBERS: |
| 82 | + normalized = pattern.sub(replacement, normalized) |
| 83 | + return normalized |
| 84 | + |
| 85 | + |
| 86 | +def _is_failure_line(line: str) -> bool: |
| 87 | + return any(marker.search(line) for marker in _FAILURE_MARKERS) |
| 88 | + |
| 89 | + |
| 90 | +def extract_signature(normalized_output: str) -> str: |
| 91 | + """Pick the lines that identify the failure. |
| 92 | +
|
| 93 | + Failure-marker lines are preferred. When a runner emits none that we recognise, the |
| 94 | + tail of the output is used instead, since test runners put their summary last. |
| 95 | + """ |
| 96 | + lines = [line.strip() for line in normalized_output.splitlines() if line.strip()] |
| 97 | + if not lines: |
| 98 | + return "" |
| 99 | + |
| 100 | + signature_lines: list[str] = [] |
| 101 | + for line in lines: |
| 102 | + if _is_failure_line(line) and line not in signature_lines: |
| 103 | + signature_lines.append(line) |
| 104 | + if len(signature_lines) == SIGNATURE_MAX_LINES: |
| 105 | + break |
| 106 | + |
| 107 | + if not signature_lines: |
| 108 | + signature_lines = lines[-SIGNATURE_MAX_LINES:] |
| 109 | + |
| 110 | + return "\n".join(signature_lines) |
| 111 | + |
| 112 | + |
| 113 | +def compute_fingerprint(signature: str) -> Optional[str]: |
| 114 | + """Hash a signature into a short stable identifier. Empty signature has no identity.""" |
| 115 | + if not signature: |
| 116 | + return None |
| 117 | + return hashlib.sha256(signature.encode("utf-8")).hexdigest()[:FINGERPRINT_LENGTH] |
| 118 | + |
| 119 | + |
| 120 | +def _build_excerpt(normalized_output: str) -> str: |
| 121 | + """Bound the normalized output, keeping the tail where failures are reported.""" |
| 122 | + excerpt = normalized_output.strip() |
| 123 | + if len(excerpt) <= EXCERPT_MAX_CHARS: |
| 124 | + return excerpt |
| 125 | + return "...\n" + excerpt[-EXCERPT_MAX_CHARS:] |
| 126 | + |
| 127 | + |
| 128 | +def fingerprint_output(raw_output: Optional[str]) -> tuple[Optional[str], str, str]: |
| 129 | + """Normalize test output into ``(fingerprint, signature, excerpt)``. |
| 130 | +
|
| 131 | + A passing or empty run has no failure identity and yields ``(None, "", "")``. |
| 132 | + """ |
| 133 | + if not raw_output or not raw_output.strip(): |
| 134 | + return None, "", "" |
| 135 | + |
| 136 | + normalized = normalize_output(raw_output) |
| 137 | + signature = extract_signature(normalized) |
| 138 | + return compute_fingerprint(signature), signature, _build_excerpt(normalized) |
0 commit comments