From bb850b25d66745a5dd82cedf8c91ccbaca4c6902 Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Tue, 24 Nov 2026 22:10:45 +0900 Subject: [PATCH] fix(run): run selected Maven tests even when the build skips tests (#5521) Running a JUnit test or class from the gutter only compiled when the build skipped tests through skipTests/maven.test.skip in POM properties or .mvn/maven.config: Maven printed 'Tests are skipped' and the Run window showed No tests. Every Maven test selection (gutter, JUnit configurations, package runs, Rerun Failed Tests) goes through withMavenTests, which now also passes -DskipTests=false -Dmaven.test.skip=false; when nothing ran, the console says why it may have been skipped. Fixes #5518. Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com> --- src/features/run/contextRun.test.ts | 8 +++++++- src/features/run/java.test.ts | 16 ++++++++++++++-- src/features/run/junitType.test.ts | 14 +++++++++++++- src/features/run/mavenTests.test.ts | 16 ++++++++++++++++ src/features/run/mavenTests.ts | 9 +++++++-- src/features/run/producers.test.ts | 10 +++++++++- src/features/run/store.ts | 9 +++++++++ src/features/run/testing/rerun.test.ts | 26 +++++++++++++++++++++++--- src/platform/locales/text/en.json | 3 ++- src/platform/locales/text/zh.json | 3 ++- 10 files changed, 102 insertions(+), 12 deletions(-) create mode 100644 src/features/run/mavenTests.test.ts diff --git a/src/features/run/contextRun.test.ts b/src/features/run/contextRun.test.ts index c5082d7a93..e30bfdcafc 100644 --- a/src/features/run/contextRun.test.ts +++ b/src/features/run/contextRun.test.ts @@ -154,7 +154,13 @@ describe("run context menu group", () => { ).toMatchObject({ kind: "maven", cwd: member, - args: ["test", "-Dtest=demo.AppTest#starts", "-Dsurefire.failIfNoSpecifiedTests=false"], + args: [ + "test", + "-Dtest=demo.AppTest#starts", + "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", + ], }); expect(produceContext({ ...main, path: `${ROOT}/src/main/java/demo/App.java` }, OPTIMISTIC)?.config).toMatchObject({ kind: "spring-boot", diff --git a/src/features/run/java.test.ts b/src/features/run/java.test.ts index 7e26773240..35eb29ec43 100644 --- a/src/features/run/java.test.ts +++ b/src/features/run/java.test.ts @@ -264,12 +264,24 @@ describe("application configurations", () => { const file = cfg({ id: "f", kind: "maven", - args: ["test", "-Dtest=OrderTest", "-Dsurefire.failIfNoSpecifiedTests=false"], + args: [ + "test", + "-Dtest=OrderTest", + "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", + ], isTest: true, }); expect(jvmTestConfig(file, "a.OrderTest$Nested", "fails")).toMatchObject({ name: "Nested.fails", - args: ["test", "-Dtest=a.OrderTest$Nested#fails", "-Dsurefire.failIfNoSpecifiedTests=false"], + args: [ + "test", + "-Dtest=a.OrderTest$Nested#fails", + "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", + ], }); const gradle = cfg({ id: "g", kind: "gradle", args: ["test", "--tests", "*OrderTest"], isTest: true }); expect(jvmTestConfig(gradle, "a.OrderTest", undefined)!.args).toEqual(["test", "--tests", "a.OrderTest"]); diff --git a/src/features/run/junitType.test.ts b/src/features/run/junitType.test.ts index 01620cec1d..1dc79ab9c2 100644 --- a/src/features/run/junitType.test.ts +++ b/src/features/run/junitType.test.ts @@ -24,23 +24,35 @@ describe("JUnit configurations", () => { const line = (p: Record) => junitCommandLine({ module: ".", ...p }, maven); expect(line({ testKind: "class", className: "a.OrderTest" })).toEqual({ command: "./mvnw", - args: ["test", "-Dtest=a.OrderTest", "-Dsurefire.failIfNoSpecifiedTests=false"], + args: [ + "test", + "-Dtest=a.OrderTest", + "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", + ], cwd: null, }); expect(line({ testKind: "method", className: "a.OrderTest", methodName: "ships" }).args).toEqual([ "test", "-Dtest=a.OrderTest#ships", "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", ]); expect(line({ testKind: "package", packageName: "a.b" }).args).toEqual([ "test", "-Dtest=a.b.**.*", "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", ]); expect(line({ testKind: "pattern", pattern: "*IT, a.**.*Test" }).args).toEqual([ "test", "-Dtest=*IT,a.**.*Test", "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", ]); expect(line({ testKind: "all", vmOptions: "-Xmx1g -Da=b" }).args).toEqual(["test", "-DargLine=-Xmx1g -Da=b"]); }); diff --git a/src/features/run/mavenTests.test.ts b/src/features/run/mavenTests.test.ts new file mode 100644 index 0000000000..58b4754f6b --- /dev/null +++ b/src/features/run/mavenTests.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import { withMavenTests } from "./mavenTests"; + +describe("withMavenTests", () => { + it("runs the selected tests even when the build or the configuration skips tests", () => { + // `skipTests` in the POM's `` or `.mvn/maven.config` only compiled: `-D` on the command line wins. + expect(withMavenTests(["test", "-DskipTests", "-Dmaven.test.skip=true", "-o"], "demo.DemoTest#test1")).toEqual([ + "test", + "-o", + "-Dtest=demo.DemoTest#test1", + "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", + ]); + }); +}); diff --git a/src/features/run/mavenTests.ts b/src/features/run/mavenTests.ts index 9618e06b74..58e0844f4a 100644 --- a/src/features/run/mavenTests.ts +++ b/src/features/run/mavenTests.ts @@ -2,10 +2,15 @@ * 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. + * A selection is an explicit request to run those tests: it overrides `skipTests` / `maven.test.skip` + * set as POM `` or in `.mvn/maven.config`, which would otherwise only compile. A value + * written literally in the Surefire plugin's `` can't be overridden from the command line. */ -const NO_MATCH_OK = "-Dsurefire.failIfNoSpecifiedTests=false"; +const OWN = ["-Dsurefire.failIfNoSpecifiedTests=false", "-DskipTests=false", "-Dmaven.test.skip=false"]; +const replaced = (a: string) => + a.startsWith("-Dtest=") || /^-D(skipTests|maven\.test\.skip|surefire\.failIfNoSpecifiedTests)(=|$)/.test(a); /** `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]; + return [...args.filter((a) => !replaced(a)), `-Dtest=${pattern}`, ...OWN]; } diff --git a/src/features/run/producers.test.ts b/src/features/run/producers.test.ts index aac26741e3..1c1cfa5850 100644 --- a/src/features/run/producers.test.ts +++ b/src/features/run/producers.test.ts @@ -742,6 +742,8 @@ describe("single-file producers (scratches)", () => { "test", "-Dtest=demo.Test#works", "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", ]); // No `main`: the file-level context runs the class's tests. const noMain = "package demo;\npublic class Test {\n @Test\n void works() {\n }\n}\n"; @@ -751,7 +753,13 @@ describe("single-file producers (scratches)", () => { )!; expect(tests.config).toMatchObject({ kind: "maven", isTest: true }); // 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"]); + expect(tests.config!.args).toEqual([ + "test", + "-Dtest=Test", + "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", + ]); }); }); diff --git a/src/features/run/store.ts b/src/features/run/store.ts index d422495eaa..cc5141f793 100644 --- a/src/features/run/store.ts +++ b/src/features/run/store.ts @@ -1316,6 +1316,15 @@ function finishSession(sessionId: string, code: number | null) { if (runtimes.get(sessionId) !== rt) return; rt.tests.finish(code); recordTestStates(rt.tests); + // The build's test task succeeded without running a test: say why instead of ending silently. + const ran = rt.tests.counts(); + if (code === 0 && frameworkFor(session.config) === "jvm" && !(ran.passed + ran.failed + ran.skipped)) { + const why = tx( + "No tests ran. The build may skip them (skipTests, maven.test.skip, a disabled test task) or lack their engine: JUnit 4 tests on the JUnit Platform need junit-vintage-engine.", + ); + rt.console.append({ kind: "system", text: `\n${why}\n` }); + rt.console.flush(); + } } patchSession(sessionId, { status: "finished", exitCode: code, endedAt: Date.now() }); bump(sessionId); diff --git a/src/features/run/testing/rerun.test.ts b/src/features/run/testing/rerun.test.ts index 0d5b4c75ec..4a489930ea 100644 --- a/src/features/run/testing/rerun.test.ts +++ b/src/features/run/testing/rerun.test.ts @@ -212,18 +212,38 @@ describe("rerunFailedConfig", () => { ["com.a.FooTest", "baz"], ["com.a.BarTest"], ]))!.args, - ).toEqual(["test", "-Dtest=com.a.FooTest#bar+baz,com.a.BarTest", "-Dsurefire.failIfNoSpecifiedTests=false"]); + ).toEqual([ + "test", + "-Dtest=com.a.FooTest#bar+baz,com.a.BarTest", + "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=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"]); + const narrowed = cfg("maven", "./mvnw", [ + "test", + "-Dtest=Old", + "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", + ]); expect((await rerunFailedConfig(narrowed, [["com.a.FooTest", "bar"]]))!.args).toEqual([ "test", "-Dtest=com.a.FooTest#bar", "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=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"]); + expect(selected!.args).toEqual([ + "test", + "-Dtest=com.a.FooTest#baz", + "-Dsurefire.failIfNoSpecifiedTests=false", + "-DskipTests=false", + "-Dmaven.test.skip=false", + ]); }); it("zig: --test-filter per failed test for `zig test`, null for build-based runs", async () => { diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index b06633e491..de4e81795f 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -3864,5 +3864,6 @@ "Mark as Unread": "Mark as Unread", "Archive All Below": "Archive All Below", "Mark Agent Tasks as Unread": "Mark Agent Tasks as Unread", - "Mark Agent Tasks as Read": "Mark Agent Tasks as Read" + "Mark Agent Tasks as Read": "Mark Agent Tasks as Read", + "No tests ran. The build may skip them (skipTests, maven.test.skip, a disabled test task) or lack their engine: JUnit 4 tests on the JUnit Platform need junit-vintage-engine.": "No tests ran. The build may skip them (skipTests, maven.test.skip, a disabled test task) or lack their engine: JUnit 4 tests on the JUnit Platform need junit-vintage-engine." } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 141a646270..bd04f036db 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -10094,5 +10094,6 @@ "Mark as Unread": "标记为未读", "Archive All Below": "归档下方全部", "Mark Agent Tasks as Unread": "将 Agent 任务标记为未读", - "Mark Agent Tasks as Read": "将 Agent 任务标记为已读" + "Mark Agent Tasks as Read": "将 Agent 任务标记为已读", + "No tests ran. The build may skip them (skipTests, maven.test.skip, a disabled test task) or lack their engine: JUnit 4 tests on the JUnit Platform need junit-vintage-engine.": "没有运行任何测试。构建可能跳过了测试(skipTests、maven.test.skip、被禁用的测试任务),或缺少对应的测试引擎:在 JUnit Platform 上运行 JUnit 4 测试需要 junit-vintage-engine。" }