Skip to content
Myr

command.exitOverride.test.js

This file

Path

tests/command.exitOverride.test.js

460 lines · 11.9 KiB · JavaScript

Last changed by

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

History of this file

Take it

The walk stopped after 200 commits: lines older than 2911e0e point at it.

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 1–22why, one step back
1const commander = require('../');
2const path = require('path');34// Test details of the exitOverride errors.5// The important checks are the exitCode and code which are intended to be stable for6// semver minor versions. For now, also testing the error.message and that output occurred7// to detect accidental changes in behaviour.89/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectCommanderError"] }] */1011function expectCommanderError(err, exitCode, code, message) {12 expect(err).toBeInstanceOf(commander.CommanderError);13 expect(err.exitCode).toBe(exitCode);14 expect(err.code).toBe(code);15 expect(err.message).toBe(message);16}1718describe('.exitOverride and error details', () => {19 // Use internal knowledge to suppress output to keep test output clean.20 let stderrSpy;2122 beforeAll(() => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 23–25why, one step back
23 stderrSpy = jest
24 .spyOn(process.stderr, 'write')25 .mockImplementation(() => {});

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 26–37why, one step back
26 });
2728 afterEach(() => {29 stderrSpy.mockClear();30 });3132 afterAll(() => {33 stderrSpy.mockRestore();34 });3536 test('when specify unknown program option then throw CommanderError', () => {37 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 38why, one step back
38 program.exitOverride();

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 39–47why, one step back
39
40 let caughtErr;41 try {42 program.parse(['node', 'test', '-m']);43 } catch (err) {44 caughtErr = err;45 }4647 expect(stderrSpy).toHaveBeenCalled();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 48–53why, one step back
48 expectCommanderError(
49 caughtErr,50 1,51 'commander.unknownOption',52 "error: unknown option '-m'",53 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 54–57why, one step back
54 });
5556 test('when specify unknown command then throw CommanderError', () => {57 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 58why, one step back
58 program.name('prog').exitOverride().command('sub');

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 59–67why, one step back
59
60 let caughtErr;61 try {62 program.parse(['node', 'test', 'oops']);63 } catch (err) {64 caughtErr = err;65 }6667 expect(stderrSpy).toHaveBeenCalled();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 68–73why, one step back
68 expectCommanderError(
69 caughtErr,70 1,71 'commander.unknownCommand',72 "error: unknown command 'oops'",73 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 74–77why, one step back
74 });
7576 // Same error as above, but with custom handler.77 test('when supply custom handler then throw custom error', () => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 78–82why, one step back
78 const customError = new commander.CommanderError(
79 123,80 'custom-code',81 'custom-message',82 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

line 83why, one step back
83 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 84–86why, one step back
84 program.exitOverride((_err) => {
85 throw customError;86 });

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 87–94why, one step back
87
88 let caughtErr;89 try {90 program.parse(['node', 'test', '-m']);91 } catch (err) {92 caughtErr = err;93 }94

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 95–100why, one step back
95 expectCommanderError(
96 caughtErr,97 customError.exitCode,98 customError.code,99 customError.message,100 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 101–105why, one step back
101 });
102103 test('when specify option without required value then throw CommanderError', () => {104 const optionFlags = '-p, --pepper <type>';105 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 106why, one step back
106 program.exitOverride().option(optionFlags, 'add pepper');

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 107–115why, one step back
107
108 let caughtErr;109 try {110 program.parse(['node', 'test', '--pepper']);111 } catch (err) {112 caughtErr = err;113 }114115 expect(stderrSpy).toHaveBeenCalled();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 116–121why, one step back
116 expectCommanderError(
117 caughtErr,118 1,119 'commander.optionMissingArgument',120 `error: option '${optionFlags}' argument missing`,121 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 122–128why, one step back
122 });
123124 test('when specify command without required argument then throw CommanderError', () => {125 const program = new commander.Command();126 program127 .exitOverride()128 .command('compress <arg-name>')

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 129why, one step back
129 .action(() => {});

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 130–138why, one step back
130
131 let caughtErr;132 try {133 program.parse(['node', 'test', 'compress']);134 } catch (err) {135 caughtErr = err;136 }137138 expect(stderrSpy).toHaveBeenCalled();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 139–144why, one step back
139 expectCommanderError(
140 caughtErr,141 1,142 'commander.missingArgument',143 "error: missing required argument 'arg-name'",144 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 145–148why, one step back
145 });
146147 test('when specify program without required argument and no action handler then throw CommanderError', () => {148 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 149why, one step back
149 program.exitOverride().argument('<arg-name>');

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 150–158why, one step back
150
151 let caughtErr;152 try {153 program.parse(['node', 'test']);154 } catch (err) {155 caughtErr = err;156 }157158 expect(stderrSpy).toHaveBeenCalled();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 159–164why, one step back
159 expectCommanderError(
160 caughtErr,161 1,162 'commander.missingArgument',163 "error: missing required argument 'arg-name'",164 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 165–171why, one step back
165 });
166167 test('when specify excess argument then throw CommanderError', () => {168 const program = new commander.Command();169 program170 .exitOverride()171 .allowExcessArguments(false)

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 172why, one step back
172 .action(() => {});

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 173–181why, one step back
173
174 let caughtErr;175 try {176 program.parse(['node', 'test', 'excess']);177 } catch (err) {178 caughtErr = err;179 }180181 expect(stderrSpy).toHaveBeenCalled();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 182–187why, one step back
182 expectCommanderError(
183 caughtErr,184 1,185 'commander.excessArguments',186 'error: too many arguments. Expected 0 arguments but got 1.',187 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 188–195why, one step back
188 });
189190 test('when specify command with excess argument then throw CommanderError', () => {191 const program = new commander.Command();192 program193 .exitOverride()194 .command('speak')195 .allowExcessArguments(false)

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 196why, one step back
196 .action(() => {});

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 197–205why, one step back
197
198 let caughtErr;199 try {200 program.parse(['node', 'test', 'speak', 'excess']);201 } catch (err) {202 caughtErr = err;203 }204205 expect(stderrSpy).toHaveBeenCalled();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 206–211why, one step back
206 expectCommanderError(
207 caughtErr,208 1,209 'commander.excessArguments',210 "error: too many arguments for 'speak'. Expected 0 arguments but got 1.",211 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 212–214why, one step back
212 });
213214 test('when specify --help then throw CommanderError', () => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 215–217why, one step back
215 const writeSpy = jest
216 .spyOn(process.stdout, 'write')217 .mockImplementation(() => {});

docs: update SECURITY.mdabetomobefore adoption2911e0e

line 218why, one step back
218 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 219why, one step back
219 program.exitOverride();

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 220–227why, one step back
220
221 let caughtErr;222 try {223 program.parse(['node', 'test', '--help']);224 } catch (err) {225 caughtErr = err;226 }227

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 228–233why, one step back
228 expectCommanderError(
229 caughtErr,230 0,231 'commander.helpDisplayed',232 '(outputHelp)',233 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 234–238why, one step back
234 writeSpy.mockRestore();
235 });236237 test('when executable subcommand and no command specified then throw CommanderError', () => {238 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 239why, one step back
239 program.exitOverride().command('compress', 'compress description');

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 240–251why, one step back
240
241 let caughtErr;242 try {243 program.parse(['node', 'test']);244 } catch (err) {245 caughtErr = err;246 }247248 expectCommanderError(caughtErr, 1, 'commander.help', '(outputHelp)');249 });250251 test('when specify --version then throw CommanderError', () => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 252–254why, one step back
252 const stdoutSpy = jest
253 .spyOn(process.stdout, 'write')254 .mockImplementation(() => {});

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 255–256why, one step back
255 const myVersion = '1.2.3';
256 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 257why, one step back
257 program.exitOverride().version(myVersion);

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 258–269why, one step back
258
259 let caughtErr;260 try {261 program.parse(['node', 'test', '--version']);262 } catch (err) {263 caughtErr = err;264 }265266 expectCommanderError(caughtErr, 0, 'commander.version', myVersion);267 stdoutSpy.mockRestore();268 });269

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 270why, one step back
270 test('when executableSubcommand succeeds then call exitOverride', async () => {

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 271–276why, one step back
271 expect.hasAssertions();
272 const pm = path.join(__dirname, 'fixtures/pm');273 const program = new commander.Command();274 await new Promise((resolve) => {275 program276 .exitOverride((err) => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 277–282why, one step back
277 expectCommanderError(
278 err,279 0,280 'commander.executeSubCommandAsync',281 '(close)',282 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 283–292why, one step back
283 resolve();
284 })285 .command('silent', 'description');286 program.parse(['node', pm, 'silent']);287 });288 });289290 test('when mandatory program option missing then throw CommanderError', () => {291 const optionFlags = '-p, --pepper <type>';292 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 293why, one step back
293 program.exitOverride().requiredOption(optionFlags, 'add pepper');

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 294–301why, one step back
294
295 let caughtErr;296 try {297 program.parse(['node', 'test']);298 } catch (err) {299 caughtErr = err;300 }301

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 302–307why, one step back
302 expectCommanderError(
303 caughtErr,304 1,305 'commander.missingMandatoryOptionValue',306 `error: required option '${optionFlags}' not specified`,307 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 308–323why, one step back
308 });
309310 test('when option argument not in choices then throw CommanderError', () => {311 const optionFlags = '--colour <shade>';312 const program = new commander.Command();313 program314 .exitOverride()315 .addOption(new commander.Option(optionFlags).choices(['red', 'blue']));316317 let caughtErr;318 try {319 program.parse(['--colour', 'green'], { from: 'user' });320 } catch (err) {321 caughtErr = err;322 }323

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 324–329why, one step back
324 expectCommanderError(
325 caughtErr,326 1,327 'commander.invalidArgument',328 "error: option '--colour <shade>' argument 'green' is invalid. Allowed choices are red, blue.",329 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 330–345why, one step back
330 });
331332 test('when command argument not in choices then throw CommanderError', () => {333 const program = new commander.Command();334 program335 .exitOverride()336 .addArgument(new commander.Argument('<shade>').choices(['red', 'blue']))337 .action(() => {});338339 let caughtErr;340 try {341 program.parse(['green'], { from: 'user' });342 } catch (err) {343 caughtErr = err;344 }345

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 346–351why, one step back
346 expectCommanderError(
347 caughtErr,348 1,349 'commander.invalidArgument',350 "error: command-argument value 'green' is invalid for argument 'shade'. Allowed choices are red, blue.",351 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 352–359why, one step back
352 });
353354 test('when custom processing for option throws InvalidArgumentError then catch CommanderError', () => {355 function justSayNo(value) {356 throw new commander.InvalidArgumentError('NO');357 }358 const optionFlags = '--colour <shade>';359 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 360why, one step back
360 program.exitOverride().option(optionFlags, 'specify shade', justSayNo);

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 361–368why, one step back
361
362 let caughtErr;363 try {364 program.parse(['--colour', 'green'], { from: 'user' });365 } catch (err) {366 caughtErr = err;367 }368

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 369–374why, one step back
369 expectCommanderError(
370 caughtErr,371 1,372 'commander.invalidArgument',373 "error: option '--colour <shade>' argument 'green' is invalid. NO",374 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 375–393why, one step back
375 });
376377 test('when custom processing for argument throws InvalidArgumentError then catch CommanderError', () => {378 function justSayNo(value) {379 throw new commander.InvalidArgumentError('NO');380 }381 const program = new commander.Command();382 program383 .exitOverride()384 .argument('[n]', 'number', justSayNo)385 .action(() => {});386387 let caughtErr;388 try {389 program.parse(['green'], { from: 'user' });390 } catch (err) {391 caughtErr = err;392 }393

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 394–399why, one step back
394 expectCommanderError(
395 caughtErr,396 1,397 'commander.invalidArgument',398 "error: command-argument value 'green' is invalid for argument 'n'. NO",399 );

docs: update SECURITY.mdabetomobefore adoption2911e0e

line 400why, one step back
400 });

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

line 401why, one step back
401

feat(option): allow to set options as conflicting (#1678)Erez Rokahbefore adoptionfc4fd41

lines 402–415why, one step back
402 test('when has conflicting option then throw CommanderError', () => {
403 const program = new commander.Command();404 program405 .exitOverride()406 .addOption(new commander.Option('--silent'))407 .addOption(new commander.Option('--debug').conflicts(['silent']));408409 let caughtErr;410 try {411 program.parse(['--debug', '--silent'], { from: 'user' });412 } catch (err) {413 caughtErr = err;414 }415

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 416–421why, one step back
416 expectCommanderError(
417 caughtErr,418 1,419 'commander.conflictingOption',420 "error: option '--debug' cannot be used with option '--silent'",421 );

feat(option): allow to set options as conflicting (#1678)Erez Rokahbefore adoptionfc4fd41

lines 422–423why, one step back
422 });
423

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 424–425why, one step back
424 test('when call error() then throw CommanderError', () => {
425 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 426why, one step back
426 program.exitOverride();

Merge pull request #1683 from tj/release/9.xJohn Geebefore adoption02a124c

lines 427–436why, one step back
427
428 let caughtErr;429 try {430 program.error('message');431 } catch (err) {432 caughtErr = err;433 }434435 expectCommanderError(caughtErr, 1, 'commander.error', 'message');436 });

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 437–439why, one step back
437});
438439test('when no override and error then exit(1)', () => {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 440why, one step back
440 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {});

docs: update SECURITY.mdabetomobefore adoption2911e0e

lines 441–460why, one step back
441 const program = new commander.Command();
442 program.configureOutput({ outputError: () => {} });443 program.parse(['--unknownOption'], { from: 'user' });444 expect(exitSpy).toHaveBeenCalledWith(1);445 exitSpy.mockRestore();446});447448test('when custom processing throws custom error then throw custom error', () => {449 function justSayNo(value) {450 throw new Error('custom');451 }452 const program = new commander.Command();453 program454 .exitOverride()455 .option('-s, --shade <value>', 'specify shade', justSayNo);456457 expect(() => {458 program.parse(['--shade', 'green'], { from: 'user' });459 }).toThrow('custom');460});