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 <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-11-24 21:10:45 +08:00
1 джерело bee5e2ed1f
коміт bb850b25d6
10 files changed
+102 -12

No files matched your search

+7 -1
Переглянути файл
@@ -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",
+14 -2
Переглянути файл
@@ -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"]);
+13 -1
Переглянути файл
@@ -24,23 +24,35 @@ describe("JUnit configurations", () => {
const line = (p: Record<string, string>) => 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"]);
});
+16
Переглянути файл
@@ -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 `<properties>` 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",
]);
});
});
+7 -2
Переглянути файл
@@ -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 `<properties>` or in `.mvn/maven.config`, which would otherwise only compile. A value
* written literally in the Surefire plugin's `<configuration>` 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];
}
+9 -1
Переглянути файл
@@ -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",
]);
});
});
+9
Переглянути файл
@@ -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);
+23 -3
Переглянути файл
@@ -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 () => {
+2 -1
Переглянути файл
@@ -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."
}
+2 -1
Переглянути файл
@@ -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。"
}