Skip to content
Myr

command.positionalOptions.test.js

This file

Path

tests/command.positionalOptions.test.js

522 lines · 18.4 KiB · JavaScript

Last changed by

Merge branch 'develop' into release/13.xJohn Geebefore adoption5a79585

History of this file

Take it

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

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 1–9why, one step back
1const commander = require('../');
23// The changes to parsing for positional options are subtle, and took extra care to work with4// implicit help and default commands. Lots of tests.56describe('program with passThrough', () => {7 function makeProgram() {8 const program = new commander.Command();9 program.passThroughOptions();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 10why, one step back
10 program.option('-d, --debug').argument('<args...>');

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 11–38why, one step back
11 return program;
12 }1314 test('when option before command-argument then option parsed', () => {15 const program = makeProgram();16 program.parse(['--debug', 'arg'], { from: 'user' });17 expect(program.args).toEqual(['arg']);18 expect(program.opts().debug).toBe(true);19 });2021 test('when known option after command-argument then option passed through', () => {22 const program = makeProgram();23 program.parse(['arg', '--debug'], { from: 'user' });24 expect(program.args).toEqual(['arg', '--debug']);25 expect(program.opts().debug).toBeUndefined();26 });2728 test('when unknown option after command-argument then option passed through', () => {29 const program = makeProgram();30 program.parse(['arg', '--pass'], { from: 'user' });31 expect(program.args).toEqual(['arg', '--pass']);32 });3334 test('when action handler and unknown option after command-argument then option passed through', () => {35 const program = makeProgram();36 const mockAction = jest.fn();37 program.action(mockAction);38 program.parse(['arg', '--pass'], { from: 'user' });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 39–43why, one step back
39 expect(mockAction).toHaveBeenCalledWith(
40 ['arg', '--pass'],41 program.opts(),42 program,43 );

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 44–49why, one step back
44 });
4546 test('when help option (without command-argument) then help called', () => {47 const program = makeProgram();48 const mockHelp = jest.fn(() => '');49

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 50why, one step back
50 program.exitOverride().configureHelp({ formatHelp: mockHelp });

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 51–52why, one step back
51 try {
52 program.parse(['--help'], { from: 'user' });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 53–55why, one step back
53 } catch (err) {
54 /* empty */55 }

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 56–112why, one step back
56 expect(mockHelp).toHaveBeenCalled();
57 });5859 test('when help option after command-argument then option passed through', () => {60 const program = makeProgram();61 program.parse(['arg', '--help'], { from: 'user' });62 expect(program.args).toEqual(['arg', '--help']);63 });6465 test('when version option after command-argument then option passed through', () => {66 const program = makeProgram();67 program.version('1.2.3');68 program.parse(['arg', '--version'], { from: 'user' });69 expect(program.args).toEqual(['arg', '--version']);70 });71});7273// -----------------------------------------------------------7475describe('program with positionalOptions and subcommand', () => {76 function makeProgram() {77 const program = new commander.Command();78 program79 .enablePositionalOptions()80 .option('-s, --shared <value>')81 .argument('<args...>');82 const sub = program83 .command('sub')84 .argument('[arg]')85 .option('-s, --shared <value>')86 .action(() => {}); // Not used, but normal to have action handler on subcommand.87 return { program, sub };88 }8990 test('when global option before subcommand then global option parsed', () => {91 const { program } = makeProgram();92 program.parse(['--shared', 'program', 'sub'], { from: 'user' });93 expect(program.opts().shared).toEqual('program');94 });9596 test('when shared option after subcommand then parsed by subcommand', () => {97 const { program, sub } = makeProgram();98 program.parse(['sub', '--shared', 'local'], { from: 'user' });99 expect(sub.opts().shared).toEqual('local');100 expect(program.opts().shared).toBeUndefined();101 });102103 test('when shared option after subcommand argument then parsed by subcommand', () => {104 const { program, sub } = makeProgram();105 program.parse(['sub', 'arg', '--shared', 'local'], { from: 'user' });106 expect(sub.opts().shared).toEqual('local');107 expect(sub.args).toEqual(['arg']);108 expect(program.opts().shared).toBeUndefined();109 });110111 test('when shared option before and after subcommand then both parsed', () => {112 const { program, sub } = makeProgram();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 113–115why, one step back
113 program.parse(['--shared', 'program', 'sub', '--shared', 'local'], {
114 from: 'user',115 });

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 116–126why, one step back
116 expect(program.opts().shared).toEqual('program');
117 expect(sub.opts().shared).toEqual('local');118 });119120 test.each([121 [[], 1, 0],122 [['sub'], 0, 0],123 [['--help'], 1, 0],124 [['sub', '--help'], 0, 1],125 [['sub', 'foo', '--help'], 0, 1],126 [['help'], 1, 0],

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 127–135why, one step back
127 [['help', 'sub'], 0, 1],
128 ])(129 'help: when user args %p then program/sub help called %p/%p',130 (userArgs, expectProgramHelpCount, expectSubHelpCount) => {131 const { program, sub } = makeProgram();132 const mockProgramHelp = jest.fn();133 program.exitOverride().configureHelp({ formatHelp: mockProgramHelp });134 const mockSubHelp = jest.fn();135 sub.exitOverride().configureHelp({ formatHelp: mockSubHelp });

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 136why, one step back
136

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 137–145why, one step back
137 try {
138 program.parse(userArgs, { from: 'user' });139 } catch (err) {140 /* empty */141 }142 expect(mockProgramHelp).toHaveBeenCalledTimes(expectProgramHelpCount);143 expect(mockSubHelp).toHaveBeenCalledTimes(expectSubHelpCount);144 },145 );

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 146–194why, one step back
146});
147148// ---------------------------------------------------------------149150describe('program with positionalOptions and default subcommand (called sub)', () => {151 function makeProgram() {152 const program = new commander.Command();153 program154 .enablePositionalOptions()155 .option('-s, --shared')156 .option('-g, --global')157 .argument('<args...>');158 const sub = program159 .command('sub', { isDefault: true })160 .argument('[args...]')161 .option('-s, --shared')162 .option('-d, --default')163 .action(() => {}); // Not used, but normal to have action handler on subcommand.164 program.command('another'); // Not used, but normal to have more than one subcommand if have a default.165 return { program, sub };166 }167168 test('when program option before sub option then program option read by program', () => {169 const { program } = makeProgram();170 program.parse(['--global', '--default'], { from: 'user' });171 expect(program.opts().global).toBe(true);172 });173174 test('when program option before sub option then sub option read by sub', () => {175 const { program, sub } = makeProgram();176 program.parse(['--global', '--default'], { from: 'user' });177 expect(sub.opts().default).toBe(true);178 });179180 test('when shared option before sub argument then option read by program', () => {181 const { program } = makeProgram();182 program.parse(['--shared', 'foo'], { from: 'user' });183 expect(program.opts().shared).toBe(true);184 });185186 test('when shared option after sub argument then option read by sub', () => {187 const { program, sub } = makeProgram();188 program.parse(['foo', '--shared'], { from: 'user' });189 expect(sub.opts().shared).toBe(true);190 });191192 test.each([193 [[], 0, 0],194 [['--help'], 1, 0],

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 195–203why, one step back
195 [['help'], 1, 0],
196 ])(197 'help: when user args %p then program/sub help called %p/%p',198 (userArgs, expectProgramHelpCount, expectSubHelpCount) => {199 const { program, sub } = makeProgram();200 const mockProgramHelp = jest.fn();201 program.exitOverride().configureHelp({ formatHelp: mockProgramHelp });202 const mockSubHelp = jest.fn();203 sub.exitOverride().configureHelp({ formatHelp: mockSubHelp });

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 204why, one step back
204

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 205–213why, one step back
205 try {
206 program.parse(userArgs, { from: 'user' });207 } catch (err) {208 /* empty */209 }210 expect(mockProgramHelp).toHaveBeenCalledTimes(expectProgramHelpCount);211 expect(mockSubHelp).toHaveBeenCalledTimes(expectSubHelpCount);212 },213 );

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 214–277why, one step back
214});
215216// ------------------------------------------------------------------------------217218describe('subcommand with passThrough', () => {219 function makeProgram() {220 const program = new commander.Command();221 program222 .enablePositionalOptions()223 .option('-s, --shared <value>')224 .argument('<args...>');225 const sub = program226 .command('sub')227 .passThroughOptions()228 .argument('[args...]')229 .option('-s, --shared <value>')230 .option('-d, --debug')231 .action(() => {}); // Not used, but normal to have action handler on subcommand.232 return { program, sub };233 }234235 test('when option before command-argument then option parsed', () => {236 const { program, sub } = makeProgram();237 program.parse(['sub', '--debug', 'arg'], { from: 'user' });238 expect(sub.args).toEqual(['arg']);239 expect(sub.opts().debug).toBe(true);240 });241242 test('when known option after command-argument then option passed through', () => {243 const { program, sub } = makeProgram();244 program.parse(['sub', 'arg', '--debug'], { from: 'user' });245 expect(sub.args).toEqual(['arg', '--debug']);246 expect(sub.opts().debug).toBeUndefined();247 });248249 test('when unknown option after command-argument then option passed through', () => {250 const { program, sub } = makeProgram();251 program.parse(['sub', 'arg', '--pass'], { from: 'user' });252 expect(sub.args).toEqual(['arg', '--pass']);253 });254255 test('when action handler and unknown option after command-argument then option passed through', () => {256 const { program, sub } = makeProgram();257 const mockAction = jest.fn();258 sub.action(mockAction);259 program.parse(['sub', 'arg', '--pass'], { from: 'user' });260 expect(mockAction).toHaveBeenCalledWith(['arg', '--pass'], sub.opts(), sub);261 });262263 test('when help option after command-argument then option passed through', () => {264 const { program, sub } = makeProgram();265 program.parse(['sub', 'arg', '--help'], { from: 'user' });266 expect(sub.args).toEqual(['arg', '--help']);267 });268269 test('when version option after command-argument then option passed through', () => {270 const { program, sub } = makeProgram();271 program.version('1.2.3');272 program.parse(['sub', 'arg', '--version'], { from: 'user' });273 expect(sub.args).toEqual(['arg', '--version']);274 });275276 test('when shared option before sub and after sub and after sub parameter then all three parsed', () => {277 const { program, sub } = makeProgram();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 278–281why, one step back
278 program.parse(
279 ['--shared=global', 'sub', '--shared=local', 'arg', '--shared'],280 { from: 'user' },281 );

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 282–292why, one step back
282 expect(program.opts().shared).toEqual('global');
283 expect(sub.opts().shared).toEqual('local');284 expect(sub.args).toEqual(['arg', '--shared']);285 });286});287288// ------------------------------------------------------------------------------289290describe('default command with passThrough', () => {291 function makeProgram() {292 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 293why, one step back
293 program.enablePositionalOptions();

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 294–369why, one step back
294 const sub = program
295 .command('sub', { isDefault: true })296 .passThroughOptions()297 .argument('[args...]')298 .option('-d, --debug')299 .action(() => {}); // Not used, but normal to have action handler on subcommand.300 return { program, sub };301 }302303 test('when option before command-argument then option parsed', () => {304 const { program, sub } = makeProgram();305 program.parse(['--debug', 'arg'], { from: 'user' });306 expect(sub.args).toEqual(['arg']);307 expect(sub.opts().debug).toBe(true);308 });309310 test('when known option after command-argument then option passed through', () => {311 const { program, sub } = makeProgram();312 program.parse(['arg', '--debug'], { from: 'user' });313 expect(sub.args).toEqual(['arg', '--debug']);314 expect(sub.opts().debug).toBeUndefined();315 });316317 test('when unknown option after command-argument then option passed through', () => {318 const { program, sub } = makeProgram();319 program.parse(['arg', '--pass'], { from: 'user' });320 expect(sub.args).toEqual(['arg', '--pass']);321 });322323 test('when action handler and unknown option after command-argument then option passed through', () => {324 const { program, sub } = makeProgram();325 const mockAction = jest.fn();326 sub.action(mockAction);327 program.parse(['arg', '--pass'], { from: 'user' });328 expect(mockAction).toHaveBeenCalledWith(['arg', '--pass'], sub.opts(), sub);329 });330});331332// ------------------------------------------------------------------------------333334describe('program with action handler and positionalOptions and subcommand', () => {335 function makeProgram() {336 const program = new commander.Command();337 program338 .enablePositionalOptions()339 .option('-g, --global')340 .argument('<args...>')341 .action(() => {});342 const sub = program343 .command('sub')344 .argument('[arg]')345 .action(() => {});346 return { program, sub };347 }348349 test('when global option before parameter then global option parsed', () => {350 const { program } = makeProgram();351 program.parse(['--global', 'foo'], { from: 'user' });352 expect(program.opts().global).toBe(true);353 });354355 test('when global option after parameter then global option parsed', () => {356 const { program } = makeProgram();357 program.parse(['foo', '--global'], { from: 'user' });358 expect(program.opts().global).toBe(true);359 });360361 test('when global option after parameter with same name as subcommand then global option parsed', () => {362 const { program } = makeProgram();363 program.parse(['foo', 'sub', '--global'], { from: 'user' });364 expect(program.opts().global).toBe(true);365 });366});367368// ------------------------------------------------------------------------------369

