command.parse.test.js
This file
Path
tests/command.parse.test.js
419 lines · 14.8 KiB · JavaScript
Last changed by
Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2
History of this fileTake it
The walk stopped after 200 commits: lines older than cc2db5b point at it.
Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 1–6why, one step back1const commander = require('../');23// Testing some Electron conventions but not directly using Electron to avoid overheads.4// https://github.com/electron/electron/issues/4690#issuecomment-2174352225// https://www.electronjs.org/docs/api/process#processdefaultapp-readonly6Add auto-detection of args when node evaluating script code on command-line (#2164)John Geebefore adoptionb9ca390
lines 7–9why, one step back7// (If mutating process.argv and process.execArgv causes problems, could add utility8// functions to get them and then mock the functions for tests.)9Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 10–12why, one step back10describe('.parse() args from', () => {11 test('when no args then use process.argv and app/script/args', () => {12 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 13why, one step back13 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 14–22why, one step back14 const hold = process.argv;15 process.argv = 'node script.js user'.split(' ');16 program.parse();17 process.argv = hold;18 expect(program.args).toEqual(['user']);19 });2021 test('when no args and electron properties and not default app then use process.argv and app/args', () => {22 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 23why, one step back23 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 24–34why, one step back24 const holdArgv = process.argv;25 process.versions.electron = '1.2.3';26 process.argv = 'node user'.split(' ');27 program.parse();28 delete process.versions.electron;29 process.argv = holdArgv;30 expect(program.args).toEqual(['user']);31 });3233 test('when args then app/script/args', () => {34 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 35why, one step back35 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 36–41why, one step back36 program.parse('node script.js user'.split(' '));37 expect(program.args).toEqual(['user']);38 });3940 test('when args from "node" then app/script/args', () => {41 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 42why, one step back42 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 43–48why, one step back43 program.parse('node script.js user'.split(' '), { from: 'node' });44 expect(program.args).toEqual(['user']);45 });4647 test('when args from "electron" and not default app then app/args', () => {48 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 49why, one step back49 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 50–58why, one step back50 const hold = process.defaultApp;51 process.defaultApp = undefined;52 program.parse('customApp user'.split(' '), { from: 'electron' });53 process.defaultApp = hold;54 expect(program.args).toEqual(['user']);55 });5657 test('when args from "electron" and default app then app/script/args', () => {58 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 59why, one step back59 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 60–68why, one step back60 const hold = process.defaultApp;61 process.defaultApp = true;62 program.parse('electron script user'.split(' '), { from: 'electron' });63 process.defaultApp = hold;64 expect(program.args).toEqual(['user']);65 });6667 test('when args from "user" then args', () => {68 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 69why, one step back69 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 70–75why, one step back70 program.parse('user'.split(' '), { from: 'user' });71 expect(program.args).toEqual(['user']);72 });7374 test('when args from "silly" then throw', () => {75 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 76why, one step back76 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 77–80why, one step back77 expect(() => {78 program.parse(['node', 'script.js'], { from: 'silly' });79 }).toThrow();80 });Add auto-detection of args when node evaluating script code on command-line (#2164)John Geebefore adoptionb9ca390
lines 81–85why, one step back8182 test.each(['-e', '--eval', '-p', '--print'])(83 'when node execArgv includes %s then app/args',84 (flag) => {85 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 86why, one step back86 program.argument('[args...]');Add auto-detection of args when node evaluating script code on command-line (#2164)John Geebefore adoptionb9ca390
lines 87–97why, one step back87 const holdExecArgv = process.execArgv;88 const holdArgv = process.argv;89 process.argv = ['node', 'user-arg'];90 process.execArgv = [flag, 'console.log("hello, world")'];91 program.parse();92 process.argv = holdArgv;93 process.execArgv = holdExecArgv;94 expect(program.args).toEqual(['user-arg']);95 process.execArgv = holdExecArgv;96 },97 );Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 98–102why, one step back98});99100describe('return type', () => {101 test('when call .parse then returns program', () => {102 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 103why, one step back103 program.argument('[args...]');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 104why, one step back104 program.action(() => {});Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 105–109why, one step back105106 const result = program.parse(['node', 'test']);107 expect(result).toBe(program);108 });109Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 110why, one step back110 test('when await .parseAsync then returns program', async () => {Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 111why, one step back111 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 112why, one step back112 program.argument('[args...]');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 113why, one step back113 program.action(() => {});Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 114–122why, one step back114115 const result = await program.parseAsync(['node', 'test']);116 expect(result).toBe(program);117 });118});119120// Easy mistake to make when writing unit tests121test('when parse strings instead of array then throw', () => {122 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 123why, one step back123 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 124–131why, one step back124 expect(() => {125 program.parse('node', 'test');126 }).toThrow();127});128129describe('parse parameter is treated as readonly, per TypeScript declaration', () => {130 test('when parse called then parameter does not change', () => {131 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 132why, one step back132 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 133–141why, one step back133 program.option('--debug');134 const original = ['node', '--debug', 'arg'];135 const param = original.slice();136 program.parse(param);137 expect(param).toEqual(original);138 });139140 test('when parse called and parsed args later changed then parameter does not change', () => {141 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 142why, one step back142 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 143–153why, one step back143 program.option('--debug');144 const original = ['node', '--debug', 'arg'];145 const param = original.slice();146 program.parse(param);147 program.args.length = 0;148 program.rawArgs.length = 0;149 expect(param).toEqual(original);150 });151152 test('when parse called and param later changed then parsed args do not change', () => {153 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 154why, one step back154 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 155–166why, one step back155 program.option('--debug');156 const param = ['node', '--debug', 'arg'];157 program.parse(param);158 const oldArgs = program.args.slice();159 const oldRawArgs = program.rawArgs.slice();160 param.length = 0;161 expect(program.args).toEqual(oldArgs);162 expect(program.rawArgs).toEqual(oldRawArgs);163 });164});165166describe('parseAsync parameter is treated as readonly, per TypeScript declaration', () => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 167why, one step back167 test('when parse called then parameter does not change', async () => {Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 168why, one step back168 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 169why, one step back169 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 170–176why, one step back170 program.option('--debug');171 const original = ['node', '--debug', 'arg'];172 const param = original.slice();173 await program.parseAsync(param);174 expect(param).toEqual(original);175 });176Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 177why, one step back177 test('when parseAsync called and parsed args later changed then parameter does not change', async () => {Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 178why, one step back178 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 179why, one step back179 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 180–188why, one step back180 program.option('--debug');181 const original = ['node', '--debug', 'arg'];182 const param = original.slice();183 await program.parseAsync(param);184 program.args.length = 0;185 program.rawArgs.length = 0;186 expect(param).toEqual(original);187 });188Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 189why, one step back189 test('when parseAsync called and param later changed then parsed args do not change', async () => {Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 190why, one step back190 const program = new commander.Command();Disallow excess arguments (#2223)John Geebefore adoption58c28eb
line 191why, one step back191 program.argument('[args...]');Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 192–201why, one step back192 program.option('--debug');193 const param = ['node', '--debug', 'arg'];194 await program.parseAsync(param);195 const oldArgs = program.args.slice();196 const oldRawArgs = program.rawArgs.slice();197 param.length = 0;198 expect(program.args).toEqual(oldArgs);199 expect(program.rawArgs).toEqual(oldRawArgs);200 });201});Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2
lines 202–419why, one step back202203describe('.parse() called multiple times', () => {204 test('when use boolean options then option values reset', () => {205 const program = new commander.Command().option('--black').option('--white');206207 program.parse(['--black'], { from: 'user' });208 expect(program.opts()).toEqual({ black: true });209210 program.parse(['--white'], { from: 'user' });211 expect(program.opts()).toEqual({ white: true });212 });213214 test('when use options with option-argument then option values and sources reset', () => {215 const program = new commander.Command()216 .option('-f, --foo <value>')217 .option('-b, --bar <value>');218219 program.parse(['--foo', 'FOO'], { from: 'user' });220 expect(program.opts()).toEqual({ foo: 'FOO' });221 expect(program.getOptionValueSource('foo')).toEqual('cli');222 expect(program.getOptionValueSource('bar')).toBeUndefined();223224 program.parse(['--bar', 'BAR'], { from: 'user' });225 expect(program.opts()).toEqual({ bar: 'BAR' });226 expect(program.getOptionValueSource('foo')).toBeUndefined();227 expect(program.getOptionValueSource('bar')).toEqual('cli');228 });229230 test('when use options with option-argument and default then option values and sources reset', () => {231 const program = new commander.Command()232 .option('-f, --foo <value>', 'description', 'default-FOO')233 .option('-b, --bar <value>', 'description', 'default-BAR');234235 program.parse(['--foo', 'FOO'], { from: 'user' });236 expect(program.opts()).toEqual({ foo: 'FOO', bar: 'default-BAR' });237 expect(program.getOptionValueSource('foo')).toEqual('cli');238 expect(program.getOptionValueSource('bar')).toEqual('default');239240 program.parse(['--bar', 'BAR'], { from: 'user' });241 expect(program.opts()).toEqual({ foo: 'default-FOO', bar: 'BAR' });242 expect(program.getOptionValueSource('foo')).toEqual('default');243 expect(program.getOptionValueSource('bar')).toEqual('cli');244 });245246 test('when use negated options then option values reset', () => {247 const program = new commander.Command()248 .option('--no-foo')249 .option('--no-bar');250251 program.parse(['--no-foo'], { from: 'user' });252 expect(program.opts()).toEqual({ foo: false, bar: true });253254 program.parse(['--no-bar'], { from: 'user' });255 expect(program.opts()).toEqual({ foo: true, bar: false });256 });257258 test('when use variadic option then option values reset', () => {259 const program = new commander.Command().option('--var <items...>');260261 program.parse(['--var', 'a', 'b'], { from: 'user' });262 expect(program.opts()).toEqual({ var: ['a', 'b'] });263264 program.parse(['--var', 'c'], { from: 'user' });265 expect(program.opts()).toEqual({ var: ['c'] });266 });267268 test('when use collect example then option value resets', () => {269 function collect(value, previous) {270 return previous.concat([value]);271 }272 const program = new commander.Command();273 program.option('-c, --collect <value>', 'repeatable value', collect, []);274275 program.parse(['-c', 'a', '-c', 'b'], { from: 'user' });276 expect(program.opts()).toEqual({ collect: ['a', 'b'] });277278 program.parse(['-c', 'c'], { from: 'user' });279 expect(program.opts()).toEqual({ collect: ['c'] });280 });281282 test('when use increaseVerbosity example then option value resets', () => {283 function increaseVerbosity(dummyValue, previous) {284 return previous + 1;285 }286 const program = new commander.Command();287 program.option(288 '-v, --verbose',289 'verbosity that can be increased',290 increaseVerbosity,291 0,292 );293294 program.parse(['-vvv'], { from: 'user' });295 expect(program.opts()).toEqual({ verbose: 3 });296 program.parse(['-vv'], { from: 'user' });297298 expect(program.opts()).toEqual({ verbose: 2 });299 program.parse([], { from: 'user' });300 expect(program.opts()).toEqual({ verbose: 0 });301 });302303 test('when use parse and parseAsync then option values reset', async () => {304 const program = new commander.Command().option('--black').option('--white');305306 program.parse(['--black'], { from: 'user' });307 expect(program.opts()).toEqual({ black: true });308 await program.parseAsync(['--white'], { from: 'user' });309 expect(program.opts()).toEqual({ white: true });310 });311312 test('when call subcommand then option values reset (program and subcommand)', () => {313 const program = new commander.Command().option('--black').option('--white');314 const subcommand = program.command('sub').option('--red').option('--green');315316 program.parse(['--black', 'sub', '--red'], { from: 'user' });317 expect(subcommand.optsWithGlobals()).toEqual({ black: true, red: true });318319 program.parse(['--white', 'sub', '--green'], { from: 'user' });320 expect(subcommand.optsWithGlobals()).toEqual({ white: true, green: true });321 });322323 test('when call different subcommand then no reset because lazy', () => {324 // This is not a required behaviour, but is the intended behaviour.325 const program = new commander.Command();326 const sub1 = program.command('sub1').option('--red');327 const sub2 = program.command('sub2').option('--green');328329 program.parse(['sub1', '--red'], { from: 'user' });330 expect(sub1.opts()).toEqual({ red: true });331 expect(sub2.opts()).toEqual({});332333 program.parse(['sub2', '--green'], { from: 'user' });334 expect(sub1.opts()).toEqual({ red: true });335 expect(sub2.opts()).toEqual({ green: true });336 });337338 test('when parse with different implied program name then name changes', () => {339 const program = new commander.Command();340341 program.parse(['node', 'script1.js']);342 expect(program.name()).toEqual('script1');343344 program.parse(['electron', 'script2.js']);345 expect(program.name()).toEqual('script2');346 });347348 test('when parse with different arguments then args change', () => {349 // weak test, would work without store/reset!350 const program = new commander.Command()351 .argument('<first>')352 .argument('[second]');353354 program.parse(['one', 'two'], { from: 'user' });355 expect(program.args).toEqual(['one', 'two']);356357 program.parse(['alpha'], { from: 'user' });358 expect(program.args).toEqual(['alpha']);359 });360361 test('when parse with different arguments then rawArgs change', () => {362 // weak test, would work without store/reset!363 const program = new commander.Command()364 .argument('<first>')365 .option('--white')366 .option('--black');367368 program.parse(['--white', 'one'], { from: 'user' });369 expect(program.rawArgs).toEqual(['--white', 'one']);370371 program.parse(['--black', 'two'], { from: 'user' });372 expect(program.rawArgs).toEqual(['--black', 'two']);373 });374375 test('when parse with different arguments then processedArgs change', () => {376 // weak test, would work without store/reset!377 const program = new commander.Command().argument(378 '<first>',379 'first arg',380 parseFloat,381 );382383 program.parse([123], { from: 'user' });384 expect(program.processedArgs).toEqual([123]);385386 program.parse([456], { from: 'user' });387 expect(program.processedArgs).toEqual([456]);388 });389390 test('when parse subcommand then reset state before preSubcommand hook called', () => {391 let hookCalled = false;392 const program = new commander.Command().hook(393 'preSubcommand',394 (thisCommand, subcommand) => {395 hookCalled = true;396 expect(subcommand.opts()).toEqual({});397 },398 );399 const subcommand = program.command('sub').option('--red').option('--green');400401 hookCalled = false;402 program.parse(['sub', '--red'], { from: 'user' });403 expect(hookCalled).toBe(true);404 expect(subcommand.opts()).toEqual({ red: true });405406 hookCalled = false;407 program.parse(['sub', '--green'], { from: 'user' });408 expect(hookCalled).toBe(true);409 expect(subcommand.opts()).toEqual({ green: true });410 });411412 test('when using storeOptionsAsProperties then throw on second parse', () => {413 const program = new commander.Command().storeOptionsAsProperties();414 program.parse();415 expect(() => {416 program.parse();417 }).toThrow();418 });419});