fix(run): let Maven reactor modules without the selected tests pass in every test run (#4845)

In a Maven multi-module project, Rerun Failed Tests and Run on a test file failed before the selected tests ran: -Dtest reached the first module without a matching test and Surefire stopped the build, skipping the module with the test. Those two paths built -Dtest themselves without -Dsurefire.failIfNoSpecifiedTests=false; every Maven test selection now goes through withMavenTests, which replaces an earlier -Dtest and always adds the flag.

Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com>

Co-authored-by: ntbang0901 <121003585+ntbang0901@users.noreply.github.com>
This commit is contained in:
ntbang0901
2026-11-20 23:44:02 +08:00
committed by GitHub
parent 1d857375dc
commit 4f192a12bc
9 changed files with 44 additions and 16 deletions
+3 -2
View File
@@ -8,6 +8,7 @@ import { CLASSPATH, TEST_CLASSPATH, jvmModuleDir } from "@/platform/javaLaunch";
import { isTestPath, rootFor } from "@/platform/languageSettings";
import { currentRoots } from "@/platform/projectRoots";
import { simpleClassName } from "./jvmTargets";
import { withMavenTests } from "./mavenTests";
export { jvmModuleDir } from "@/platform/javaLaunch";
@@ -49,7 +50,7 @@ export function applicationConfig(
/**
* Narrows a Maven / Gradle test configuration to a class (`method` unset) or
* one method: Surefire `-Dtest=Class#method`, Gradle `--tests Class.method`.
* `fileConfig` is the file-level configuration whose last argument is the file filter.
* `fileConfig` is the file-level configuration; for Gradle its last argument is the file filter.
*/
export function jvmTestConfig(fileConfig: RunConfig, className: string, method: string | undefined): RunConfig | null {
const args = fileConfig.args.slice(0, -1);
@@ -63,7 +64,7 @@ export function jvmTestConfig(fileConfig: RunConfig, className: string, method:
id,
name,
env: { ...fileConfig.env },
args: [...args, `-Dtest=${className}${method ? `#${method}` : ""}`, "-Dsurefire.failIfNoSpecifiedTests=false"],
args: withMavenTests(fileConfig.args, `${className}${method ? `#${method}` : ""}`),
};
case "gradle":
return {
+2 -1
View File
@@ -10,6 +10,7 @@ import { phpunitArgs } from "./testing/phpunit";
import { goTestParts } from "./goArguments";
import { goBuildType } from "./goTypes";
import { isWindows } from "./jvmTypes";
import { withMavenTests } from "./mavenTests";
import { typeFor } from "./types";
import { runTemplate } from "./runTemplates";
import { appendScriptArgs } from "./scriptArgs";
@@ -279,7 +280,7 @@ export function testConfigFor(
case "gradle":
return derived(base, relPath, { args: [...base.args, "--tests", `*${stem(relPath)}`] });
case "maven":
return derived(base, relPath, { args: [...base.args, `-Dtest=${stem(relPath)}`] });
return derived(base, relPath, { args: withMavenTests(base.args, stem(relPath)) });
}
return base;
}
+6 -1
View File
@@ -261,7 +261,12 @@ describe("application configurations", () => {
});
it("scopes Maven and Gradle test configs to a class or method", () => {
const file = cfg({ id: "f", kind: "maven", args: ["test", "-Dtest=OrderTest"], isTest: true });
const file = cfg({
id: "f",
kind: "maven",
args: ["test", "-Dtest=OrderTest", "-Dsurefire.failIfNoSpecifiedTests=false"],
isTest: true,
});
expect(jvmTestConfig(file, "a.OrderTest$Nested", "fails")).toMatchObject({
name: "Nested.fails",
args: ["test", "-Dtest=a.OrderTest$Nested#fails", "-Dsurefire.failIfNoSpecifiedTests=false"],
+4 -4
View File
@@ -15,6 +15,7 @@ import {
splitJvmArgs,
vmOptionsField,
} from "./jvmTypes";
import { withMavenTests } from "./mavenTests";
import { useRun } from "./store";
type TestKind = "all" | "package" | "class" | "method" | "pattern";
@@ -40,8 +41,6 @@ export function junitCommandLine(
const base = baseFor(p, configs);
if (!base) return { command: "", args: [], cwd: null };
const maven = base.kind === "maven";
const select = (pattern: string): string[] =>
maven ? [`-Dtest=${pattern}`, "-Dsurefire.failIfNoSpecifiedTests=false"] : ["--tests", pattern];
let args = [...base.args];
const cls = p.className?.trim() ?? "";
switch (kindOf(p)) {
@@ -56,7 +55,7 @@ export function junitCommandLine(
}
case "package": {
const pkg = p.packageName?.trim();
if (pkg) args.push(...select(maven ? `${pkg}.**.*` : `${pkg}.*`));
if (pkg) args = maven ? withMavenTests(args, `${pkg}.**.*`) : [...args, "--tests", `${pkg}.*`];
break;
}
case "pattern": {
@@ -64,7 +63,8 @@ export function junitCommandLine(
.split(",")
.map((s) => s.trim())
.filter(Boolean);
if (patterns.length) args.push(...(maven ? select(patterns.join(",")) : patterns.flatMap(select)));
if (patterns.length)
args = maven ? withMavenTests(args, patterns.join(",")) : [...args, ...patterns.flatMap((p) => ["--tests", p])];
break;
}
case "all":
+11
View File
@@ -0,0 +1,11 @@
/**
* Surefire's test selection for a Maven run. In a reactor, `-Dtest` applies to every module, and a
* module without a matching test fails the build ("No tests matching pattern") before the module
* that has it runs, so every selection also lets modules without matches pass.
*/
const NO_MATCH_OK = "-Dsurefire.failIfNoSpecifiedTests=false";
/** `args` running only the tests `pattern` selects (Surefire syntax), replacing an earlier selection. */
export function withMavenTests(args: readonly string[], pattern: string): string[] {
return [...args.filter((a) => !a.startsWith("-Dtest=") && a !== NO_MATCH_OK), `-Dtest=${pattern}`, NO_MATCH_OK];
}
+2 -1
View File
@@ -750,7 +750,8 @@ describe("single-file producers (scratches)", () => {
ALL,
)!;
expect(tests.config).toMatchObject({ kind: "maven", isTest: true });
expect(tests.config!.args).toEqual(["test", "-Dtest=Test"]);
// A reactor module without `Test` must not fail the build before the module that has it.
expect(tests.config!.args).toEqual(["test", "-Dtest=Test", "-Dsurefire.failIfNoSpecifiedTests=false"]);
});
});
+2 -5
View File
@@ -31,6 +31,7 @@ import { djangoLabel, djangoTestsOf, hasDjangoTests } from "./djangoTypes";
import { firstTopLevelType, scanJvmTargets, simpleClassName } from "./jvmTargets";
import { isWindows } from "./jvmTypes";
import { inheritedRunTargets } from "./inheritedMain";
import { withMavenTests } from "./mavenTests";
import { detectRunTargets, goFuzzConfig, targetConfig, targetLanguage, zigTargets, type RunTarget } from "./targets";
import { tx } from "@/platform/i18n";
@@ -1348,11 +1349,7 @@ export function produceDirectory(input: ContextInput): Produced | null {
const pkg = jvmPackage[2]!.replace(/\//g, ".");
return base.kind === "gradle"
? make(moduleBase, `Tests in '${pkg}'`, [...moduleArgs(base), "--tests", `${pkg}.*`])
: make(moduleBase, `Tests in '${pkg}'`, [
...base.args,
`-Dtest=${pkg}.**.*`,
"-Dsurefire.failIfNoSpecifiedTests=false",
]);
: make(moduleBase, `Tests in '${pkg}'`, withMavenTests(base.args, `${pkg}.**.*`));
}
}
const js = closest(["npm", "bun", "deno"]);
+12 -1
View File
@@ -201,7 +201,18 @@ describe("rerunFailedConfig", () => {
["com.a.FooTest", "baz"],
["com.a.BarTest"],
]))!.args,
).toEqual(["test", "-Dtest=com.a.FooTest#bar+baz,com.a.BarTest"]);
).toEqual(["test", "-Dtest=com.a.FooTest#bar+baz,com.a.BarTest", "-Dsurefire.failIfNoSpecifiedTests=false"]);
});
it("maven: a rerun replaces the previous selection and lets modules without the tests pass", async () => {
const narrowed = cfg("maven", "./mvnw", ["test", "-Dtest=Old", "-Dsurefire.failIfNoSpecifiedTests=false"]);
expect((await rerunFailedConfig(narrowed, [["com.a.FooTest", "bar"]]))!.args).toEqual([
"test",
"-Dtest=com.a.FooTest#bar",
"-Dsurefire.failIfNoSpecifiedTests=false",
]);
const [selected] = await testsConfigs(cfg("maven", "mvn", ["test"]), [["com.a.FooTest", "baz"]], "FooTest.baz");
expect(selected!.args).toEqual(["test", "-Dtest=com.a.FooTest#baz", "-Dsurefire.failIfNoSpecifiedTests=false"]);
});
it("zig: --test-filter per failed test for `zig test`, null for build-based runs", async () => {
+2 -1
View File
@@ -9,6 +9,7 @@ import { formatArgs, parseArgs } from "../configs";
import { appendScriptArgs } from "../scriptArgs";
import { djangoTestsOf } from "../djangoTypes";
import { goTestParts, withoutGoSelection } from "../goArguments";
import { withMavenTests } from "../mavenTests";
import { frameworkFor } from "./index";
import { dotnetFilter } from "./dotnet";
import { nodeStatus, type TestNode, type TestResults } from "./results";
@@ -134,7 +135,7 @@ function jvmArgs(config: RunConfig, paths: string[][]): string[] | null {
...[...byClass].map(([cls, ms]) => `${cls}#${unique(ms).join("+")}`),
...classes.filter((c) => !byClass.has(c)),
];
return [...config.args.filter((a) => !a.startsWith("-Dtest=")), `-Dtest=${specs.join(",")}`];
return withMavenTests(config.args, specs.join(","));
}
const filters = [...unique(tests.map((t) => `${t.cls}.${t.method}`)), ...classes];
return [...dropFlag(config.args, ["--tests"]), ...filters.flatMap((f) => ["--tests", f])];