Merge pull request #2140 from tj/release/12.xJohn Geebefore adoptione3192a1

lines 370–373why, one step back
370describe('broken passThrough', () => {
371 test('when program not positional and turn on passThroughOptions in subcommand then error', () => {372 const program = new commander.Command();373 const sub = program.command('sub');

Clarify that more than just READMEJohn Geebefore adoption411ca95

line 374why, one step back
374

Merge pull request #2140 from tj/release/12.xJohn Geebefore adoptione3192a1

lines 375–381why, one step back
375 expect(() => {
376 sub.passThroughOptions();377 }).toThrow();378 });379380 test('when program not positional and add subcommand with passThroughOptions then error', () => {381 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 382why, one step back
382 const sub = new commander.Command('sub').passThroughOptions();

Merge pull request #2140 from tj/release/12.xJohn Geebefore adoptione3192a1

lines 383–387why, one step back
383
384 expect(() => {385 program.addCommand(sub);386 }).toThrow();387 });

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 388–426why, one step back
388});
389390// ------------------------------------------------------------------------------391392describe('program with action handler and passThrough and subcommand', () => {393 function makeProgram() {394 const program = new commander.Command();395 program396 .passThroughOptions()397 .option('-g, --global')398 .argument('<args...>')399 .action(() => {});400 const sub = program401 .command('sub')402 .argument('[arg]')403 .option('-g, --group')404 .option('-d, --debug')405 .action(() => {});406 return { program, sub };407 }408409 test('when global option before parameter then global option parsed', () => {410 const { program } = makeProgram();411 program.parse(['--global', 'foo'], { from: 'user' });412 expect(program.opts().global).toBe(true);413 });414415 test('when global option after parameter then passed through', () => {416 const { program } = makeProgram();417 program.parse(['foo', '--global'], { from: 'user' });418 expect(program.args).toEqual(['foo', '--global']);419 });420421 test('when subcommand option after subcommand then option parsed', () => {422 const { program, sub } = makeProgram();423 program.parse(['sub', '--debug'], { from: 'user' });424 expect(sub.opts().debug).toBe(true);425 });426

