Файли
openidea/src/features/run/testing/index.ts
T
LU XIANGRUIтаwildcatDownstairs 511d3f21bb feat(run): run swift test with results in the test tree and single tests from the gutter (#6798)
swift test runs now feed the test tree: XCTest results are read from its console lines (including failure locations), and Swift Testing results from its JSON event stream (ABI version 0), requested with --experimental-event-stream-output so suite-qualified ids and issues arrive while the run goes. Swift files under Tests/<target>/ that import XCTest or Testing get gutter run icons on XCTestCase test methods, @Test functions and the types holding them or marked @Suite, each running swift test --filter in the package directory; Rerun Failed and Run Selected build one --filter per test.

Adds fixtures/swiftpm-app with XCTest and Swift Testing tests; it declares macOS 10.15 so its Swift Testing tests also build with older toolchains (verified with Swift 6.1).

Closes #6788

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-29 14:44:13 +08:00

258 рядки
10 KiB
TypeScript

/** Picks the test output parser for a run configuration. */
import type { RunConfig } from "@/platform/ipc";
import { isJunitRunner } from "../application";
import { cargoParser } from "./cargo";
import { cypressParser, cypressSpecs } from "./cypress";
import { goParser } from "./go";
import { jsParser } from "./js";
import { withJasmineHook } from "./jasmine";
import { jvmParser } from "./jvm";
import { withMochaHook } from "./mocha";
import { withNodeTestReporter } from "./nodeTest";
import { recordsParser } from "./records";
import { phpunitParser, withTeamcity } from "./phpunit";
import { playwrightParser } from "./playwright";
import { pytestParser } from "./pytest";
import { withDoctestReporter } from "./pythonDoctest";
import { withUnittestReporter } from "./pythonUnittest";
import { rubyParser } from "./ruby";
import { unittestParser } from "./unittest";
import { swiftParser, withSwiftEventStream } from "./swift";
import { zigParser } from "./zig";
import { isWindows } from "../jvmTypes";
import type { TestEvent, TestOutputParser } from "./types";
import { appendScriptArgs } from "../scriptArgs";
export type { TestEvent, TestOutputParser, TestStatus } from "./types";
export { formatDuration, parseDuration } from "./types";
export { TestResults, nodeDuration, nodeOutput, nodeStatus, type TestNode } from "./results";
export {
cargoParser,
goParser,
jsParser,
jvmParser,
phpunitParser,
playwrightParser,
pytestParser,
recordsParser,
rubyParser,
swiftParser,
unittestParser,
zigParser,
};
/** Runs several parsers side by side (unknown tool: e.g. `make test`). */
export function combinedParser(parsers: TestOutputParser[]): TestOutputParser {
return {
feed: (line) => parsers.flatMap((p) => p.feed(line)),
end: () => parsers.flatMap((p) => p.end?.() ?? []),
};
}
export type Framework =
| "cargo"
| "js"
| "playwright"
| "cypress"
| "node-test"
| "mocha"
| "jasmine"
| "pytest"
/** Django's `manage.py test`: unittest's text runner. */
| "unittest"
/** `python -m unittest`: the IDE's reporter. */
| "python-unittest"
/** `python -m doctest`: the IDE's reporter. */
| "python-doctest"
| "phpunit"
| "go"
| "jvm"
| "ruby"
| "dotnet"
| "zig"
/** `swift test`: XCTest and Swift Testing. */
| "swift"
| "auto";
type FrameworkConfig = Pick<RunConfig, "kind" | "command" | "args"> &
Partial<Pick<RunConfig, "testRunner" | "env" | "watch">>;
export function frameworkFor(config: FrameworkConfig): Framework {
const cmd = [config.command, ...config.args].join(" ");
// A `package.json` script names its runner only in package.json: detection records it.
if (config.testRunner === "playwright" || /\bplaywright\s+test\b/.test(cmd)) return "playwright";
if (config.kind === "cypress" || config.testRunner === "cypress" || /\bcypress\s+run\b/.test(cmd)) return "cypress";
if (config.testRunner === "node-test" || /(^|[\s/\\])(node|tsx)(\.exe)?(\s+-\S*)*\s+--test(\s|$)/.test(cmd))
return "node-test";
if (config.testRunner === "mocha" || /(^|[\s/\\])_?mocha(\.cmd|\.js)?(\s|$)/.test(cmd)) return "mocha";
if (config.testRunner === "jasmine" || /(^|[\s/\\])jasmine(\.cmd|\.js)?(\s|$)/.test(cmd)) return "jasmine";
if (config.testRunner === "jvm" || isJunitRunner(config)) return "jvm";
switch (config.kind) {
case "dotnet-test":
return "dotnet";
case "cargo":
return "cargo";
case "npm":
case "deno":
case "bun":
return "js";
case "pytest":
return "pytest";
case "unittest":
return "python-unittest";
case "doctest":
return "python-doctest";
case "python":
// `manage.py test` made as a Python configuration before the Django tests type existed.
if (/\bmanage\.py test\b/.test(cmd)) return "unittest";
if (/(^|\s)-m doctest(\s|$)/.test(cmd)) return "python-doctest";
return /(^|\s)-m unittest(\s|$)/.test(cmd) ? "python-unittest" : "pytest";
case "django-test":
return "unittest";
case "phpunit":
return "phpunit";
case "go":
return "go";
case "gradle":
case "maven":
return "jvm";
case "ruby":
case "rake":
case "rspec":
return "ruby";
case "zig":
return "zig";
}
if (/(^|[/\\])dotnet(?:\.exe)?$/i.test(config.command) && config.args[0] === "test") return "dotnet";
if (/\bcargo\b/.test(cmd)) return "cargo";
if (/\b(jest|vitest|npm|pnpm|yarn|bun|npx|deno)\b/.test(cmd)) return "js";
if (/\bpytest\b/.test(cmd)) return "pytest";
if (/\bmanage\.py test\b/.test(cmd)) return "unittest";
if (/(^|[/\\\s])(phpunit|pest)(\.phar|\.bat)?(\s|$)/.test(cmd)) return "phpunit";
if (/\bgo\s+test\b/.test(cmd)) return "go";
if (/(^|[/\\\s])swift(\.exe)?\s+test(\s|$)/.test(cmd)) return "swift";
if (/\b(gradlew?|mvnw?)\b/.test(cmd)) return "jvm";
return "auto";
}
/** `TESTOPTS=-v` / a trailing `-v`: Minitest prints a line per test only when verbose. */
function rubyVerbose<T extends Pick<RunConfig, "kind" | "command" | "args">>(config: T): T {
if (config.kind === "rspec") return config;
if (config.kind === "rake") {
const at = config.args.findIndex((a) => a.startsWith("TESTOPTS="));
if (at < 0) return { ...config, args: [...config.args, "TESTOPTS=-v"] };
// Options already given (`--name=Class#test_x`): add ours unless verbosity was chosen.
return /(^|\s)(-v|--verbose)(\s|$)/.test(config.args[at]!)
? config
: { ...config, args: config.args.map((a, i) => (i === at ? `${a} -v` : a)) };
}
const chosen = config.args.some((a) => a === "-v" || a === "--verbose");
return chosen ? config : { ...config, args: [...config.args, "-v"] };
}
/**
* Launch arguments that make a test run reportable per test: pytest and
* Django print only progress dots by default, so passed tests would never
* reach the tree. `-v` (Django: `-v 2`) adds one line per test (live, like
* IDEA's runner) unless the user chose a verbosity already.
*/
export function withTestReporting<T extends FrameworkConfig>(config: T): T {
if (frameworkFor(config) === "node-test") return { ...config, env: withNodeTestReporter(config.env ?? {}) };
if (frameworkFor(config) === "mocha") return { ...config, env: withMochaHook(config.env ?? {}) };
if (frameworkFor(config) === "jasmine") return { ...config, env: withJasmineHook(config.env ?? {}) };
if (frameworkFor(config) === "python-unittest") return withUnittestReporter(config);
if (frameworkFor(config) === "python-doctest") return withDoctestReporter(config);
if (frameworkFor(config) === "playwright") {
// The list reporter prints a line per test; a project's own (`html`, `dot` on CI) may print none.
const chosen = config.args.some((a) => a === "--reporter" || a.startsWith("--reporter="));
return chosen ? config : { ...config, args: appendScriptArgs(config.command, config.args, ["--reporter=list"]) };
}
if (frameworkFor(config) === "cypress") {
// Service messages per test; the project's own reporter (`spec`, `mochawesome`…) prints none the tree can read.
const chosen = config.args.some((a) => a === "--reporter" || a === "-r" || a.startsWith("--reporter="));
return chosen
? config
: { ...config, args: appendScriptArgs(config.command, config.args, ["--reporter", "teamcity"]) };
}
if (frameworkFor(config) === "go" && config.args[0] === "test") {
const boundary = config.args.findIndex((a) => a === "-args" || a === "--");
const tool = boundary < 0 ? config.args : config.args.slice(0, boundary);
const args = tool.filter((a) => a !== "-json" && !a.startsWith("-json="));
return {
...config,
args: ["test", "-json", ...args.slice(1), ...(boundary < 0 ? [] : config.args.slice(boundary))],
};
}
if (frameworkFor(config) === "phpunit") return { ...config, args: withTeamcity(config.args) };
if (frameworkFor(config) === "zig" && config.args[0] === "build") {
// `zig build test` prints nothing for a passing (or cached) run; the summary reports the counts.
return config.args.includes("--summary") ? config : { ...config, args: [...config.args, "--summary", "all"] };
}
if (frameworkFor(config) === "swift") return { ...config, args: withSwiftEventStream(config.args, isWindows()) };
if (frameworkFor(config) === "unittest") {
const chosen = config.args.some((a) => /^(-v|--verbosity)(=?\d)?$/.test(a));
return chosen ? config : { ...config, args: [...config.args, "-v", "2"] };
}
if (frameworkFor(config) === "ruby") return rubyVerbose(config);
if (frameworkFor(config) !== "pytest") return config;
const cmd = [config.command, ...config.args];
const pytestAt = cmd.findIndex((a) => /(^|[/\\])pytest(\.exe)?$/.test(a));
if (pytestAt < 0) return config;
const chosen = cmd.slice(pytestAt + 1).some((a) => /^-[vq]+$|^--(verbose|quiet)$|^--verbosity/.test(a));
return chosen ? config : { ...config, args: [...config.args, "-v"] };
}
export function createTestParser(config: FrameworkConfig): TestOutputParser {
switch (frameworkFor(config)) {
case "dotnet":
return { feed: () => [] };
case "cargo":
return cargoParser();
case "js":
return jsParser(!!config.watch);
case "playwright":
return playwrightParser();
case "cypress":
return cypressParser(cypressSpecs(config.args));
case "node-test":
case "mocha":
case "jasmine":
case "python-unittest":
case "python-doctest":
return recordsParser();
case "pytest":
return pytestParser();
case "unittest":
return unittestParser();
case "phpunit":
return phpunitParser();
case "go":
return goParser();
case "jvm":
return jvmParser();
case "ruby":
return rubyParser();
case "zig":
return zigParser();
case "swift":
return swiftParser();
default:
return combinedParser([
cargoParser(),
jsParser(),
pytestParser(),
unittestParser(),
phpunitParser(),
goParser(),
jvmParser(),
rubyParser(),
]);
}
}
/** Convenience for tests and batch parsing. */
export function parseAll(parser: TestOutputParser, text: string): TestEvent[] {
const events = text.split("\n").flatMap((l) => parser.feed(l.replace(/\r$/, "")));
return [...events, ...(parser.end?.() ?? [])];
}