

























11// Builds grouped Vitest duration reports or compares two grouped reports.
2-import { spawn } from "node:child_process";
2+import { spawn, spawnSync } from "node:child_process";
33import fs from "node:fs";
44import os from "node:os";
55import path from "node:path";
@@ -232,6 +232,41 @@ function formatSpawnError(error) {
232232return error instanceof Error ? error.message : String(error);
233233}
234234235+export function signalTestGroupReportChild(
236+child,
237+signal,
238+{
239+ appendDiagnostic = () => {},
240+ platform = process.platform,
241+ runTaskkill = spawnSync,
242+ useProcessGroup = platform !== "win32",
243+} = {},
244+) {
245+if (useProcessGroup && typeof child.pid === "number") {
246+try {
247+process.kill(-child.pid, signal);
248+return;
249+} catch (error) {
250+if (error && error.code !== "ESRCH") {
251+appendDiagnostic(
252+`[test-group-report] failed to send ${signal} to process group: ${formatSpawnError(error)}\n`,
253+);
254+}
255+}
256+}
257+if (platform === "win32" && typeof child.pid === "number") {
258+const args = ["/PID", String(child.pid), "/T"];
259+if (signal === "SIGKILL") {
260+args.push("/F");
261+}
262+const result = runTaskkill("taskkill", args, { stdio: "ignore" });
263+if (!result?.error && result?.status === 0) {
264+return;
265+}
266+}
267+child.kill(signal);
268+}
269+235270/**
236271 * Runs a command, captures text output, and terminates timed-out process groups.
237272 */
@@ -267,21 +302,8 @@ export function spawnText(command, args, options) {
267302let killGraceMessage = null;
268303let childClosedResult = null;
269304let waitingForKillGrace = false;
270-const signalChild = (signal) => {
271-if (useProcessGroup && typeof child.pid === "number") {
272-try {
273-process.kill(-child.pid, signal);
274-return;
275-} catch (error) {
276-if (error && error.code !== "ESRCH") {
277-appendDiagnostic(
278-`[test-group-report] failed to send ${signal} to process group: ${formatSpawnError(error)}\n`,
279-);
280-}
281-}
282-}
283-child.kill(signal);
284-};
305+const signalChild = (signal) =>
306+signalTestGroupReportChild(child, signal, { appendDiagnostic, useProcessGroup });
285307const parentSignalHandlers = [];
286308const cleanupParentSignalHandlers = () => {
287309for (const { signal, handler } of parentSignalHandlers) {
@@ -303,6 +325,9 @@ export function spawnText(command, args, options) {
303325relayParentSignal("SIGINT");
304326relayParentSignal("SIGTERM");
305327relayParentSignal("SIGHUP");
328+} else if (process.platform === "win32") {
329+relayParentSignal("SIGINT");
330+relayParentSignal("SIGTERM");
306331}
307332const processGroupIsAlive = () => {
308333if (!useProcessGroup || typeof child.pid !== "number") {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。