Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(distill): preserve failure messages for verbose go test runs (#3596) (#3627)

Rishabh Mishraf0642b37367c
diff --git a/packages/core/src/repowise/core/distill/filters/test_output.py b/packages/core/src/repowise/core/distill/filters/test_output.py
index 75586a9ac..20a0792f3 100644
--- a/packages/core/src/repowise/core/distill/filters/test_output.py
+++ b/packages/core/src/repowise/core/distill/filters/test_output.py
@@ -10,6 +10,7 @@ the pass parade — dots, PASSED lines, ok lines — which is replaced by counts
from __future__ import annotations
import re
+from collections import deque
from typing import ClassVar
from repowise.core.distill.filters.base import OutputFilter, cap_block, is_error_line
@@ -29,8 +30,11 @@ _PYTEST_PROGRESS_FAIL_RE = re.compile(r"\b(FAILED|ERROR)\b")
# go test -------------------------------------------------------------------
_GO_RUN_RE = re.compile(r"^=== (?:RUN|PAUSE|CONT|NAME)\b")
+_GO_RUN_PARSE_RE = re.compile(r"^===\s+(?P<verb>RUN|PAUSE|CONT|NAME)(?:\s+(?P<name>\S+))?")
+_GO_STATUS_PARSE_RE = re.compile(r"^\s*--- (?:FAIL|PASS|SKIP):\s+(?P<name>\S+)")
_GO_PASS_RE = re.compile(r"^\s*--- PASS:|^PASS$|^ok\s+\S+")
_GO_FAIL_RE = re.compile(r"^\s*--- FAIL:|^FAIL\b")
+_GO_MAX_BUFFERED_TEST_LINES = 20
# cargo test ----------------------------------------------------------------
_CARGO_OK_RE = re.compile(r"^test \S+ \.\.\. ok$")
@@ -168,31 +172,157 @@ def _distill_pytest(lines: list[str]) -> str:
# -- go test -----------------------------------------------------------------
+class _TestBuffer:
+ """Bounded per-test buffer holding head and tail lines while preserving errors."""
+
+ def __init__(
+ self,
+ head_lines: int = 10,
+ tail_lines: int = 10,
+ max_middle_errors: int = 20,
+ ) -> None:
+ self.head_lines = head_lines
+ self.tail_lines = tail_lines
+ self.max_middle_errors = max_middle_errors
+ self.head: list[str] = []
+ self.middle_errors: list[str] = []
+ self.tail: deque[str] = deque(maxlen=tail_lines)
+
+ def append(self, line: str) -> None:
+ if len(self.head) < self.head_lines:
+ self.head.append(line)
+ elif is_error_line(line):
+ if len(self.middle_errors) >= self.max_middle_errors:
+ self.middle_errors.pop(0)
+ self.middle_errors.append(line)
+ else:
+ self.tail.append(line)
+
+ def drain(self) -> list[str]:
+ out = list(self.head)
+ out.extend(self.middle_errors)
+ out.extend(self.tail)
+ self.head.clear()
+ self.middle_errors.clear()
+ self.tail.clear()
+ return out
+
+ def drain_errors(self) -> list[str]:
+ errors = [ln for ln in self.head if is_error_line(ln)]
+ errors.extend(self.middle_errors)
+ errors.extend(ln for ln in self.tail if is_error_line(ln))
+ self.head.clear()
+ self.middle_errors.clear()
+ self.tail.clear()
+ return errors
+
+
+class _GoDistiller:
+ """Stateful distiller for `go test` and `go test -v` outputs."""
+
+ def __init__(self, lines: list[str]) -> None:
+ self.lines = lines
+ self.out: list[str] = []
+ self.passes = 0
+ self.runs = 0
+ self.in_fail_block = False
+ self.current_test: str | None = None
+ self.buffers: dict[str, _TestBuffer] = {}
+
+ def _parse_test_name(self, line: str) -> str | None:
+ m = _GO_STATUS_PARSE_RE.match(line)
+ return m.group("name") if m else None
+
+ def _active_parent(self, test_name: str | None) -> str | None:
+ if test_name and "/" in test_name:
+ parent = test_name.rsplit("/", 1)[0]
+ if parent in self.buffers:
+ return parent
+ return None
+
+ def _handle_exit_status(self, line: str) -> None:
+ if self.current_test and self.current_test in self.buffers:
+ self.out.extend(self.buffers.pop(self.current_test).drain())
+ for buf in self.buffers.values():
+ self.out.extend(buf.drain_errors())
+ self.buffers.clear()
+ self.out.append(line)
+ self.current_test = None
+ self.in_fail_block = False
+
+ def _handle_run(self, line: str) -> bool:
+ run_m = _GO_RUN_PARSE_RE.match(line)
+ if not run_m:
+ return False
+ verb = run_m.group("verb")
+ name = run_m.group("name")
+ self.in_fail_block = False
+ if verb == "RUN":
+ self.runs += 1
+ self.current_test = name
+ if name:
+ self.buffers[name] = _TestBuffer()
+ elif verb in ("CONT", "NAME"):
+ self.current_test = name
+ if name and name not in self.buffers:
+ self.buffers[name] = _TestBuffer()
+ elif verb == "PAUSE":
+ self.current_test = None
+ return True
+
+ def _handle_fail(self, line: str) -> bool:
+ if not _GO_FAIL_RE.match(line):
+ return False
+ is_test_fail = line.lstrip().startswith("--- FAIL")
+ self.in_fail_block = is_test_fail
+ self.out.append(line)
+ test_name = self._parse_test_name(line) if is_test_fail else self.current_test
+ if test_name and test_name in self.buffers:
+ self.out.extend(self.buffers.pop(test_name).drain())
+ self.current_test = self._active_parent(test_name)
+ return True
+
+ def _handle_pass_or_skip(self, line: str) -> bool:
+ if not (_GO_PASS_RE.match(line) or line.lstrip().startswith("--- SKIP")):
+ return False
+ self.in_fail_block = False
+ if line.startswith(("ok", "PASS")):
+ self.out.append(line)
+ self.current_test = None
+ return True
+ if not line.lstrip().startswith("--- SKIP"):
+ self.passes += 1
+ test_name = self._parse_test_name(line)
+ if test_name and test_name in self.buffers:
+ self.out.extend(self.buffers.pop(test_name).drain_errors())
+ self.current_test = self._active_parent(test_name)
+ return True
+
+ def _handle_line(self, line: str) -> None:
+ if line.startswith("exit status "):
+ self._handle_exit_status(line)
+ elif self._handle_run(line) or self._handle_fail(line) or self._handle_pass_or_skip(line):
+ return
+ elif self.current_test is not None:
+ self.buffers[self.current_test].append(line)
+ elif self.in_fail_block or is_error_line(line):
+ self.out.append(line)
+
+ def distill(self) -> str:
+ for line in self.lines:
+ self._handle_line(line)
+ for buf in self.buffers.values():
+ self.out.extend(buf.drain_errors())
+
+ if not self.out and self.passes == 0 and self.runs == 0:
+ raise ValueError("not go test output")
+ if self.passes or self.runs:
+ self.out.insert(0, f"({self.passes or self.runs} passing tests omitted)")
+ return "\n".join(self.out)
+
+
def _distill_go(lines: list[str]) -> str:
- out: list[str] = []
- passes = 0
- runs = 0
- in_fail_block = False
- for line in lines:
- if _GO_RUN_RE.match(line):
- runs += 1
- in_fail_block = False
- elif _GO_FAIL_RE.match(line):
- in_fail_block = line.lstrip().startswith("--- FAIL")
- out.append(line)
- elif _GO_PASS_RE.match(line):
- in_fail_block = False
- if line.startswith(("ok", "PASS")):
- out.append(line)
- else:
- passes += 1
- elif line.startswith("exit status ") or in_fail_block or is_error_line(line):
- out.append(line)
- if not out and passes == 0 and runs == 0:
- raise ValueError("not go test output")
- if passes or runs:
- out.insert(0, f"({passes or runs} passing tests omitted)")
- return "\n".join(out)
+ return _GoDistiller(lines).distill()
# -- cargo test --------------------------------------------------------------
diff --git a/tests/fixtures/distill/go_test_verbose_fail.txt b/tests/fixtures/distill/go_test_verbose_fail.txt
new file mode 100644
index 000000000..16877f113
--- /dev/null
+++ b/tests/fixtures/distill/go_test_verbose_fail.txt
@@ -0,0 +1,37 @@
+=== RUN TestPick
+ pick_test.go:10: unexpected value: got "alpha" want "beta"
+--- FAIL: TestPick (0.00s)
+=== RUN TestSub
+=== RUN TestSub/upper
+ pick_test.go:16: subtest broke: 3
+--- FAIL: TestSub (0.00s)
+ --- FAIL: TestSub/upper (0.00s)
+=== RUN TestMany
+=== RUN TestMany/case0
+--- PASS: TestMany/case0 (0.00s)
+=== RUN TestMany/case1
+--- PASS: TestMany/case1 (0.00s)
+=== RUN TestMany/case2
+--- PASS: TestMany/case2 (0.00s)
+=== RUN TestMany/case3
+--- PASS: TestMany/case3 (0.00s)
+=== RUN TestMany/case4
+--- PASS: TestMany/case4 (0.00s)
+=== RUN TestMany/case5
+--- PASS: TestMany/case5 (0.00s)
+=== RUN TestMany/case6
+--- PASS: TestMany/case6 (0.00s)
+=== RUN TestMany/case7
+--- PASS: TestMany/case7 (0.00s)
+=== RUN TestMany/case8
+--- PASS: TestMany/case8 (0.00s)
+=== RUN TestMany/case9
+--- PASS: TestMany/case9 (0.00s)
+=== RUN TestMany/case10
+--- PASS: TestMany/case10 (0.00s)
+=== RUN TestMany/case11
+--- PASS: TestMany/case11 (0.00s)
+--- PASS: TestMany (0.00s)
+FAIL
+FAIL example.com/gofail 0.322s
+FAIL
diff --git a/tests/unit/distill/test_filters.py b/tests/unit/distill/test_filters.py
index 4c317b9d7..2200e0f4b 100644
--- a/tests/unit/distill/test_filters.py
+++ b/tests/unit/distill/test_filters.py
@@ -110,6 +110,188 @@ def test_go_panic_traceback_survives(load_fixture) -> None:
assert "TestParserPython3/basic" not in distilled
+def test_go_verbose_failure_messages_survive(load_fixture) -> None:
+ _raw, distilled = _distill(
+ load_fixture, "go_test_verbose_fail.txt", "test_output", "go test -v ./..."
+ )
+ assert "(13 passing tests omitted)" in distilled
+ assert "--- FAIL: TestPick" in distilled
+ assert "pick_test.go:10: unexpected value" in distilled
+ pick_fail_idx = distilled.index("--- FAIL: TestPick")
+ pick_msg_idx = distilled.index("pick_test.go:10: unexpected value")
+ assert pick_fail_idx < pick_msg_idx
+
+ assert "--- FAIL: TestSub/upper" in distilled
+ assert "pick_test.go:16: subtest broke: 3" in distilled
+ sub_fail_idx = distilled.index("--- FAIL: TestSub/upper")
+ sub_msg_idx = distilled.index("pick_test.go:16: subtest broke: 3")
+ assert sub_fail_idx < sub_msg_idx
+
+ assert "TestMany/case" not in distilled
+ assert "FAIL" in distilled
+
+
+def test_go_parallel_interleaved_distill() -> None:
+ raw = "\n".join(
+ [
+ "=== RUN TestA",
+ "=== PAUSE TestA",
+ "=== RUN TestB",
+ "=== PAUSE TestB",
+ "=== CONT TestA",
+ "=== CONT TestB",
+ " test_b.go:42: info message from passing TestB",
+ "--- PASS: TestB (0.01s)",
+ " test_a.go:10: failure reason for TestA",
+ "--- FAIL: TestA (0.02s)",
+ "FAIL",
+ ]
+ )
+ f = filter_registry.get("test_output")
+ distilled = f.distill(raw, command="go test -v ./...")
+ assert "--- FAIL: TestA (0.02s)" in distilled
+ assert "test_a.go:10: failure reason for TestA" in distilled
+ assert "test_b.go:42: info message from passing TestB" not in distilled
+ assert "(1 passing tests omitted)" in distilled
+
+
+def test_go_chatty_test_buffer_capped_to_20() -> None:
+ lines = ["=== RUN TestChattyFail"]
+ for i in range(35):
+ lines.append(f" chatty_test.go:{i}: log output line {i}")
+ lines.extend(
+ [
+ "--- FAIL: TestChattyFail (0.05s)",
+ "FAIL",
+ ]
+ )
+ raw = "\n".join(lines)
+ f = filter_registry.get("test_output")
+ distilled = f.distill(raw, command="go test -v ./...")
+ assert "--- FAIL: TestChattyFail (0.05s)" in distilled
+ assert "log output line 0" in distilled
+ assert "log output line 9" in distilled
+ assert "log output line 14" not in distilled
+ assert "log output line 25" in distilled
+ assert "log output line 34" in distilled
+
+
+def test_go_passing_test_preserves_error_lines() -> None:
+ raw = "\n".join(
+ [
+ "=== RUN TestPassingWithError",
+ " pass_test.go:10: warning: failed connection attempt 1, retrying",
+ " pass_test.go:11: connection established successfully",
+ "--- PASS: TestPassingWithError (0.01s)",
+ "PASS",
+ ]
+ )
+ f = filter_registry.get("test_output")
+ distilled = f.distill(raw, command="go test -v ./...")
+ assert "failed connection attempt 1" in distilled
+ assert "connection established successfully" not in distilled
+ assert "(1 passing tests omitted)" in distilled
+
+
+def test_go_crash_diagnostics_survive_exit_status() -> None:
+ raw = "\n".join(
+ [
+ "=== RUN TestCrash",
+ "panic: runtime error: index out of range",
+ "goroutine 1 [running]:",
+ "repowise/internal/crash.TestCrash(0x0)",
+ "exit status 2",
+ "FAIL\texample.com/crash\t0.01s",
+ ]
+ )
+ f = filter_registry.get("test_output")
+ distilled = f.distill(raw, command="go test -v ./...")
+ assert "panic: runtime error: index out of range" in distilled
+ assert "goroutine 1 [running]:" in distilled
+ assert "exit status 2" in distilled
+
+
+def test_go_failure_message_not_evicted_by_many_logs() -> None:
+ lines = [
+ "=== RUN TestEviction",
+ " fail_test.go:5: assertion error: expected 1 got 2",
+ ]
+ for i in range(30):
+ lines.append(f" fail_test.go:{10 + i}: debug message {i}")
+ lines.extend(
+ [
+ "--- FAIL: TestEviction (0.05s)",
+ "FAIL",
+ ]
+ )
+ raw = "\n".join(lines)
+ f = filter_registry.get("test_output")
+ distilled = f.distill(raw, command="go test -v ./...")
+ assert "assertion error: expected 1 got 2" in distilled
+ assert "debug message 29" in distilled
+ assert "debug message 15" not in distilled
+
+
+def test_go_parent_diagnostics_survive_subtests() -> None:
+ raw = "\n".join(
+ [
+ "=== RUN TestParent",
+ "=== RUN TestParent/sub",
+ "--- PASS: TestParent/sub (0.01s)",
+ " parent_test.go:25: got 1, want 2",
+ "--- FAIL: TestParent (0.02s)",
+ "FAIL",
+ ]
+ )
+ f = filter_registry.get("test_output")
+ distilled = f.distill(raw, command="go test -v ./...")
+ assert "--- FAIL: TestParent (0.02s)" in distilled
+ assert "parent_test.go:25: got 1, want 2" in distilled
+
+
+def test_go_error_heavy_test_strictly_bounded_to_20() -> None:
+ lines = ["=== RUN TestManyErrors"]
+ for i in range(35):
+ lines.append(f" fail_test.go:{i}: error message {i}")
+ lines.extend(
+ [
+ "--- FAIL: TestManyErrors (0.05s)",
+ "FAIL",
+ ]
+ )
+ raw = "\n".join(lines)
+ f = filter_registry.get("test_output")
+ distilled = f.distill(raw, command="go test -v ./...")
+ assert "--- FAIL: TestManyErrors (0.05s)" in distilled
+ assert "error message 0" in distilled
+ assert "error message 9" in distilled
+ assert "error message 14" not in distilled
+ assert "error message 15" in distilled
+ assert "error message 34" in distilled
+
+
+def test_go_failure_diagnostic_survives_many_subsequent_logs() -> None:
+ lines = [
+ "=== RUN TestPick",
+ " pick_test.go:10: got \"alpha\" want \"beta\"",
+ ]
+ for i in range(30):
+ lines.append(f" pick_test.go:{20 + i}: debug output line {i}")
+ lines.extend(
+ [
+ "--- FAIL: TestPick (0.05s)",
+ "FAIL",
+ ]
+ )
+ raw = "\n".join(lines)
+ f = filter_registry.get("test_output")
+ distilled = f.distill(raw, command="go test -v ./...")
+ assert "--- FAIL: TestPick (0.05s)" in distilled
+ assert "got \"alpha\" want \"beta\"" in distilled
+ assert "debug output line 29" in distilled
+ assert "debug output line 15" not in distilled
+
+
def test_cargo_failures_section_survives(load_fixture) -> None:
_raw, distilled = _distill(load_fixture, "cargo_test_fail.txt", "test_output", "cargo test")
assert "store::tests::test_ttl_prune" in distilled