Skip to content
Myr

command.exitOverride.test.js

This file

Path

tests/command.exitOverride.test.js

340 lines · 10.1 KiB · JavaScript

Last changed by

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

History of this file

Take it

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

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

lines 1–5why, 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 for

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

line 6why, one step back
6// semver minor versions. For now, also testing the error.message and that output occurred

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

lines 7–8why, one step back
7// to detect accidental changes in behaviour.
8

Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7

lines 9–10why, one step back
9/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectCommanderError"] }] */
10

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

lines 11–19why, one step back
11function 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;

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

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(() => { });

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

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

configure output (#1387)John Geebefore adoptioned7f13e

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

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

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

configure output (#1387)John Geebefore adoptioned7f13e

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

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

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();

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

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

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

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

Suggest help when unknown command (#1179)John Geebefore adoption28b649d

line 53why, one step back
53 .name('prog')

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 54–63why, one step back
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();

Suggest help when unknown command (#1179)John Geebefore adoption28b649d

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

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

lines 66–67why, one step back
66 });
67

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

lines 68–100why, one step back
68 // 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();

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

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();

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

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

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

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(() => { });

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

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();

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

lines 192–206why, 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 }206

Parse rework for nested commands (#1149)John Geebefore adoption1345f98

line 207why, one step back
207 expectCommanderError(caughtErr, 1, 'commander.help', '(outputHelp)');

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

lines 208–210why, one step back
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(() => { });

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

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();

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

lines 227–228why, one step back
227 });
228

tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706

lines 229–230why, one step back
229 test('when executableSubcommand succeeds then call exitOverride', async() => {
230 expect.hasAssertions();

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

lines 231–232why, one step back
231 const pm = path.join(__dirname, 'fixtures/pm');
232 const program = new commander.Command();

tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706

lines 233–241why, one step back
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 });

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

line 242why, one step back
242 });

Add .requiredOption() for mandatory options (#1071)John Geebefore adoption7f9edba

lines 243–259why, one step back
243
244 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

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

line 278why, one step back
278 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 279why, one step back
279 function justSayNo(value) {

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

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

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

lines 281–294why, one step back
281 }
282 const optionFlags = '--colour <shade>';283 const program = new commander.Command();284 program285 .exitOverride()286 .option(optionFlags, 'specify shade', justSayNo);287288 let caughtErr;289 try {290 program.parse(['--colour', 'green'], { from: 'user' });291 } catch (err) {292 caughtErr = err;293 }294

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

lines 295–315why, one step back
295 expectCommanderError(caughtErr, 1, 'commander.invalidArgument', "error: option '--colour <shade>' argument 'green' is invalid. NO");
296 });297298 test('when custom processing for argument throws InvalidArgumentError then catch CommanderError', () => {299 function justSayNo(value) {300 throw new commander.InvalidArgumentError('NO');301 }302 const program = new commander.Command();303 program304 .exitOverride()305 .argument('[n]', 'number', justSayNo)306 .action(() => {});307308 let caughtErr;309 try {310 program.parse(['green'], { from: 'user' });311 } catch (err) {312 caughtErr = err;313 }314315 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 316why, one step back
316 });

Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47

line 317why, one step back
317});

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

lines 318–340why, one step back
318
319test('when no override and error then exit(1)', () => {320 const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => { });321 const program = new commander.Command();322 program.configureOutput({ outputError: () => {} });323 program.parse(['--unknownOption'], { from: 'user' });324 expect(exitSpy).toHaveBeenCalledWith(1);325 exitSpy.mockRestore();326});327328test('when custom processing throws custom error then throw custom error', () => {329 function justSayNo(value) {330 throw new Error('custom');331 }332 const program = new commander.Command();333 program334 .exitOverride()335 .option('-s, --shade <value>', 'specify shade', justSayNo);336337 expect(() => {338 program.parse(['--shade', 'green'], { from: 'user' });339 }).toThrow('custom');340});