Skip to content

feat(mvn): add mvn test filter with broadened goal matching - #1921

Closed
ehsansasanian wants to merge 4 commits into
rtk-ai:developfrom
ehsansasanian:feat/mvn-add-test-filter
Closed

ehsansasanian wants to merge 4 commits into
rtk-ai:developfrom
ehsansasanian:feat/mvn-add-test-filter

Conversation

@ehsansasanian

@ehsansasanian ehsansasanian commented May 16, 2026 •

Copy link
Copy Markdown

Closes #1537.

Summary

Adds Maven Surefire and Failsafe test output filtering — mvn test, mvn verify, mvn integration-test — on top of the existing mvn-build filter, with a corrected rewrite regex, Maven Wrapper support (mvnw, ./mvnw, mvnw.cmd, mvnw.bat), and explicit routing between the two filters via a new match_command_excludes TOML field.

Motivation

LLM agents running mvn test, mvn verify, or mvn integration-test against a real Java project routinely consume thousands of tokens on Surefire/Failsafe boilerplate (download lines, plugin lifecycle headers, the T E S T S banner, per-class passing summaries, JVM agent warnings, framework stack frames, and the [Help 1] / Re-run with -X footer). The signal — which test failed, why, and the user-code stack frame — is a tiny fraction of the output.

What changes

