diff --git a/src/features/run/application.ts b/src/features/run/application.ts index ef2bbd08e..488d62daa 100644 --- a/src/features/run/application.ts +++ b/src/features/run/application.ts @@ -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 { diff --git a/src/features/run/context.ts b/src/features/run/context.ts index 4f3dfb2d0..206658f77 100644 --- a/src/features/run/context.ts +++ b/src/features/run/context.ts @@ -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; } diff --git a/src/features/run/java.test.ts b/src/features/run/java.test.ts index 2071c470f..7e2677324 100644 --- a/src/features/run/java.test.ts +++ b/src/features/run/java.test.ts @@ -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"], diff --git a/src/features/run/junitType.ts b/src/features/run/junitType.ts index f32138264..7d1a0a9af 100644 --- a/src/features/run/junitType.ts +++ b/src/features/run/junitType.ts @@ -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": diff --git a/src/features/run/mavenTests.ts b/src/features/run/mavenTests.ts new file mode 100644 index 000000000..9618e06b7 --- /dev/null +++ b/src/features/run/mavenTests.ts @@ -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]; +} diff --git a/src/features/run/producers.test.ts b/src/features/run/producers.test.ts index 869d6844e..7da8f3fc4 100644 --- a/src/features/run/producers.test.ts +++ b/src/features/run/producers.test.ts @@ -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"]); }); }); diff --git a/src/features/run/producers.ts b/src/features/run/producers.ts index b512ad3af..b2358dc66 100644 --- a/src/features/run/producers.ts +++ b/src/features/run/producers.ts @@ -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"]); diff --git a/src/features/run/testing/rerun.test.ts b/src/features/run/testing/rerun.test.ts index d015c46ee..c9e91f150 100644 --- a/src/features/run/testing/rerun.test.ts +++ b/src/features/run/testing/rerun.test.ts @@ -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 () => { diff --git a/src/features/run/testing/rerun.ts b/src/features/run/testing/rerun.ts index 8b4b48666..cb9caa814 100644 --- a/src/features/run/testing/rerun.ts +++ b/src/features/run/testing/rerun.ts @@ -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])];