



























@@ -1,5 +1,6 @@
11import { beforeEach, describe, expect, it, vi } from "vitest";
2-import { createGlobalCommandRunner } from "./shared.js";
2+import { defaultRuntime } from "../../runtime.js";
3+import { createGlobalCommandRunner, parseTimeoutMsOrExit } from "./shared.js";
3445const runCommandWithTimeout = vi.hoisted(() => vi.fn());
56@@ -48,4 +49,41 @@ describe("createGlobalCommandRunner", () => {
4849code: 17,
4950});
5051});
52+53+it("requires timeout values to be complete positive integer seconds", () => {
54+const error = vi.spyOn(defaultRuntime, "error").mockImplementation(() => undefined);
55+const exit = vi.spyOn(defaultRuntime, "exit").mockImplementation(() => undefined as never);
56+57+try {
58+expect(parseTimeoutMsOrExit("1.5")).toBeNull();
59+expect(parseTimeoutMsOrExit("10abc")).toBeNull();
60+expect(parseTimeoutMsOrExit("0")).toBeNull();
61+expect(parseTimeoutMsOrExit("-1")).toBeNull();
62+expect(parseTimeoutMsOrExit(" ")).toBeNull();
63+64+expect(error).toHaveBeenCalledTimes(5);
65+expect(error).toHaveBeenCalledWith("--timeout must be a positive integer (seconds)");
66+expect(exit).toHaveBeenCalledTimes(5);
67+expect(exit).toHaveBeenCalledWith(1);
68+} finally {
69+error.mockRestore();
70+exit.mockRestore();
71+}
72+});
73+74+it("parses complete positive integer timeout values as milliseconds", () => {
75+const error = vi.spyOn(defaultRuntime, "error").mockImplementation(() => undefined);
76+const exit = vi.spyOn(defaultRuntime, "exit").mockImplementation(() => undefined as never);
77+78+try {
79+expect(parseTimeoutMsOrExit(" 10 ")).toBe(10_000);
80+expect(parseTimeoutMsOrExit("001")).toBe(1_000);
81+expect(parseTimeoutMsOrExit()).toBeUndefined();
82+expect(error).not.toHaveBeenCalled();
83+expect(exit).not.toHaveBeenCalled();
84+} finally {
85+error.mockRestore();
86+exit.mockRestore();
87+}
88+});
5189});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。