Repository navigation
feat(mvn): add mvn test filter with broadened goal matching - #1921
ehsansasanian wants to merge 4 commits into
Conversation
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
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
left a comment
There was a problem hiding this comment.
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:
Since rtk_cmd = "rtk mvn", the actual rewrites are:
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. |
|
👋 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:
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. |
You're right — I traced the TOML matching in isolation without following the rewrite pipeline through
Fair point — I was thinking readability in isolation rather than lint consistency. Clean PR. No further comments from me. |
|
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. |
|
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: |
Closes #1537.
Summary
Adds Maven Surefire and Failsafe test output filtering —
mvn test,mvn verify,mvn integration-test— on top of the existingmvn-buildfilter, with a corrected rewrite regex, Maven Wrapper support (mvnw,./mvnw,mvnw.cmd,mvnw.bat), and explicit routing between the two filters via a newmatch_command_excludesTOML field.Motivation
LLM agents running
mvn test,mvn verify, ormvn integration-testagainst a real Java project routinely consume thousands of tokens on Surefire/Failsafe boilerplate (download lines, plugin lifecycle headers, theT E S T Sbanner, per-class passing summaries, JVM agent warnings, framework stack frames, and the[Help 1]/Re-run with -Xfooter). 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[INFO]chatter), Surefire/Failsafe noise (theT E S T Sbanner, per-classRunning com.example.AppIT/Running com.example.FooTestlines, passing per-class summaries), JVM agent warnings, framework stack frames (java.*,jdk.internal.*,sun.*,org.junit.*,org.apache.maven.surefire.*,org.testng.*, plusapp//JPMS prefixes), and Maven's footer hints ([Help 1],Re-run with -X,Please refer to …).[ERROR]failure lines (per-class and per-method), user-code stack frames, theResults:block,BUILD SUCCESS/BUILD FAILURE, total time, and the final summary.[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.on_empty = "mvn test: ok"for fully-stripped passing runs.Rewrite rule:
src/discover/rules.rs^(?:\./mvnw|mvnw\.cmd|mvnw\.bat|mvnw|mvn)(?:\s+\S+)*\s+(compile|package|clean|install|test|verify|integration-test)\bso it accepts the Maven Wrapper, global flags, and multi-goal invocations like:mvn -q -Dtest=UserServiceTest testmvn clean testmvn clean verify -DskipITs=falsemvn integration-test./mvnw test,./mvnw -q -Dtest=Foo test,mvnw clean verify,mvnw.cmd test,mvnw.bat clean installrewrite_prefixesnow lists./mvnw,mvnw.cmd,mvnw.bat,mvnw,mvn(longest-first so./mvnwwins beforemvnw). All wrapper invocations rewrite tortk mvn …, so the existing TOML filters match unchanged.("test", 80.0),("verify", 80.0),("integration-test", 80.0)tosubcmd_savings.Engine addition:
src/core/toml_filter.rsmatch_command_excludesfield onTomlFilterDef. When set and matches the command, the filter is skipped byfind_filter_in. This is a clean negative-match mechanism that substitutes for negative lookahead (unsupported by theregexcrate) and is reusable for any future filter-routing conflict.Routing fix:
src/filters/mvn-build.tomlmatch_commandreverts to the original simple form^mvn\s+(?:\S+\s+)*(compile|package|clean|install)\b.match_command_excludes = "(?:^|\s)(?:test|verify|integration-test)(?:\s|$)"ensuresmvn clean test,mvn clean verify, andmvn integration-testroute tomvn-test, notmvn-build, despite alphabetic filter load order makingmvn-buildfirst in line.Tests
[[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_matchinginsrc/core/toml_filter.rs— 14 routing cases verifying every flag/goal combination routes to the correct filter (test, verify, integration-test, mixedclean install integration-test, etc.).test_classify_mvn_test_with_global_options,test_classify_mvn_verify_and_integration_test, andtest_rewrite_mvn_test_variantsinsrc/discover/registry.rs.test_classify_mvnw_variants(8 cases),test_classify_mvnw_with_env_prefix(coversMAVEN_OPTS="-Xmx1g" ./mvnw test), andtest_rewrite_mvnw_variants(8 cases) — verify the wrapper variants both classify asrtk mvnand rewrite tortk mvn ….test_mvn_test_fixture_savings_and_failure_preservation— loadstests/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
on_emptyor justBUILD SUCCESS+ total timeThe 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).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.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, andMAVEN_OPTS="-Xmx1g" ./mvnw test.Notes for reviewers
mvn test|verify|integration-testfiltering). Concrete differences:^mvn\s+(test|verify|integration-test)\brequires the test goal to be the first token aftermvn, somvn clean test,mvn -q -Dtest=Foo test, andmvn clean verify -DskipITs=falsedon't match it. This PR's^mvn(?:\s+\S+)*\s+(test|verify|integration-test)\baccepts those, and a 14-case routing test insrc/core/toml_filter.rscovers the combinations.match_command_excludesengine field somvn-build'smatch_commandcan stay simple while still deferringmvn clean testtomvn-test. The mentioned PR leavesmvn-build'smatch_commanduntouched, somvn clean testmatches both filters and routes by alphabetic load order (tomvn-build).[INFO] Running com.example.AppITfailure block.tests/fixtures/mvn_test_raw.txt(≈90-line representative Surefire 3.x output) with a≥75%savings test on the failing-build case.test_mvn_test_filter_crlf_safeasserting filter output is identical for LF and CRLF input.match_command_excludesfield 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.mvn/mvnwdespite handler existing #1537 scope note: plugin-prefixed goals likemvn 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.