Skip to content

feat(mvn): recover test failures from Surefire/Failsafe XML reports - #2939

Closed
mariuszs wants to merge 2 commits into
rtk-ai:developfrom
mariuszs:feat/mvn-surefire-xml-recovery
Closed

mariuszs wants to merge 2 commits into
rtk-ai:developfrom
mariuszs:feat/mvn-surefire-xml-recovery

Conversation

@mariuszs

@mariuszs mariuszs commented Jul 10, 2026 •

Copy link
Copy Markdown

Problem

Maven's stdout is not always a complete record of what failed. A forked-VM crash, parallel forks interleaving output, -q, <redirectTestOutputToFile>, or -Dmaven.test.failure.ignore=true can hide per-test detail — while target/surefire-reports/TEST-*.xml still carries it authoritatively. No stdout filter, however good, can recover what stdout never printed. Today the agent's only options are re-running the build or reading the XML files by hand.

What this does

After the stdout filter runs (Test / Package / quiet routes), a post-filter pass parses the fresh target/surefire-reports/ and target/failsafe-reports/ XML and compares the reported failures against the filtered text:

  • Failures already visible in stdout are left alone — when stdout tells the whole story (the overwhelmingly common case), output is unchanged byte for byte.
  • Only the failures stdout missed are appended, in [ERROR]-prefixed native style: Class.method <<< FAILURE!, then the exception header and the stack trace verbatim (head-capped at 30 lines).
  • When stdout carried no Tests run: aggregate at all (blind-stdout case), the aggregate line is restored from the XML.

Safety rails:

  • mtime freshness gate — reports older than the run's start time are ignored, so stale files from a previous run can never leak in.
  • Reactor-aware — discovers report dirs at the root and depth-1 modules (<module>/target/{surefire,failsafe}-reports).
  • Capped — at most CAP_WARNINGS appended blocks (… +N more recovered failures tail), stack traces head-capped.
  • Never worse — the block is appended only while the result still fits under the raw size, so the existing never_worse guard is never tripped by it. When the raw log is too small to fit the block (typically -q), the filtered output is left exactly as today.

Example — blind stdout (-q or a forked-VM crash), two failures only the XML knows about:

[INFO] BUILD FAILURE

[ERROR] 2 test failure(s) found in Surefire/Failsafe XML reports but missing from build output:
[ERROR]   com.example.FailingTest.shouldReturnUser <<< FAILURE!
[ERROR]     org.opentest4j.AssertionFailedError: expected:<200> but was:<404>
[ERROR]     	at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150)
[ERROR]     	at com.example.FailingTest.shouldReturnUser(FailingTest.java:42)
[ERROR]     	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
[ERROR]   com.example.FailingTest.shouldHandleNull <<< FAILURE!
[ERROR]     java.lang.AssertionError: Unexpected exception: NullPointerException
[ERROR]     	at com.example.FailingTest.shouldHandleNull(FailingTest.java:55)
[ERROR]     	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
[ERROR] Tests run: 4, Failures: 2, Errors: 0, Skipped: 0 (from XML reports)

Implementation

Two modules; the existing filters are untouched — only the three routing arms in run_tool() go through a small run_with_recovery wrapper that appends the pass after the stdout filter:

Module Role
surefire_reports.rs quick-xml streaming parser for TEST-*.xml (counts, failures, mtime gate)
surefire_recovery.rs diff XML failures vs filtered text, render the missing ones

No new dependencies — quick-xml and tempfile are already in Cargo.toml.

Scope note (rev 2): this revision is deliberately minimal. The earlier stack-trace collapsing and application-package autodetection (stack_trace.rs, pom_groupid.rs, ~900 lines) are gone — traces are appended verbatim with a line cap instead — and captured system-out/system-err recovery was dropped too. Both are possible follow-ups once the core mechanism is in. Rebased onto current develop (mvnd support).

Validation

  • 22 new unit tests (cargo test 'cmds::jvm::surefire_re'); cargo test --all shows no new failures against the develop baseline.
  • cargo clippy --all-targets zero warnings; new modules rustfmt-clean.
  • End-to-end smoke through the binary with a stub ./mvnw that writes a fresh TEST-*.xml and prints a forked-VM-crash log: the two XML-only failures are appended after the filtered log (tee hint intact, exit code propagated); the same run with stdout covering the failures is unchanged; under -q (raw too small for the block) the filtered output is left as is.