Merge branch 'develop' into release/13.xJohn Geebefore adoption5a79585

line 427why, one step back
427 // This is somewhat of a side-effect of supporting previous test.

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 428–440why, one step back
428 test('when shared option after subcommand then parsed by subcommand', () => {
429 const { program, sub } = makeProgram();430 program.parse(['sub', '-g'], { from: 'user' });431 expect(sub.opts().group).toBe(true);432 expect(program.opts().global).toBeUndefined();433 });434});435436// ------------------------------------------------------------------------------437438describe('program with allowUnknownOption', () => {439 test('when passThroughOptions and unknown option then arguments from unknown passed through', () => {440 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 441why, one step back
441 program.passThroughOptions().allowUnknownOption().option('--debug');

Disallow excess arguments (#2223)John Geebefore adoption58c28eb

line 442why, one step back
442 program.argument('[args...]');

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 443–449why, one step back
443
444 program.parse(['--unknown', '--debug'], { from: 'user' });445 expect(program.args).toEqual(['--unknown', '--debug']);446 });447448 test('when positionalOptions and unknown option then known options then known option parsed', () => {449 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 450why, one step back
450 program.enablePositionalOptions().allowUnknownOption().option('--debug');

Disallow excess arguments (#2223)John Geebefore adoption58c28eb

line 451why, one step back
451 program.argument('[args...]');

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 452–463why, one step back
452
453 program.parse(['--unknown', '--debug'], { from: 'user' });454 expect(program.opts().debug).toBe(true);455 expect(program.args).toEqual(['--unknown']);456 });457});458459// ------------------------------------------------------------------------------460461describe('passThroughOptions(xxx) and option after command-argument', () => {462 function makeProgram() {463 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 464why, one step back
464 program.option('-d, --debug').argument('<args...>');

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 465–495why, one step back
465 return program;
466 }467468 test('when passThroughOptions() then option passed through', () => {469 const program = makeProgram();470 program.passThroughOptions();471 program.parse(['foo', '--debug'], { from: 'user' });472 expect(program.args).toEqual(['foo', '--debug']);473 });474475 test('when passThroughOptions(true) then option passed through', () => {476 const program = makeProgram();477 program.passThroughOptions(true);478 program.parse(['foo', '--debug'], { from: 'user' });479 expect(program.args).toEqual(['foo', '--debug']);480 });481482 test('when passThroughOptions(false) then option parsed', () => {483 const program = makeProgram();484 program.passThroughOptions(false);485 program.parse(['foo', '--debug'], { from: 'user' });486 expect(program.args).toEqual(['foo']);487 expect(program.opts().debug).toEqual(true);488 });489});490491// ------------------------------------------------------------------------------492493describe('enablePositionalOptions(xxx) and shared option after subcommand', () => {494 function makeProgram() {495 const program = new commander.Command();

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 496–497why, one step back
496 program.option('-d, --debug');
497 const sub = program.command('sub').option('-d, --debug');

Clarify that more than just READMEJohn Geebefore adoption411ca95

lines 498–522why, one step back
498 return { program, sub };
499 }500501 test('when enablePositionalOptions() then option parsed by subcommand', () => {502 const { program, sub } = makeProgram();503 program.enablePositionalOptions();504 program.parse(['sub', '--debug'], { from: 'user' });505 expect(sub.opts().debug).toEqual(true);506 });507508 test('when enablePositionalOptions(true) then option parsed by subcommand', () => {509 const { program, sub } = makeProgram();510 program.enablePositionalOptions(true);511 program.parse(['sub', '--debug'], { from: 'user' });512 expect(sub.opts().debug).toEqual(true);513 });514515 test('when enablePositionalOptions(false) then option parsed by program', () => {516 const { program, sub } = makeProgram();517 program.enablePositionalOptions(false);518 program.parse(['sub', '--debug'], { from: 'user' });519 expect(sub.opts().debug).toBeUndefined();520 expect(program.opts().debug).toEqual(true);521 });522});