New filter: src/filters/mvn-test.toml

  • Strips Maven decoration (download/build/plugin lifecycle, separators, [INFO] chatter), Surefire/Failsafe noise (the T E S T S banner, per-class Running com.example.AppIT/Running com.example.FooTest lines, passing per-class summaries), JVM agent warnings, framework stack frames (java.*, jdk.internal.*, sun.*, org.junit.*, org.apache.maven.surefire.*, org.testng.*, plus app// JPMS prefixes), and Maven's footer hints ([Help 1], Re-run with -X, Please refer to …).
  • Preserves [ERROR] failure lines (per-class and per-method), user-code stack frames, the Results: block, BUILD SUCCESS/BUILD FAILURE, total time, and the final summary.
  • Skips per-class passing summaries ([INFO] Tests run: N, Failures: 0, Errors: 0, Skipped: N) but keeps [ERROR]-prefixed failing summaries — the prefix asymmetry lets us discard chatter while preserving failure context.
  • Head + tail bracketing (40 + 60 lines) plus on_empty = "mvn test: ok" for fully-stripped passing runs.

Rewrite rule: src/discover/rules.rs

  • Broadens the regex to ^(?:\./mvnw|mvnw\.cmd|mvnw\.bat|mvnw|mvn)(?:\s+\S+)*\s+(compile|package|clean|install|test|verify|integration-test)\b so it accepts the Maven Wrapper, global flags, and multi-goal invocations like:
    • mvn -q -Dtest=UserServiceTest test
    • mvn clean test
    • mvn clean verify -DskipITs=false
    • mvn integration-test
    • ./mvnw test, ./mvnw -q -Dtest=Foo test, mvnw clean verify, mvnw.cmd test, mvnw.bat clean install
  • rewrite_prefixes now lists ./mvnw, mvnw.cmd, mvnw.bat, mvnw, mvn (longest-first so ./mvnw wins before mvnw). All wrapper invocations rewrite to rtk mvn …, so the existing TOML filters match unchanged.
  • Adds ("test", 80.0), ("verify", 80.0), ("integration-test", 80.0) to subcmd_savings.

Engine addition: src/core/toml_filter.rs

  • New optional match_command_excludes field on TomlFilterDef. When set and matches the command, the filter is skipped by find_filter_in. This is a clean negative-match mechanism that substitutes for negative lookahead (unsupported by the regex crate) and is reusable for any future filter-routing conflict.

Routing fix: src/filters/mvn-build.toml

  • match_command reverts to the original simple form ^mvn\s+(?:\S+\s+)*(compile|package|clean|install)\b.
  • match_command_excludes = "(?:^|\s)(?:test|verify|integration-test)(?:\s|$)" ensures mvn clean test, mvn clean verify, and mvn integration-test route to mvn-test, not mvn-build, despite alphabetic filter load order making mvn-build first in line.

Tests

  • 5 inline [[tests.mvn-test]] blocks: Surefire failure preservation, successful build summary, compile errors, fully-stripped → mvn test: ok, Failsafe IT failure block.
  • test_maven_builtin_filter_command_matching in src/core/toml_filter.rs — 14 routing cases verifying every flag/goal combination routes to the correct filter (test, verify, integration-test, mixed clean install integration-test, etc.).
  • test_classify_mvn_test_with_global_options, test_classify_mvn_verify_and_integration_test, and test_rewrite_mvn_test_variants in src/discover/registry.rs.
  • test_classify_mvnw_variants (8 cases), test_classify_mvnw_with_env_prefix (covers MAVEN_OPTS="-Xmx1g" ./mvnw test), and test_rewrite_mvnw_variants (8 cases) — verify the wrapper variants both classify as rtk mvn and rewrite to rtk mvn ….
  • test_mvn_test_fixture_savings_and_failure_preservation — loads tests/fixtures/mvn_test_raw.txt (representative Surefire 3.x output: 23 tests, 2 failures, full stack trace, JVM warnings) and asserts ≥75% token savings and that failure context survives (UserServiceTest.shouldCreateUserWithValidEmail, expected: <201> but was: <500>, Tests run: 23, Failures: 2, BUILD FAILURE).
  • test_mvn_test_filter_crlf_safe — confirms the filter produces identical output for LF and CRLF input.

Token savings

Scenario Savings Notes
Passing run (everything stripped) ~95% Single line via on_empty or just BUILD SUCCESS + total time
Failing run (preserves context) ~77% Asserted ≥75% on the included fixture — failure context is genuinely large

The fixture deliberately models a failing build to set a floor; passing-only fixtures would inflate the number.

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test — all green (1879 tests pass, 0 warnings).
  • Routing verified for: mvn test, mvn -q -Dtest=Foo test, mvn clean test, mvn clean test -Dtest=Bar, mvn verify, mvn clean verify, mvn clean verify -DskipITs=false, mvn integration-test, mvn clean install integration-test, mvn package, mvn clean package -DskipTests, mvn -q clean -DskipTests, mvn clean -q, mvn clean install.
  • Maven Wrapper routing verified for: mvnw test, ./mvnw test, ./mvnw -q -Dtest=Foo test, mvnw clean verify, ./mvnw clean verify -DskipITs=false, mvnw integration-test, mvnw.cmd test, mvnw.bat clean install, and MAVEN_OPTS="-Xmx1g" ./mvnw test.
  • CRLF-safe (filter output identical for LF and CRLF input).
  • Failure context preserved in fixture-based test (regression guard).
  • Failsafe integration-test output covered by inline fixture test.

Notes for reviewers

  • Overlaps with feat(jvm): add mvn test/verify/integration-test filter #1907 (also adds mvn test|verify|integration-test filtering). Concrete differences:
    • Goal matching: The mentioned PR's ^mvn\s+(test|verify|integration-test)\b requires the test goal to be the first token after mvn, so mvn clean test, mvn -q -Dtest=Foo test, and mvn clean verify -DskipITs=false don't match it. This PR's ^mvn(?:\s+\S+)*\s+(test|verify|integration-test)\b accepts those, and a 14-case routing test in src/core/toml_filter.rs covers the combinations.
    • Build/test disambiguation: this PR adds the match_command_excludes engine field so mvn-build's match_command can stay simple while still deferring mvn clean test to mvn-test. The mentioned PR leaves mvn-build's match_command untouched, so mvn clean test matches both filters and routes by alphabetic load order (to mvn-build).
    • Failsafe IT coverage: inline fixture for [INFO] Running com.example.AppIT failure block.
    • Fixture + savings assertion: tests/fixtures/mvn_test_raw.txt (≈90-line representative Surefire 3.x output) with a ≥75% savings test on the failing-build case.
    • CRLF guard: explicit test_mvn_test_filter_crlf_safe asserting filter output is identical for LF and CRLF input.
  • The new match_command_excludes field is the only engine change. It's small, additive, opt-in — existing filters behave exactly as before. The design rationale and its role as a lookahead-substitute are documented inline on the field.
  • Hook doesn't auto-route mvn / mvnw despite handler existing #1537 scope note: plugin-prefixed goals like mvn checkstyle:check (mentioned in the issue's example list) are deliberately out of scope here. They follow a different <plugin>:<goal> shape and Surefire/Failsafe filtering doesn't apply to their output — they're a separate routing problem worth tracking on their own.

