























@@ -593,7 +593,7 @@ exit 0
593593it("spawns the script as a detached process on Linux", async () => {
594594Object.defineProperty(process, "platform", { value: "linux" });
595595const scriptPath = "/tmp/fake-script.sh";
596-const mockChild = { unref: vi.fn() };
596+const mockChild = { on: vi.fn(), unref: vi.fn() };
597597vi.mocked(spawn).mockReturnValue(mockChild as unknown as ChildProcess);
598598599599await runRestartScript(scriptPath);
@@ -603,13 +603,14 @@ exit 0
603603stdio: "ignore",
604604windowsHide: true,
605605});
606+expect(mockChild.on).toHaveBeenCalledWith("error", expect.any(Function));
606607expect(mockChild.unref).toHaveBeenCalledTimes(1);
607608});
608609609610it("uses cmd.exe on Windows", async () => {
610611Object.defineProperty(process, "platform", { value: "win32" });
611612const scriptPath = "C:\\Temp\\fake-script.bat";
612-const mockChild = { unref: vi.fn() };
613+const mockChild = { on: vi.fn(), unref: vi.fn() };
613614vi.mocked(spawn).mockReturnValue(mockChild as unknown as ChildProcess);
614615615616await runRestartScript(scriptPath);
@@ -619,13 +620,14 @@ exit 0
619620stdio: "ignore",
620621windowsHide: true,
621622});
623+expect(mockChild.on).toHaveBeenCalledWith("error", expect.any(Function));
622624expect(mockChild.unref).toHaveBeenCalledTimes(1);
623625});
624626625627it("quotes cmd.exe /c paths with metacharacters on Windows", async () => {
626628Object.defineProperty(process, "platform", { value: "win32" });
627629const scriptPath = "C:\\Temp\\me&(ow)\\fake-script.bat";
628-const mockChild = { unref: vi.fn() };
630+const mockChild = { on: vi.fn(), unref: vi.fn() };
629631vi.mocked(spawn).mockReturnValue(mockChild as unknown as ChildProcess);
630632631633await runRestartScript(scriptPath);
@@ -636,5 +638,33 @@ exit 0
636638windowsHide: true,
637639});
638640});
641+642+it("does not throw when spawn fails synchronously", async () => {
643+Object.defineProperty(process, "platform", { value: "linux" });
644+vi.mocked(spawn).mockImplementation(() => {
645+throw Object.assign(new Error("spawn /bin/sh ENOENT"), { code: "ENOENT" });
646+});
647+648+await expect(runRestartScript("/tmp/fake-script.sh")).resolves.toBeUndefined();
649+});
650+651+it("handles child process spawn errors after the detached handoff", async () => {
652+Object.defineProperty(process, "platform", { value: "linux" });
653+let errorHandler: ((error: Error) => void) | undefined;
654+const mockChild = {
655+on: vi.fn((event: string, handler: (error: Error) => void) => {
656+if (event === "error") {
657+errorHandler = handler;
658+}
659+return mockChild;
660+}),
661+unref: vi.fn(),
662+};
663+vi.mocked(spawn).mockReturnValue(mockChild as unknown as ChildProcess);
664+665+await runRestartScript("/tmp/fake-script.sh");
666+expect(errorHandler).toBeDefined();
667+expect(() => errorHandler?.(new Error("spawn /bin/sh ENOENT"))).not.toThrow();
668+});
639669});
640670});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。