Output cost: zero on every run where stdout already covers the failures; on recovery it adds only the missing failure blocks, capped as above.

Maven stdout is not always a complete record of what failed: a forked-VM
crash, parallel forks interleaving output, -q, redirected test output or
-Dmaven.test.failure.ignore=true can hide per-test detail that
target/surefire-reports/TEST-*.xml still carries.

After the stdout filter runs (Test / Package / quiet routes), compare the
XML-reported failures against the filtered text and append only the ones
stdout missed, in [ERROR]-prefixed native style: exception header plus the
stack trace verbatim (head-capped), at most CAP_WARNINGS blocks. Restore
the 'Tests run:' aggregate from XML when stdout had none. When stdout
already covers every XML failure - the common case - output is unchanged
byte for byte. Reports older than the run's start are ignored, and the
block is appended only while the result still fits under the raw size, so
the runner's never-worse guard is never tripped by it.

Two modules: surefire_reports (quick-xml streaming parser) and
surefire_recovery (diff + render). No new dependencies.
@mariuszs
mariuszs force-pushed the feat/mvn-surefire-xml-recovery branch from 3464c8f to 707668b Compare September 3, 2026 16:24
romualdbassinot-efficy added a commit to romualdbassinot-efficy/rtk that referenced this pull request Sep 20, 2026
Review comment 4058013628 (maven_cmd.rs:1).

The module doc comment asserted "with Surefire XML parser". There is no
XML handling in the module: nothing reads target/surefire-reports, and
every parser here is a regex over Maven's stdout. @mariuszs made the same
point on the PR on 2026-09-10.

The doc comment now says what the code does - parses Surefire's stdout
summary - and names the shapes it therefore cannot recover: a forked-VM
crash, -q, redirectTestOutputToFile. It points at rtk-ai#2939, where
an XML-backed pass over the report directory is being designed on top of
develop's mvn_cmd.rs.

The other two copies of the claim:
  - the CHANGELOG bullet went away with ec3f93d, which reverted the
    hand-written section wholesale
  - the PR body is corrected separately, since it is not a file

The commit message of 414b48d still reads "Surefire parser", which is
defensible on its own terms - it does parse Surefire output, just not the
XML - and changing it would need a force-push, which is out of bounds
for this round.

Verified: cargo test --quiet jvm -- --skip snapshot -> 40 passed, 0 failed

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ures

Three ways the recovery pass could append failures that were not missing:

- The stdout filter caps its failure list (`… +N more failures`); the
  elided tests matched nothing in the text and were re-rendered as
  "missing from build output". Recovery now stays out when a cap tail
  is present.
- Coverage was a plain substring match, so `FooTest.shouldSave:42`
  covered a missing `shouldSaveAll`, and `com.a.FooTest.m` covered a
  missing `com.b.FooTest.m`. Names must now stand whole: no identifier
  character after, no identifier character or package dot before.
- Reports were accepted on `mtime >= start` from every module, so a
  concurrent `-pl other-module` build leaked its failures in. The mtime
  window now closes at the child's exit, and when stdout announced its
  classes (`[INFO] Running …`) only those suites are read.
@mariuszs

Copy link
Copy Markdown
Author

Closing this. Following the discussion in #782, I re-tested the motivating cases against a real project (Maven 3.9.16, Surefire 3.5.3, JUnit 5.12), running each through develop's binary and this branch's:

  • -Dmaven.test.failure.ignore=true, -q, and both combined
  • -Dsurefire.printSummary=false, with and without failure.ignore
  • redirectTestOutputToFile
  • a forked-VM System.exit crash, before and after the failing class
  • parallel forks (forkCount=4; forkCount=2 with reuseForks=false), including a fork crashing while the others are still running
  • JUnit in-JVM parallel classes, alone and with forkCount=3

In all 13 scenarios develop's stdout filter already names every failing test, and this PR appends nothing. The reason is structural: Surefire writes a test class's XML report at the same moment it prints that class's result block, and a class that crashes the fork writes no XML at all. So the XML never knows about a failure that stdout didn't already show.

Without a reproducible case where the XML carries more than stdout, this is ~1000 lines with nothing to recover.

@mariuszs mariuszs closed this Sep 23, 2026
@mariuszs
mariuszs deleted the feat/mvn-surefire-xml-recovery branch September 23, 2026 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant