Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog.d/3603-a-gate-that-exits-non-zero-without-a-rec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
class: fixed
trace: #3603
---
A gate that exits non-zero without a recognised pass/fail summary is now reported with its exit code, and failure_excerpt carries the command's own last lines instead of a "NO VERDICT FOUND ... may be unrelated" note.
49 changes: 44 additions & 5 deletions specfuse/loop/loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2760,8 +2760,24 @@ def extract_failure_excerpt(stdout: str, max_chars: int = 500) -> str:
# The report echoes the gate's own command as `$ <command>`; a command
# that mentions FAIL in its text (a surefire grep fallback, say) would
# otherwise be the excerpt's head and eat half its budget (#3293).
relevant = [ln for ln in stdout.splitlines()
if _KW.search(ln) and not ln.lstrip().startswith("$ ")]
lines = stdout.splitlines()
keep: set[int] = set()
for i, ln in enumerate(lines):
if _KW.search(ln) and not ln.lstrip().startswith("$ "):
keep.add(i)
# A gate that exited non-zero with no recognisable summary (#3603):
# its own last lines, which `select_gate_report_lines` put just above
# this note, are the failure reason even when no keyword matches them
# (an oracle's `RED: ...` lines). Walk back to the command echo.
if _NONZERO_EXIT_NOTE_RE.match(ln.strip()):
j = i - 1
while j >= 0:
prev = lines[j].strip()
if prev.startswith(("$ ", "### ")) or _FENCE_RE.match(prev):
break
keep.add(j)
j -= 1
relevant = [lines[i] for i in sorted(keep)]
# Hoist surefire failing-test lines to the front (#207): Maven emits
# dozens of boilerplate [ERROR] wrapper lines that all pass the keyword
# filter, so the head+tail budget reliably crowded out the actual
Expand Down Expand Up @@ -6600,8 +6616,26 @@ def resolve_bash() -> str | None:
"only, and may be unrelated to the failure. Run the command directly."
)

#: Note for a command that exited non-zero with no recognisable summary
#: (#3603). The exit code is the verdict, and the lines above it are the
#: command's own last lines, so they are the failure reason, not noise. Worded
#: without any `_FAILURE_KEYWORD_RE` keyword so the failure signature still
#: keys off the command's own output, exactly as before.
_NONZERO_EXIT_NOTE_PREFIX = "exit code "
_NONZERO_EXIT_NOTE_RE = re.compile(r"^exit code -?\d+: ")


def _nonzero_exit_note(returncode: int) -> str:
return (
f"{_NONZERO_EXIT_NOTE_PREFIX}{returncode}: the command exited non-zero "
"without a recognisable pass/fail summary — the lines above are the last "
"lines of its own output."
)


def select_gate_report_lines(out: "str | None", window: int = 15) -> list[str]:
def select_gate_report_lines(
out: "str | None", window: int = 15, returncode: "int | None" = None,
) -> list[str]:
"""Return the lines to show in a gate's report — verdict first, tail always.

A positional tail is not enough (FEAT-2026-0068). `unittest` writes its
Expand All @@ -6616,7 +6650,9 @@ def select_gate_report_lines(out: "str | None", window: int = 15) -> list[str]:
(see `_VERDICT_RE`) found *before* the tail, with an explicit elision marker
between the two so nothing is dropped silently. When no verdict is found
anywhere, say so rather than presenting unrelated trailing output as though
it were the failure.
it were the failure — unless *returncode* is non-zero (#3603): then the exit
code is the verdict and the tail is the command's own account of it, so
the note names the exit code instead.
"""
lines = (out or "").strip().splitlines()
if not lines:
Expand Down Expand Up @@ -6649,6 +6685,8 @@ def select_gate_report_lines(out: "str | None", window: int = 15) -> list[str]:
tail_verdict_re = _MAVEN_VERDICT_RE if maven else _VERDICT_RE
if any(tail_verdict_re.search(ln.strip()) for ln in tail):
return tail
if returncode:
return tail + [_nonzero_exit_note(returncode)]
return tail + [_NO_VERDICT_NOTE]


