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 fileTake 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 back1const 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 forMerge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b
line 6why, one step back6// semver minor versions. For now, also testing the error.message and that output occurredMerge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 7–8why, one step back7// to detect accidental changes in behaviour.8Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7
lines 9–10why, one step back9/* eslint jest/expect-expect: ["error", { "assertFunctionNames": ["expect", "expectCommanderError"] }] */10Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 11–19why, one step back11function 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 back20 let stderrSpy;Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 21–22why, one step back2122 beforeAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 23why, one step back23 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 back24 });2526 afterEach(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 27why, one step back27 stderrSpy.mockClear();Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 28–30why, one step back28 });2930 afterAll(() => {configure output (#1387)John Geebefore adoptioned7f13e
line 31why, one step back31 stderrSpy.mockRestore();Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 32–45why, one step back32 });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 }45configure output (#1387)John Geebefore adoptioned7f13e
line 46why, one step back46 expect(stderrSpy).toHaveBeenCalled();Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 47–49why, one step back47 expectCommanderError(caughtErr, 1, 'commander.unknownOption', "error: unknown option '-m'");48 });49Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 50–52why, one step back50 test('when specify unknown command then throw CommanderError', () => {51 const program = new commander.Command();52 programSuggest help when unknown command (#1179)John Geebefore adoption28b649d
line 53why, one step back53 .name('prog')Parse rework for nested commands (#1149)John Geebefore adoption1345f98
lines 54–63why, one step back54 .exitOverride()55 .command('sub');5657 let caughtErr;58 try {59 program.parse(['node', 'test', 'oops']);60 } catch (err) {61 caughtErr = err;62 }63configure output (#1387)John Geebefore adoptioned7f13e
line 64why, one step back64 expect(stderrSpy).toHaveBeenCalled();Suggest help when unknown command (#1179)John Geebefore adoption28b649d
line 65why, one step back65 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 back66 });67Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 68–100why, one step back68 // 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 }100configure output (#1387)John Geebefore adoptioned7f13e
line 101why, one step back101 expect(stderrSpy).toHaveBeenCalled();Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 102–118why, one step back102 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 }118configure output (#1387)John Geebefore adoptioned7f13e
line 119why, one step back119 expect(stderrSpy).toHaveBeenCalled();Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 120–122why, one step back120 expectCommanderError(caughtErr, 1, 'commander.missingArgument', "error: missing required argument 'arg-name'");121 });122Check command-arguments even without action handler (#1502)John Geebefore adoption0504945
lines 123–139why, one step back123 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 });139Add .allowExcessArguments() and error message (#1407)John Geebefore adoption09f277a
lines 140–176why, one step back140 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 });176Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
line 177why, one step back177 test('when specify --help then throw CommanderError', () => {Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
line 178why, one step back178 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 back179 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 back191 writeSpy.mockRestore();Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 192–206why, one step back192 });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 }206Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 207why, one step back207 expectCommanderError(caughtErr, 1, 'commander.help', '(outputHelp)');Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 208–210why, one step back208 });209210 test('when specify --version then throw CommanderError', () => {configure output (#1387)John Geebefore adoptioned7f13e
line 211why, one step back211 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 back212 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 back226 stdoutSpy.mockRestore();Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
lines 227–228why, one step back227 });228tests: remove eslint-disable jest/no-test-callback (#1312)michgabefore adoptionb915706
lines 229–230why, one step back229 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 back231 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 back233 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 back242 });Add .requiredOption() for mandatory options (#1071)John Geebefore adoption7f9edba
lines 243–259why, one step back243244 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 back260261 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 }274Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
line 275why, one step back275 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 back276 });277Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
line 278why, one step back278 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 back279 function justSayNo(value) {Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
line 280why, one step back280 throw new commander.InvalidArgumentError('NO');Support custom error messages for option argument coercion failures (#1392)John Geebefore adoption689d7a0
lines 281–294why, one step back281 }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 }294Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa
lines 295–315why, one step back295 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 back316 });Merge pull request #1060 from shadowspawn/feature/changelog-2.20.1Tomoaki Abebefore adoption9499d47
line 317why, one step back317});Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2
lines 318–340why, one step back318319test('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});