Adds a new mvn-test TOML filter that compresses Maven Surefire output
while preserving failure context. Filter strips boilerplate (downloads,
plugin lifecycle headers, T E S T S banner, passing-class summaries,
JVM agent warnings, framework stack frames from java/jdk/sun/junit/
maven-surefire, and Maven's "[Help 1]"/"Re-run with -X" footer hints)
and keeps [ERROR] failure lines, user-code stack frames, the Results:
block, BUILD SUCCESS/FAILURE, and total time.

Broadens the mvn rewrite regex in src/discover/rules.rs to accept
global flags before the goal (e.g. `mvn -q -Dtest=Foo test`) and adds
`test` as a recognised goal alternative. Refines mvn-build's
match_command so `mvn clean test` routes to mvn-test and
`mvn clean install` stays on mvn-build; the disambiguation regex is
documented inline because the `regex` crate has no lookahead.

Adds:
  - Inline filter tests covering failure, success, compile-error, empty
  - Routing tests in src/core/toml_filter.rs and src/discover/registry.rs
  - tests/fixtures/mvn_test_raw.txt — representative Surefire 3.x output
    with 8 passing tests, 2 failures, JVM warnings, and full stack trace
  - Token-savings assertion (>=75% on the failing-build fixture)
  - CRLF-safety assertion: filter output is identical for LF and CRLF
@CLAassistant

CLAassistant commented May 16, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Extends the new mvn-test filter to also handle Maven Failsafe integration
tests so `mvn verify`, `mvn clean verify`, and `mvn integration-test`
benefit from the same Surefire-style filtering. Failsafe's output format
is identical to Surefire's; existing strip rules already catch its noise
(plugin lifecycle headers, per-class `Running ...IT` lines, JVM warnings,
framework stack frames).

Routing changes:
  - mvn-test match_command now matches `test|verify|integration-test`
  - mvn-build's negative-lookahead-via-alternation regex is replaced with
    a clean `match_command_excludes` field — new optional TomlFilterDef
    knob that skips a filter when the command also matches the excludes
    pattern. Solves first-match-wins ordering without exploding the regex
    enumeration. `find_filter_in` applies the exclude check next to the
    match check.
  - src/discover/rules.rs rewrite regex extended with `verify` and
    `integration-test`; `subcmd_savings` gets two new entries at 80%

Tests:
  - 5 new routing cases in test_maven_builtin_filter_command_matching
    (mvn verify, mvn clean verify, mvn clean verify -DskipITs=false,
    mvn integration-test, mvn clean install integration-test)
  - test_classify_mvn_verify_and_integration_test in registry.rs
  - 3 new rewrite cases in test_rewrite_mvn_test_variants
  - New inline mvn-test fixture for a Failsafe IT failure block
  - Engine field has serde deny_unknown_fields coverage via the existing
    schema; explicit unit test isn't needed beyond the routing tests
    above which exercise the new code path.

README + docs/guide/resources/what-rtk-covers.md updated.
The first inline mvn-test fixture's expected string ended with a
`[ERROR] Please refer to ...surefire-reports...` line that the filter
now strips (via the `^\[ERROR\] Please refer to ` rule added when the
filter was broadened for Failsafe coverage). `rtk verify --filter
mvn-test` caught the mismatch (4/5 → 5/5 after fix); `cargo test`
did not, because the inline TOML comparisons are exercised only by
the verify CLI, not by the Rust unit-test harness.
Broadens the mvn rule to accept the Maven Wrapper (mvnw, ./mvnw,
mvnw.cmd, mvnw.bat) in addition to mvn. Adds classify and rewrite
tests covering env-prefixed wrappers (MAVEN_OPTS="..." ./mvnw test)
and global-flag invocations like ./mvnw -q -Dtest=Foo test.

@tylern91 tylern91 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR — significantly more thorough than the simpler filter approaches. Closing my own #1907 in favor of this one. Two findings from a deep review:

🐛 Functional gap: TOML filters don't match Maven Wrapper invocations

The rules.rs rule rewrites ./mvnw test → rtk ./mvnw test, and the discover tests confirm rewriting works for all 8 wrapper variants. However, both mvn-build.toml and mvn-test.toml anchor their match_command on ^mvn:

# mvn-test.toml
match_command = "^mvn(?:\\s+\\S+)*\\s+(?:test|verify|integration-test)\\b"

# mvn-build.toml
match_command = "^mvn\\s+(?:\\S+\\s+)*(compile|package|clean|install)\\b"

When the rewritten command rtk ./mvnw test flows through find_filter_in, neither regex matches — so the filter pipeline doesn't activate and output passes through unfiltered. The 14-case test_maven_builtin_filter_command_matching only covers mvn * forms, so this is silently uncaught. For Spring Boot projects (most modern Maven projects use the wrapper) this means the feature ships but doesn't actually filter.

Suggested fix:

# mvn-test.toml
match_command = "^(?:\\./mvnw|mvnw\\.cmd|mvnw\\.bat|mvnw|mvn)(?:\\s+\\S+)*\\s+(?:test|verify|integration-test)\\b"

# mvn-build.toml
match_command = "^(?:\\./mvnw|mvnw\\.cmd|mvnw\\.bat|mvnw|mvn)\\s+(?:\\S+\\s+)*(compile|package|clean|install)\\b"

Plus a few mvnw entries added to the routing test.

🧹 Nit: find_filter_in double-negative

&& !f.exclude_regex.as_ref().is_some_and(|r| r.is_match(command))

Reads more naturally as:

&& f.exclude_regex.as_ref().map_or(true, |r| !r.is_match(command))

("no exclusion regex, or the command doesn't match it" — matches the field doc semantics directly.)

Happy to submit either of these as a follow-up patch if it helps.

@ehsansasanian

Copy link
Copy Markdown
Author

Great PR — significantly more thorough than the simpler filter approaches. Closing my own #1907 in favor of this one. Two findings from a deep review:

🐛 Functional gap: TOML filters don't match Maven Wrapper invocations

The rules.rs rule rewrites ./mvnw test → rtk ./mvnw test, and the discover tests confirm rewriting works for all 8 wrapper variants. However, both mvn-build.toml and mvn-test.toml anchor their match_command on ^mvn:

# mvn-test.toml
match_command = "^mvn(?:\\s+\\S+)*\\s+(?:test|verify|integration-test)\\b"

# mvn-build.toml
match_command = "^mvn\\s+(?:\\S+\\s+)*(compile|package|clean|install)\\b"

When the rewritten command rtk ./mvnw test flows through find_filter_in, neither regex matches — so the filter pipeline doesn't activate and output passes through unfiltered. The 14-case test_maven_builtin_filter_command_matching only covers mvn * forms, so this is silently uncaught. For Spring Boot projects (most modern Maven projects use the wrapper) this means the feature ships but doesn't actually filter.

Suggested fix:

# mvn-test.toml
match_command = "^(?:\\./mvnw|mvnw\\.cmd|mvnw\\.bat|mvnw|mvn)(?:\\s+\\S+)*\\s+(?:test|verify|integration-test)\\b"

# mvn-build.toml
match_command = "^(?:\\./mvnw|mvnw\\.cmd|mvnw\\.bat|mvnw|mvn)\\s+(?:\\S+\\s+)*(compile|package|clean|install)\\b"

Plus a few mvnw entries added to the routing test.

🧹 Nit: find_filter_in double-negative

&& !f.exclude_regex.as_ref().is_some_and(|r| r.is_match(command))

Reads more naturally as:

&& f.exclude_regex.as_ref().map_or(true, |r| !r.is_match(command))

("no exclusion regex, or the command doesn't match it" — matches the field doc semantics directly.)

Happy to submit either of these as a follow-up patch if it helps.

Thanks for the careful review! Both points worth addressing.

On the mvnw filter-matching concern — I think this one's a false alarm.

The rewrite path doesn't produce rtk ./mvnw test. The rewriter in src/discover/registry.rs replaces the matched prefix with rule.rtk_cmd rather than prepending rtk to the original command:

for &prefix in rule.rewrite_prefixes { if let Some(rest) = strip_word_prefix(cmd_part, prefix) { let rewritten = format!("{} {}{}", rule.rtk_cmd, rest, redirect_suffix); ...

Since rtk_cmd = "rtk mvn", the actual rewrites are:

  • ./mvnw test → rtk mvn test
  • mvnw.cmd test → rtk mvn test
  • mvnw.bat clean install → rtk mvn clean install

This is exactly what test_rewrite_mvnw_variants (registry.rs:1358) asserts for all 8 wrapper variants. So by the time find_filter_in sees the command, the mvnw* prefix is gone and the ^mvn-anchored TOML regexes match cleanly - which is why the PR description says "All wrapper invocations rewrite to rtk mvn …, so the existing TOML filters match unchanged".

Broadening the TOML match_command regexes to include mvnw* would be dead code in the current pipeline, and adding mvnw entries to test_maven_builtin_filter_command_matching would assert behavior that can't actually happen at runtime.

On the find_filter_in style nit.

Both forms are semantically equivalent, but clippy's unnecessary_map_or lint actively prefers is_some_and map_or(true, |r| !r.is_match(x)) — the current form is the clippy-idiomatic one. I'd rather keep it as-is to stay on clippy's good side. The field's doc comment carries the "skip if exclude matches" semantics clearly enough that I don't think the inline expression needs to mirror them.

Happy to revisit either if I'm missing something — and thanks again for the depth of the review and for closing #1907 in favor of this one.

@YOMXXX

YOMXXX commented May 20, 2026

Copy link
Copy Markdown
Contributor

👋 Thanks for the mvn filter work! Heads up to the maintainers and to @ehsansasanian / @vdufloth / @bkirosangma — three PRs currently target the mvn surface and they're not strictly mergeable in parallel:

Merging any one of these will create conflicts for the other two. A few options for the maintainers to consider:

  1. Pick a base: merge feat(mvn)!: Rust module replacing TOML filter, adds test phase support #1956 first as the new Rust foundation, then rebase feat(jvm): add rtk mvn with Surefire/Failsafe XML test summarization #1974 on top (XML summarization integrates cleanly with a Rust module). feat(mvn): add mvn test filter with broadened goal matching #1921's goal-matching expansion would need to be re-targeted at the new Rust module.
  2. Stage feat(mvn): add mvn test filter with broadened goal matching #1921 first as a tactical fix on the current TOML filter, then plan the Rust migration (feat(mvn)!: Rust module replacing TOML filter, adds test phase support #1956) as a separate cycle.
  3. Co-author — if the three authors are open to it, merge the highest-quality base and cherry-pick targeted commits from the others with co-author attribution.

Not blocking #1921 with this comment — just flagging the situation so reviewers can decide on a strategy before any of them gets approved. Happy to help map out integration points if useful.

@tylern91

Copy link
Copy Markdown

The rewriter in src/discover/registry.rs replaces the matched prefix with rule.rtk_cmd rather than prepending rtk to the original command
Since rtk_cmd = "rtk mvn", the actual rewrites are:

  • ./mvnw test → rtk mvn test
  • mvnw.cmd test → rtk mvn test
  • mvnw.bat clean install → rtk mvn clean install

You're right — I traced the TOML matching in isolation without following the rewrite pipeline through strip_word_prefix, which normalizes all wrapper variants to rtk mvn before find_filter_in ever sees them. Good design — the normalization layer means TOML filters don't need to know about wrapper binaries at all.

Both forms are semantically equivalent, but clippy's unnecessary_map_or lint actively prefers is_some_and over map_or(true, |r| !r.is_match(x))

Fair point — I was thinking readability in isolation rather than lint consistency.

Clean PR. No further comments from me.

@rtk-release-bot

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale due to 90 days of inactivity. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. Your contribution is very much appreciated — we're sorry we haven't been able to review it yet.

@rtk-release-bot rtk-release-bot Bot added the stale label Sep 3, 2026
@KuSh

KuSh commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator

Thanks for this. The mvn support here was landed by #1956 on 2026-06-08 and extended by #3199 on 2026-08-30: the discover rule now matches ./mvnw, mvnw.cmd, mvnw and mvn with the compile, test, integration-test, package, install, verify and deploy phases, and the TOML filters were replaced by a Rust module. The RtkRule struct also changed since this PR was written, so the rule hunk no longer applies as is. One case is still open: mvnw.bat is not in the pattern or the rewrite prefixes, so mvnw.bat clean install is left unrewritten. A one line PR adding that token to src/discover/rules.rs, or an issue, would close it. If I missed something here, please comment or reopen.

@KuSh KuSh closed this Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hook doesn't auto-route mvn / mvnw despite handler existing

5 participants