Expand Down Expand Up @@ -6894,7 +6932,8 @@ def _run_gate_set(
ok = proc.returncode == 0
if _capture_returncodes is not None:
_capture_returncodes[gate["name"]] = proc.returncode
tail = select_gate_report_lines(out, window=15)
tail = select_gate_report_lines(
out, window=15, returncode=proc.returncode)
# A green gate whose oracle silently degraded is a hollow pass
# (issue #134): force FAIL and name the degradation honestly so the
# log reads as "oracle couldn't measure it," not "code is clean."
Expand Down
103 changes: 103 additions & 0 deletions tests/test_nonzero_exit_no_verdict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Copyright 2026 Specfuse Contributors
# Licensed under the Apache License, Version 2.0. See LICENSE.
"""Issue #3603 — a gate that exits non-zero without a recognised summary.

A `feature_oracle` script printed `RED: <file> lacks: <text>` lines and exited
1. Its report ended in the NO VERDICT note ("may be unrelated to the failure.
Run the command directly."), and `failure_excerpt` carried that note and the
`### feature_oracle: FAIL` header but none of the oracle's own lines: the
excerpt keeps keyword lines only, `RED:` matches none of the keywords, and the
note does ("pass/fail"). The signature, meanwhile, was the first `RED:` line.

A non-zero exit IS the verdict. The report must say so with the exit code, the
excerpt must carry the command's own last lines, and the no-verdict wording is
kept for a command that exited zero with nothing recognisable. Pass/fail,
failure_class and failure_signature are unchanged.
"""
from __future__ import annotations

import os
import tempfile
import unittest
from pathlib import Path

from tests._loop_loader import load_loop

loop = load_loop()

_RED_ORACLE = (
"echo 'checking 3 files'; "
"echo 'RED: docs/guide.md lacks: the new flag'; "
"echo 'RED: README.md lacks: the install line'; "
"exit 1"
)


class _InTempDir(unittest.TestCase):
def setUp(self):
self._cwd = os.getcwd()
self._tmp = tempfile.TemporaryDirectory()
os.chdir(self._tmp.name)
self.feature_dir = Path("feat")
self.feature_dir.mkdir()

def tearDown(self):
os.chdir(self._cwd)
self._tmp.cleanup()

def _payload(self, command: str, name: str = "feature_oracle") -> tuple[bool, str]:
results = loop._run_gate_set([{"name": name, "command": command}], self.feature_dir)
return results[0]["ok"], "\n\n".join(r["report"] for r in results)


class TestNonZeroExitWithoutSummary(_InTempDir):
def test_report_names_the_exit_code_not_no_verdict(self):
ok, payload = self._payload(_RED_ORACLE)
self.assertFalse(ok)
self.assertNotIn("NO VERDICT FOUND", payload)
self.assertNotIn("may be unrelated to the failure", payload)
self.assertIn("exit code 1", payload)

def test_excerpt_carries_the_commands_own_last_lines(self):
_, payload = self._payload(_RED_ORACLE)
excerpt = loop.extract_failure_excerpt(payload)
self.assertIn("RED: docs/guide.md lacks: the new flag", excerpt)
self.assertIn("RED: README.md lacks: the install line", excerpt)
self.assertIn("exit code 1", excerpt)
self.assertNotIn("NO VERDICT FOUND", excerpt)
self.assertNotIn("$ echo", excerpt, "the command echo is not the command's output")

def test_class_and_signature_are_unchanged(self):
_, payload = self._payload(_RED_ORACLE)
self.assertEqual(
loop.parse_gate_failure_signature(payload),
("other", "checking 3 files"),
)

def test_other_exit_codes_are_named(self):
_, payload = self._payload("echo 'RED: x'; exit 3")
self.assertIn("exit code 3", payload)
self.assertIn("exit code 3", loop.extract_failure_excerpt(payload))


class TestZeroExitWithoutSummaryKeepsTheNote(_InTempDir):
def test_a_passing_command_with_nothing_recognisable_still_reads_no_verdict(self):
ok, payload = self._payload("echo 'abc0001 subject'; exit 0", name="recent-commits")
self.assertTrue(ok)
self.assertIn(loop._NO_VERDICT_NOTE, payload)

def test_selector_without_a_return_code_is_unchanged(self):
# prerun_capture and older callers pass no return code.
lines = loop.select_gate_report_lines("plain line\n", window=15)
self.assertEqual(lines, ["plain line", loop._NO_VERDICT_NOTE])


class TestRecognisedSummaryIsUnaffected(_InTempDir):
def test_a_nonzero_exit_with_a_verdict_line_gets_no_exit_note(self):
_, payload = self._payload("echo 'FAILED (failures=1)'; exit 1", name="tests")
self.assertNotIn("exit code 1", payload)
self.assertNotIn("NO VERDICT FOUND", payload)


if __name__ == "__main__":
unittest.main()