|
1 | 1 | import process from "node:process"; |
2 | | -import { Command } from "commander"; |
| 2 | +import { Command, CommanderError } from "commander"; |
3 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
4 | 4 | import { buildProgram } from "./build-program.js"; |
5 | 5 | import type { ProgramContext } from "./context.js"; |
@@ -31,14 +31,26 @@ vi.mock("./program-context.js", () => ({ |
31 | 31 | })); |
32 | 32 |
|
33 | 33 | describe("buildProgram", () => { |
34 | | - function mockProcessExit() { |
35 | | - return vi.spyOn(process, "exit").mockImplementation(((code?: number) => { |
36 | | - throw new Error(`process.exit:${String(code)}`); |
37 | | - }) as typeof process.exit); |
| 34 | + function mockProcessOutput() { |
| 35 | + vi.spyOn(process.stdout, "write").mockImplementation( |
| 36 | + ((() => true) as unknown) as typeof process.stdout.write, |
| 37 | + ); |
| 38 | + vi.spyOn(process.stderr, "write").mockImplementation( |
| 39 | + ((() => true) as unknown) as typeof process.stderr.write, |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + async function expectCommanderExit(promise: Promise<unknown>, exitCode: number) { |
| 44 | + const error = await promise.catch((err) => err); |
| 45 | + |
| 46 | + expect(error).toBeInstanceOf(CommanderError); |
| 47 | + expect(error).toMatchObject({ exitCode }); |
| 48 | + return error as CommanderError; |
38 | 49 | } |
39 | 50 |
|
40 | 51 | beforeEach(() => { |
41 | 52 | vi.clearAllMocks(); |
| 53 | + mockProcessOutput(); |
42 | 54 | createProgramContextMock.mockReturnValue({ |
43 | 55 | programVersion: "9.9.9-test", |
44 | 56 | channelOptions: ["telegram"], |
@@ -72,62 +84,57 @@ describe("buildProgram", () => { |
72 | 84 |
|
73 | 85 | it("sets exitCode to 1 on argument errors (fixes #60905)", async () => { |
74 | 86 | const program = buildProgram(); |
75 | | - const exitSpy = mockProcessExit(); |
76 | 87 | program.command("test").description("Test command"); |
77 | 88 |
|
78 | | - await expect(program.parseAsync(["test", "unexpected-arg"], { from: "user" })).rejects.toThrow( |
79 | | - "process.exit:1", |
| 89 | + const error = await expectCommanderExit( |
| 90 | + program.parseAsync(["test", "unexpected-arg"], { from: "user" }), |
| 91 | + 1, |
80 | 92 | ); |
81 | | - expect(exitSpy).toHaveBeenCalledWith(1); |
| 93 | + |
| 94 | + expect(error.code).toBe("commander.excessArguments"); |
82 | 95 | expect(process.exitCode).toBe(1); |
83 | 96 | }); |
84 | 97 |
|
85 | 98 | it("does not run the command action after an argument error", async () => { |
86 | 99 | const program = buildProgram(); |
87 | | - const exitSpy = mockProcessExit(); |
88 | 100 | const actionSpy = vi.fn(); |
89 | 101 | program.command("test").action(actionSpy); |
90 | 102 |
|
91 | | - await expect(program.parseAsync(["test", "unexpected-arg"], { from: "user" })).rejects.toThrow( |
92 | | - "process.exit:1", |
93 | | - ); |
94 | | - expect(exitSpy).toHaveBeenCalledWith(1); |
| 103 | + await expectCommanderExit(program.parseAsync(["test", "unexpected-arg"], { from: "user" }), 1); |
| 104 | + |
95 | 105 | expect(actionSpy).not.toHaveBeenCalled(); |
96 | 106 | }); |
97 | 107 |
|
98 | 108 | it("preserves exitCode 0 for help display", async () => { |
99 | 109 | const program = buildProgram(); |
100 | | - const exitSpy = mockProcessExit(); |
101 | 110 | program.command("test").description("Test command"); |
102 | 111 |
|
103 | | - await expect(program.parseAsync(["--help"], { from: "user" })).rejects.toThrow( |
104 | | - "process.exit:0", |
105 | | - ); |
106 | | - expect(exitSpy).toHaveBeenCalledWith(0); |
| 112 | + const error = await expectCommanderExit(program.parseAsync(["--help"], { from: "user" }), 0); |
| 113 | + |
| 114 | + expect(error.code).toBe("commander.helpDisplayed"); |
107 | 115 | expect(process.exitCode).toBe(0); |
108 | 116 | }); |
109 | 117 |
|
110 | 118 | it("preserves exitCode 0 for version display", async () => { |
111 | 119 | const program = buildProgram(); |
112 | | - const exitSpy = mockProcessExit(); |
113 | 120 | program.version("1.0.0"); |
114 | 121 |
|
115 | | - await expect(program.parseAsync(["--version"], { from: "user" })).rejects.toThrow( |
116 | | - "process.exit:0", |
117 | | - ); |
118 | | - expect(exitSpy).toHaveBeenCalledWith(0); |
| 122 | + const error = await expectCommanderExit(program.parseAsync(["--version"], { from: "user" }), 0); |
| 123 | + |
| 124 | + expect(error.code).toBe("commander.version"); |
119 | 125 | expect(process.exitCode).toBe(0); |
120 | 126 | }); |
121 | 127 |
|
122 | 128 | it("preserves non-zero exitCode for help error flows", async () => { |
123 | 129 | const program = buildProgram(); |
124 | | - const exitSpy = mockProcessExit(); |
125 | 130 | program.helpCommand("help [command]"); |
126 | 131 |
|
127 | | - await expect(program.parseAsync(["help", "missing"], { from: "user" })).rejects.toThrow( |
128 | | - "process.exit:1", |
| 132 | + const error = await expectCommanderExit( |
| 133 | + program.parseAsync(["help", "missing"], { from: "user" }), |
| 134 | + 1, |
129 | 135 | ); |
130 | | - expect(exitSpy).toHaveBeenCalledWith(1); |
| 136 | + |
| 137 | + expect(error.code).toBe("commander.help"); |
131 | 138 | expect(process.exitCode).toBe(1); |
132 | 139 | }); |
133 | 140 | }); |
0 commit comments