Skip to content
Myr

command.exitOverride.test.js

This file

Path

tests/command.exitOverride.test.js

389 lines · 11.5 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 1–19why, 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.

configure output (#1387)John Geebefore adoptioned7f13e

line 20why, one step back
20 let stderrSpy;

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 21–22why, one step back
21
22 beforeAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

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

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 24–26why, one step back
24 });
2526 afterEach(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 27why, one step back
27 stderrSpy.mockClear();

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 28–30why, one step back
28 });
2930 afterAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 31why, one step back
31 stderrSpy.mockRestore();

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 32–45why, one step back
32 });
3334 test('when specify unknown program option then throw CommanderError', () => {35 const program = new commander.Command();36 program37 .exitOverride();3839 let caughtErr;40 try {41 program.parse(['node', 'test', '-m']);42 } catch (err) {43 caughtErr = err;44 }45

configure output (#1387)John Geebefore adoptioned7f13e

line 46why, one step back
46 expect(stderrSpy).toHaveBeenCalled();

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 47–63why, one step back
47 expectCommanderError(caughtErr, 1, 'commander.unknownOption', "error: unknown option '-m'");
48 });4950 test('when specify unknown command then throw CommanderError', () => {51 const program = new commander.Command();52 program53 .name('prog')54 .exitOverride()55 .command('sub');5657 let caughtErr;58 try {59 program.parse(['node', 'test', 'oops']);60 } catch (err) {61 caughtErr = err;62 }63

configure output (#1387)John Geebefore adoptioned7f13e

line 64why, one step back
64 expect(stderrSpy).toHaveBeenCalled();

Add showHelpAfterError (#1534)John Geebefore adoption082717f

line 65why, one step back
65 expectCommanderError(caughtErr, 1, 'commander.unknownCommand', "error: unknown command 'oops'");

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 66–100why, one step back
66 });
6768 // Same error as above, but with custom handler.69 test('when supply custom handler then throw custom error', () => {70 const customError = new commander.CommanderError(123, 'custom-code', 'custom-message');71 const program = new commander.Command();72 program73 .exitOverride((_err) => {74 throw customError;75 });7677 let caughtErr;78 try {79 program.parse(['node', 'test', '-m']);80 } catch (err) {81 caughtErr = err;82 }8384 expectCommanderError(caughtErr, customError.exitCode, customError.code, customError.message);85 });8687 test('when specify option without required value then throw CommanderError', () => {88 const optionFlags = '-p, --pepper <type>';89 const program = new commander.Command();90 program91 .exitOverride()92 .option(optionFlags, 'add pepper');9394 let caughtErr;95 try {96 program.parse(['node', 'test', '--pepper']);97 } catch (err) {98 caughtErr = err;99 }100

configure output (#1387)John Geebefore adoptioned7f13e

line 101why, one step back
101 expect(stderrSpy).toHaveBeenCalled();

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 102–118why, one step back
102 expectCommanderError(caughtErr, 1, 'commander.optionMissingArgument', `error: option '${optionFlags}' argument missing`);
103 });104105 test('when specify command without required argument then throw CommanderError', () => {106 const program = new commander.Command();107 program108 .exitOverride()109 .command('compress <arg-name>')110 .action(() => { });111112 let caughtErr;113 try {114 program.parse(['node', 'test', 'compress']);115 } catch (err) {116 caughtErr = err;117 }118

configure output (#1387)John Geebefore adoptioned7f13e

line 119why, one step back
119 expect(stderrSpy).toHaveBeenCalled();

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 120–122why, one step back
120 expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");
121 });122

Check command-arguments even without action handler (#1502)John Geebefore adoption0504945

lines 123–139why, one step back
123 test('when specify program without required argument and no action handler then throw CommanderError', () => {
124 const program = new commander.Command();125 program126 .exitOverride()127 .argument('<arg-name>');128129 let caughtErr;130 try {131 program.parse(['node', 'test']);132 } catch (err) {133 caughtErr = err;134 }135136 expect(stderrSpy).toHaveBeenCalled();137 expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");138 });139

Add .allowExcessArguments() and error message (#1407)John Geebefore adoption09f277a

lines 140–176why, one step back
140 test('when specify excess argument then throw CommanderError', () => {
141 const program = new commander.Command();142 program143 .exitOverride()144 .allowExcessArguments(false)145 .action(() => { });146147 let caughtErr;148 try {149 program.parse(['node', 'test', 'excess']);150 } catch (err) {151 caughtErr = err;152 }153154 expect(stderrSpy).toHaveBeenCalled();155 expectCommanderError(caughtErr, 1, 'commander.excessArguments', 'error: too many arguments. Expected 0 arguments but got 1.');156 });157158 test('when specify command with excess argument then throw CommanderError', () => {159 const program = new commander.Command();160 program161 .exitOverride()162 .command('speak')163 .allowExcessArguments(false)164 .action(() => { });165166 let caughtErr;167 try {168 program.parse(['node', 'test', 'speak', 'excess']);169 } catch (err) {170 caughtErr = err;171 }172173 expect(stderrSpy).toHaveBeenCalled();174 expectCommanderError(caughtErr, 1, 'commander.excessArguments', "error: too many arguments for 'speak'. Expected 0 arguments but got 1.");175 });176

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

line 177why, one step back
177 test('when specify --help then throw CommanderError', () => {

Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d

line 178why, one step back
178 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 179–190why, one step back
179 const program = new commander.Command();
180 program181 .exitOverride();182183 let caughtErr;184 try {185 program.parse(['node', 'test', '--help']);186 } catch (err) {187 caughtErr = err;188 }189190 expectCommanderError(caughtErr, 0, 'commander.helpDisplayed', '(outputHelp)');

Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d

line 191why, one step back
191 writeSpy.mockRestore();

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 192–210why, one step back
192 });
193194 test('when executable subcommand and no command specified then throw CommanderError', () => {195 const program = new commander.Command();196 program197 .exitOverride()198 .command('compress', 'compress description');199200 let caughtErr;201 try {202 program.parse(['node', 'test']);203 } catch (err) {204 caughtErr = err;205 }206207 expectCommanderError(caughtErr, 1, 'commander.help', '(outputHelp)');208 });209210 test('when specify --version then throw CommanderError', () => {

configure output (#1387)John Geebefore adoptioned7f13e

line 211why, one step back
211 const stdoutSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 212–225why, one step back
212 const myVersion = '1.2.3';
213 const program = new commander.Command();214 program215 .exitOverride()216 .version(myVersion);217218 let caughtErr;219 try {220 program.parse(['node', 'test', '--version']);221 } catch (err) {222 caughtErr = err;223 }224225 expectCommanderError(caughtErr, 0, 'commander.version', myVersion);

configure output (#1387)John Geebefore adoptioned7f13e

line 226why, one step back
226 stdoutSpy.mockRestore();

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

lines 227–259why, one step back
227 });
228229 test('when executableSubcommand succeeds then call exitOverride', async() => {230 expect.hasAssertions();231 const pm = path.join(__dirname, 'fixtures/pm');232 const program = new commander.Command();233 await new Promise((resolve) => {234 program235 .exitOverride((err) => {236 expectCommanderError(err, 0, 'commander.executeSubCommandAsync', '(close)');237 resolve();238 })239 .command('silent', 'description');240 program.parse(['node', pm, 'silent']);241 });242 });243244 test('when mandatory program option missing then throw CommanderError', () => {245 const optionFlags = '-p, --pepper <type>';246 const program = new commander.Command();247 program248 .exitOverride()249 .requiredOption(optionFlags, 'add pepper');250251 let caughtErr;252 try {253 program.parse(['node', 'test']);254 } catch (err) {255 caughtErr = err;256 }257258 expectCommanderError(caughtErr, 1, 'commander.missingMandatoryOptionValue', `error: required option '${optionFlags}' not specified`);259 });

Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc

lines 260–274why, one step back
260
261 test('when option argument not in choices then throw CommanderError', () => {262 const optionFlags = '--colour <shade>';263 const program = new commander.Command();264 program265 .exitOverride()266 .addOption(new commander.Option(optionFlags).choices(['red', 'blue']));267268 let caughtErr;269 try {270 program.parse(['--colour', 'green'], { from: 'user' });271 } catch (err) {272 caughtErr = err;273 }274

Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa

line 275why, one step back
275 expectCommanderError(caughtErr, 1, 'commander.invalidArgument', "error: option '--colour <shade>' argument 'green' is invalid. Allowed choices are red, blue.");

Support custom error messages for option argument coercion failures (#1392)John Geebefore adoption689d7a0

lines 276–277why, one step back
276 });
277

Choices for arguments (#1525)John Geebefore adoption708527e

lines 278–294why, one step back
278 test('when command argument not in choices then throw CommanderError', () => {
279 const program = new commander.Command();280 program281 .exitOverride()282 .addArgument(new commander.Argument('<shade>').choices(['red', 'blue']))283 .action(() => {});284285 let caughtErr;286 try {287 program.parse(['green'], { from: 'user' });288 } catch (err) {289 caughtErr = err;290 }291292 expectCommanderError(caughtErr, 1, 'commander.invalidArgument', "error: command-argument value 'green' is invalid for argument 'shade'. Allowed choices are red, blue.");293 });294

Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa

line 295why, one step back
295 test('when custom processing for option throws InvalidArgumentError then catch CommanderError', () => {

Support custom error messages for option argument coercion failures (#1392)John Geebefore adoption689d7a0

line 296why, one step back
296 function justSayNo(value) {

Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa

line 297why, one step back
297 throw new commander.InvalidArgumentError('NO');

Support custom error messages for option argument coercion failures (#1392)John Geebefore adoption689d7a0

lines 298–311why, one step back
298 }
299 const optionFlags = '--colour <shade>';300 const program = new commander.Command();301 program302 .exitOverride()303 .option(optionFlags, 'specify shade', justSayNo);304305 let caughtErr;306 try {307 program.parse(['--colour', 'green'], { from: 'user' });308 } catch (err) {309 caughtErr = err;310 }311

Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa

lines 312–332why, one step back
312 expectCommanderError(caughtErr, 1, 'commander.invalidArgument', "error: option '--colour <shade>' argument 'green' is invalid. NO");
313 });314315 test('when custom processing for argument throws InvalidArgumentError then catch CommanderError', () => {316 function justSayNo(value) {317 throw new commander.InvalidArgumentError('NO');318 }319 const program = new commander.Command();320 program321 .exitOverride()322 .argument('[n]', 'number', justSayNo)323 .action(() => {});324325 let caughtErr;326 try {327 program.parse(['green'], { from: 'user' });328 } catch (err) {329 caughtErr = err;330 }331332 expectCommanderError(caughtErr, 1, 'commander.invalidArgument', "error: command-argument value 'green' is invalid for argument 'n'. NO");

Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc

line 333why, one step back
333 });

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

line 334why, one step back
334

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

lines 335–351why, one step back
335 test('when has conflicting option then throw CommanderError', () => {
336 const program = new commander.Command();337 program338 .exitOverride()339 .addOption(new commander.Option('--silent'))340 .addOption(new commander.Option('--debug').conflicts(['silent']));341342 let caughtErr;343 try {344 program.parse(['--debug', '--silent'], { from: 'user' });345 } catch (err) {346 caughtErr = err;347 }348349 expectCommanderError(caughtErr, 1, 'commander.conflictingOption', "error: option '--debug' cannot be used with option '--silent'");350 });351

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

lines 352–365why, one step back
352 test('when call error() then throw CommanderError', () => {
353 const program = new commander.Command();354 program355 .exitOverride();356357 let caughtErr;358 try {359 program.error('message');360 } catch (err) {361 caughtErr = err;362 }363364 expectCommanderError(caughtErr, 1, 'commander.error', 'message');365 });

Tidy up example now check for required command arguments (#1320)John Geebefore adoptionf47bdf5

line 366why, one step back
366});

Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2

lines 367–389why, one step back
367
368test('when no override and error then exit(1)', () => {369 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { });370 const program = new commander.Command();371 program.configureOutput({ outputError: () => {} });372 program.parse(['--unknownOption'], { from: 'user' });373 expect(exitSpy).toHaveBeenCalledWith(1);374 exitSpy.mockRestore();375});376377test('when custom processing throws custom error then throw custom error', () => {378 function justSayNo(value) {379 throw new Error('custom');380 }381 const program = new commander.Command();382 program383 .exitOverride()384 .option('-s, --shade <value>', 'specify shade', justSayNo);385386 expect(() => {387 program.parse(['--shade', 'green'], { from: 'user' });388 }).toThrow('custom');389});