forked from tj/commander.js
/
command.exitOverride.test.js
372 lines (310 loc) · 11 KB
/
command.exitOverride.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
const commander = require('../');
const path = require('path');
// Test details of the exitOverride errors.
// The important checks are the exitCode and code which are intended to be stable for
// semver minor versions. For now, also testing the error.message and that output occurred
// to detect accidental changes in behaviour.
/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectCommanderError"] }] */
function expectCommanderError(err, exitCode, code, message) {
expect(err).toBeInstanceOf(commander.CommanderError);
expect(err.exitCode).toBe(exitCode);
expect(err.code).toBe(code);
expect(err.message).toBe(message);
}
describe('.exitOverride and error details', () => {
// Use internal knowledge to suppress output to keep test output clean.
let stderrSpy;
beforeAll(() => {
stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });
});
afterEach(() => {
stderrSpy.mockClear();
});
afterAll(() => {
stderrSpy.mockRestore();
});
test('when specify unknown program option then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride();
let caughtErr;
try {
program.parse(['node', 'test', '-m']);
} catch (err) {
caughtErr = err;
}
expect(stderrSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.unknownOption', "error: unknown option '-m'");
});
test('when specify unknown command then throw CommanderError', () => {
const program = new commander.Command();
program
.name('prog')
.exitOverride()
.command('sub');
let caughtErr;
try {
program.parse(['node', 'test', 'oops']);
} catch (err) {
caughtErr = err;
}
expect(stderrSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.unknownCommand', "error: unknown command 'oops'");
});
// Same error as above, but with custom handler.
test('when supply custom handler then throw custom error', () => {
const customError = new commander.CommanderError(123, 'custom-code', 'custom-message');
const program = new commander.Command();
program
.exitOverride((_err) => {
throw customError;
});
let caughtErr;
try {
program.parse(['node', 'test', '-m']);
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, customError.exitCode, customError.code, customError.message);
});
test('when specify option without required value then throw CommanderError', () => {
const optionFlags = '-p, --pepper <type>';
const program = new commander.Command();
program
.exitOverride()
.option(optionFlags, 'add pepper');
let caughtErr;
try {
program.parse(['node', 'test', '--pepper']);
} catch (err) {
caughtErr = err;
}
expect(stderrSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.optionMissingArgument', `error: option '${optionFlags}' argument missing`);
});
test('when specify command without required argument then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride()
.command('compress <arg-name>')
.action(() => { });
let caughtErr;
try {
program.parse(['node', 'test', 'compress']);
} catch (err) {
caughtErr = err;
}
expect(stderrSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");
});
test('when specify program without required argument and no action handler then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride()
.argument('<arg-name>');
let caughtErr;
try {
program.parse(['node', 'test']);
} catch (err) {
caughtErr = err;
}
expect(stderrSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");
});
test('when specify excess argument then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride()
.allowExcessArguments(false)
.action(() => { });
let caughtErr;
try {
program.parse(['node', 'test', 'excess']);
} catch (err) {
caughtErr = err;
}
expect(stderrSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.excessArguments', 'error: too many arguments. Expected 0 arguments but got 1.');
});
test('when specify command with excess argument then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride()
.command('speak')
.allowExcessArguments(false)
.action(() => { });
let caughtErr;
try {
program.parse(['node', 'test', 'speak', 'excess']);
} catch (err) {
caughtErr = err;
}
expect(stderrSpy).toHaveBeenCalled();
expectCommanderError(caughtErr, 1, 'commander.excessArguments', "error: too many arguments for 'speak'. Expected 0 arguments but got 1.");
});
test('when specify --help then throw CommanderError', () => {
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
const program = new commander.Command();
program
.exitOverride();
let caughtErr;
try {
program.parse(['node', 'test', '--help']);
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');
writeSpy.mockRestore();
});
test('when executable subcommand and no command specified then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride()
.command('compress', 'compress description');
let caughtErr;
try {
program.parse(['node', 'test']);
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, 1, 'commander.help', '(outputHelp)');
});
test('when specify --version then throw CommanderError', () => {
const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
const myVersion = '1.2.3';
const program = new commander.Command();
program
.exitOverride()
.version(myVersion);
let caughtErr;
try {
program.parse(['node', 'test', '--version']);
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, 0, 'commander.version', myVersion);
stdoutSpy.mockRestore();
});
test('when executableSubcommand succeeds then call exitOverride', async() => {
expect.hasAssertions();
const pm = path.join(__dirname, 'fixtures/pm');
const program = new commander.Command();
await new Promise((resolve) => {
program
.exitOverride((err) => {
expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');
resolve();
})
.command('silent', 'description');
program.parse(['node', pm, 'silent']);
});
});
test('when mandatory program option missing then throw CommanderError', () => {
const optionFlags = '-p, --pepper <type>';
const program = new commander.Command();
program
.exitOverride()
.requiredOption(optionFlags, 'add pepper');
let caughtErr;
try {
program.parse(['node', 'test']);
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, 1, 'commander.missingMandatoryOptionValue', `error: required option '${optionFlags}' not specified`);
});
test('when option argument not in choices then throw CommanderError', () => {
const optionFlags = '--colour <shade>';
const program = new commander.Command();
program
.exitOverride()
.addOption(new commander.Option(optionFlags).choices(['red', 'blue']));
let caughtErr;
try {
program.parse(['--colour', 'green'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, 1, 'commander.invalidArgument', "error: option '--colour <shade>' argument 'green' is invalid. Allowed choices are red, blue.");
});
test('when command argument not in choices then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride()
.addArgument(new commander.Argument('<shade>').choices(['red', 'blue']))
.action(() => {});
let caughtErr;
try {
program.parse(['green'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, 1, 'commander.invalidArgument', "error: command-argument value 'green' is invalid for argument 'shade'. Allowed choices are red, blue.");
});
test('when custom processing for option throws InvalidArgumentError then catch CommanderError', () => {
function justSayNo(value) {
throw new commander.InvalidArgumentError('NO');
}
const optionFlags = '--colour <shade>';
const program = new commander.Command();
program
.exitOverride()
.option(optionFlags, 'specify shade', justSayNo);
let caughtErr;
try {
program.parse(['--colour', 'green'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, 1, 'commander.invalidArgument', "error: option '--colour <shade>' argument 'green' is invalid. NO");
});
test('when custom processing for argument throws InvalidArgumentError then catch CommanderError', () => {
function justSayNo(value) {
throw new commander.InvalidArgumentError('NO');
}
const program = new commander.Command();
program
.exitOverride()
.argument('[n]', 'number', justSayNo)
.action(() => {});
let caughtErr;
try {
program.parse(['green'], { from: 'user' });
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, 1, 'commander.invalidArgument', "error: command-argument value 'green' is invalid for argument 'n'. NO");
});
test('when call error() then throw CommanderError', () => {
const program = new commander.Command();
program
.exitOverride();
let caughtErr;
try {
program.error('message');
} catch (err) {
caughtErr = err;
}
expectCommanderError(caughtErr, 1, 'commander.error', 'message');
});
});
test('when no override and error then exit(1)', () => {
const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { });
const program = new commander.Command();
program.configureOutput({ outputError: () => {} });
program.parse(['--unknownOption'], { from: 'user' });
expect(exitSpy).toHaveBeenCalledWith(1);
exitSpy.mockRestore();
});
test('when custom processing throws custom error then throw custom error', () => {
function justSayNo(value) {
throw new Error('custom');
}
const program = new commander.Command();
program
.exitOverride()
.option('-s, --shade <value>', 'specify shade', justSayNo);
expect(() => {
program.parse(['--shade', 'green'], { from: 'user' });
}).toThrow('custom');
});