From b22bbf5660445f2735426b7cdb7f1cf85075a6ee Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 12 Apr 2026 09:36:28 +0100 Subject: [PATCH] test(process): share shimmed windows success assertions --- src/process/exec.windows.test.ts | 55 +++++++++++++------------------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/src/process/exec.windows.test.ts b/src/process/exec.windows.test.ts index b997baae438..29909528d2f 100644 --- a/src/process/exec.windows.test.ts +++ b/src/process/exec.windows.test.ts @@ -90,6 +90,26 @@ function expectCmdWrappedInvocation(params: { expect(params.captured[2].windowsVerbatimArguments).toBe(true); } +async function expectShimmedWindowsCommandWithoutExitCodeSucceeds(params?: { killed?: boolean }) { + const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32"); + const child = createMockChild({ + closeCode: null, + exitCode: null, + }); + child.killed = params?.killed ?? false; + + spawnMock.mockImplementation(() => child); + + try { + const result = await runCommandWithTimeout(["npm", "--version"], { timeoutMs: 1000 }); + expect(result.code).toBe(0); + expect(result.signal).toBeNull(); + expect(result.termination).toBe("exit"); + } finally { + platformSpy.mockRestore(); + } +} + describe("windows command wrapper behavior", () => { beforeAll(async () => { ({ runCommandWithTimeout, runExec } = await import("./exec.js")); @@ -237,42 +257,11 @@ describe("windows command wrapper behavior", () => { }); it("treats shimmed Windows commands without a reported exit code as success when they close cleanly", async () => { - const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32"); - const child = createMockChild({ - closeCode: null, - exitCode: null, - }); - - spawnMock.mockImplementation(() => child); - - try { - const result = await runCommandWithTimeout(["npm", "--version"], { timeoutMs: 1000 }); - expect(result.code).toBe(0); - expect(result.signal).toBeNull(); - expect(result.termination).toBe("exit"); - } finally { - platformSpy.mockRestore(); - } + await expectShimmedWindowsCommandWithoutExitCodeSucceeds(); }); it("treats shimmed Windows commands without a reported exit code as success even when child.killed is true", async () => { - const platformSpy = vi.spyOn(process, "platform", "get").mockReturnValue("win32"); - const child = createMockChild({ - closeCode: null, - exitCode: null, - }); - child.killed = true; - - spawnMock.mockImplementation(() => child); - - try { - const result = await runCommandWithTimeout(["npm", "--version"], { timeoutMs: 1000 }); - expect(result.code).toBe(0); - expect(result.signal).toBeNull(); - expect(result.termination).toBe("exit"); - } finally { - platformSpy.mockRestore(); - } + await expectShimmedWindowsCommandWithoutExitCodeSucceeds({ killed: true }); }); it("uses cmd.exe wrapper with windowsVerbatimArguments in runExec for .cmd shims", async () => {