command.positionalOptions.test.js
This file
Path
tests/command.positionalOptions.test.js
527 lines · 18.3 KiB · JavaScript
Last changed by
Use weaker wording: "broken" instead of "illegal"Wee Bitbefore adoptionc60ce9d
History of this fileTake it
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();10 program11 .option('-d, --debug')12 .argument('<args...>');13 return program;14 }1516 test('when option before command-argument then option parsed', () => {17 const program = makeProgram();18 program.parse(['--debug', 'arg'], { from: 'user' });19 expect(program.args).toEqual(['arg']);20 expect(program.opts().debug).toBe(true);21 });2223 test('when known option after command-argument then option passed through', () => {24 const program = makeProgram();25 program.parse(['arg', '--debug'], { from: 'user' });26 expect(program.args).toEqual(['arg', '--debug']);27 expect(program.opts().debug).toBeUndefined();28 });2930 test('when unknown option after command-argument then option passed through', () => {31 const program = makeProgram();32 program.parse(['arg', '--pass'], { from: 'user' });33 expect(program.args).toEqual(['arg', '--pass']);34 });3536 test('when action handler and unknown option after command-argument then option passed through', () => {37 const program = makeProgram();38 const mockAction = jest.fn();39 program.action(mockAction);40 program.parse(['arg', '--pass'], { from: 'user' });41 expect(mockAction).toHaveBeenCalledWith(['arg', '--pass'], program.opts(), program);42 });4344 test('when help option (without command-argument) then help called', () => {45 const program = makeProgram();46 const mockHelp = jest.fn(() => '');4748 program49 .exitOverride()50 .configureHelp({ formatHelp: mockHelp });51 try {52 program.parse(['--help'], { from: 'user' });53 } catch (err) {54 }55 expect(mockHelp).toHaveBeenCalled();56 });5758 test('when help option after command-argument then option passed through', () => {59 const program = makeProgram();60 program.parse(['arg', '--help'], { from: 'user' });61 expect(program.args).toEqual(['arg', '--help']);62 });6364 test('when version option after command-argument then option passed through', () => {65 const program = makeProgram();66 program.version('1.2.3');67 program.parse(['arg', '--version'], { from: 'user' });68 expect(program.args).toEqual(['arg', '--version']);69 });70});7172// -----------------------------------------------------------7374describe('program with positionalOptions and subcommand', () => {75 function makeProgram() {76 const program = new commander.Command();77 program78 .enablePositionalOptions()79 .option('-s, --shared <value>')80 .argument('<args...>');81 const sub = program82 .command('sub')83 .argument('[arg]')84 .option('-s, --shared <value>')85 .action(() => {}); // Not used, but normal to have action handler on subcommand.86 return { program, sub };87 }8889 test('when global option before subcommand then global option parsed', () => {90 const { program } = makeProgram();91 program.parse(['--shared', 'program', 'sub'], { from: 'user' });92 expect(program.opts().shared).toEqual('program');93 });9495 test('when shared option after subcommand then parsed by subcommand', () => {96 const { program, sub } = makeProgram();97 program.parse(['sub', '--shared', 'local'], { from: 'user' });98 expect(sub.opts().shared).toEqual('local');99 expect(program.opts().shared).toBeUndefined();100 });101102 test('when shared option after subcommand argument then parsed by subcommand', () => {103 const { program, sub } = makeProgram();104 program.parse(['sub', 'arg', '--shared', 'local'], { from: 'user' });105 expect(sub.opts().shared).toEqual('local');106 expect(sub.args).toEqual(['arg']);107 expect(program.opts().shared).toBeUndefined();108 });109110 test('when shared option before and after subcommand then both parsed', () => {111 const { program, sub } = makeProgram();112 program.parse(['--shared', 'program', 'sub', '--shared', 'local'], { from: 'user' });113 expect(program.opts().shared).toEqual('program');114 expect(sub.opts().shared).toEqual('local');115 });116117 test.each([118 [[], 1, 0],119 [['sub'], 0, 0],120 [['--help'], 1, 0],121 [['sub', '--help'], 0, 1],122 [['sub', 'foo', '--help'], 0, 1],123 [['help'], 1, 0],124 [['help', 'sub'], 0, 1]125 ])('help: when user args %p then program/sub help called %p/%p', (userArgs, expectProgramHelpCount, expectSubHelpCount) => {126 const { program, sub } = makeProgram();127 const mockProgramHelp = jest.fn();128 program129 .exitOverride()130 .configureHelp({ formatHelp: mockProgramHelp });131 const mockSubHelp = jest.fn();132 sub133 .exitOverride()134 .configureHelp({ formatHelp: mockSubHelp });135136 try {137 program.parse(userArgs, { from: 'user' });138 } catch (err) {139 }140 expect(mockProgramHelp).toHaveBeenCalledTimes(expectProgramHelpCount);141 expect(mockSubHelp).toHaveBeenCalledTimes(expectSubHelpCount);142 });143});144145// ---------------------------------------------------------------146147describe('program with positionalOptions and default subcommand (called sub)', () => {148 function makeProgram() {149 const program = new commander.Command();150 program151 .enablePositionalOptions()152 .option('-s, --shared')153 .option('-g, --global')154 .argument('<args...>');155 const sub = program156 .command('sub', { isDefault: true })157 .argument('[args...]')158 .option('-s, --shared')159 .option('-d, --default')160 .action(() => {}); // Not used, but normal to have action handler on subcommand.161 program.command('another'); // Not used, but normal to have more than one subcommand if have a default.162 return { program, sub };163 }164165 test('when program option before sub option then program option read by program', () => {166 const { program } = makeProgram();167 program.parse(['--global', '--default'], { from: 'user' });168 expect(program.opts().global).toBe(true);169 });170171 test('when program option before sub option then sub option read by sub', () => {172 const { program, sub } = makeProgram();173 program.parse(['--global', '--default'], { from: 'user' });174 expect(sub.opts().default).toBe(true);175 });176177 test('when shared option before sub argument then option read by program', () => {178 const { program } = makeProgram();179 program.parse(['--shared', 'foo'], { from: 'user' });180 expect(program.opts().shared).toBe(true);181 });182183 test('when shared option after sub argument then option read by sub', () => {184 const { program, sub } = makeProgram();185 program.parse(['foo', '--shared'], { from: 'user' });186 expect(sub.opts().shared).toBe(true);187 });188189 test.each([190 [[], 0, 0],191 [['--help'], 1, 0],192 [['help'], 1, 0]193 ])('help: when user args %p then program/sub help called %p/%p', (userArgs, expectProgramHelpCount, expectSubHelpCount) => {194 const { program, sub } = makeProgram();195 const mockProgramHelp = jest.fn();196 program197 .exitOverride()198 .configureHelp({ formatHelp: mockProgramHelp });199 const mockSubHelp = jest.fn();200 sub201 .exitOverride()202 .configureHelp({ formatHelp: mockSubHelp });203204 try {205 program.parse(userArgs, { from: 'user' });206 } catch (err) {207 }208 expect(mockProgramHelp).toHaveBeenCalledTimes(expectProgramHelpCount);209 expect(mockSubHelp).toHaveBeenCalledTimes(expectSubHelpCount);210 });211});212213// ------------------------------------------------------------------------------214215describe('subcommand with passThrough', () => {216 function makeProgram() {217 const program = new commander.Command();218 program219 .enablePositionalOptions()220 .option('-s, --shared <value>')221 .argument('<args...>');222 const sub = program223 .command('sub')224 .passThroughOptions()225 .argument('[args...]')226 .option('-s, --shared <value>')227 .option('-d, --debug')228 .action(() => {}); // Not used, but normal to have action handler on subcommand.229 return { program, sub };230 }231232 test('when option before command-argument then option parsed', () => {233 const { program, sub } = makeProgram();234 program.parse(['sub', '--debug', 'arg'], { from: 'user' });235 expect(sub.args).toEqual(['arg']);236 expect(sub.opts().debug).toBe(true);237 });238239 test('when known option after command-argument then option passed through', () => {240 const { program, sub } = makeProgram();241 program.parse(['sub', 'arg', '--debug'], { from: 'user' });242 expect(sub.args).toEqual(['arg', '--debug']);243 expect(sub.opts().debug).toBeUndefined();244 });245246 test('when unknown option after command-argument then option passed through', () => {247 const { program, sub } = makeProgram();248 program.parse(['sub', 'arg', '--pass'], { from: 'user' });249 expect(sub.args).toEqual(['arg', '--pass']);250 });251252 test('when action handler and unknown option after command-argument then option passed through', () => {253 const { program, sub } = makeProgram();254 const mockAction = jest.fn();255 sub.action(mockAction);256 program.parse(['sub', 'arg', '--pass'], { from: 'user' });257 expect(mockAction).toHaveBeenCalledWith(['arg', '--pass'], sub.opts(), sub);258 });259260 test('when help option after command-argument then option passed through', () => {261 const { program, sub } = makeProgram();262 program.parse(['sub', 'arg', '--help'], { from: 'user' });263 expect(sub.args).toEqual(['arg', '--help']);264 });265266 test('when version option after command-argument then option passed through', () => {267 const { program, sub } = makeProgram();268 program.version('1.2.3');269 program.parse(['sub', 'arg', '--version'], { from: 'user' });270 expect(sub.args).toEqual(['arg', '--version']);271 });272273 test('when shared option before sub and after sub and after sub parameter then all three parsed', () => {274 const { program, sub } = makeProgram();275 program.parse(['--shared=global', 'sub', '--shared=local', 'arg', '--shared'], { from: 'user' });276 expect(program.opts().shared).toEqual('global');277 expect(sub.opts().shared).toEqual('local');278 expect(sub.args).toEqual(['arg', '--shared']);279 });280});281282// ------------------------------------------------------------------------------283284describe('default command with passThrough', () => {285 function makeProgram() {286 const program = new commander.Command();287 program288 .enablePositionalOptions();289 const sub = program290 .command('sub', { isDefault: true })291 .passThroughOptions()292 .argument('[args...]')293 .option('-d, --debug')294 .action(() => {}); // Not used, but normal to have action handler on subcommand.295 return { program, sub };296 }297298 test('when option before command-argument then option parsed', () => {299 const { program, sub } = makeProgram();300 program.parse(['--debug', 'arg'], { from: 'user' });301 expect(sub.args).toEqual(['arg']);302 expect(sub.opts().debug).toBe(true);303 });304305 test('when known option after command-argument then option passed through', () => {306 const { program, sub } = makeProgram();307 program.parse(['arg', '--debug'], { from: 'user' });308 expect(sub.args).toEqual(['arg', '--debug']);309 expect(sub.opts().debug).toBeUndefined();310 });311312 test('when unknown option after command-argument then option passed through', () => {313 const { program, sub } = makeProgram();314 program.parse(['arg', '--pass'], { from: 'user' });315 expect(sub.args).toEqual(['arg', '--pass']);316 });317318 test('when action handler and unknown option after command-argument then option passed through', () => {319 const { program, sub } = makeProgram();320 const mockAction = jest.fn();321 sub.action(mockAction);322 program.parse(['arg', '--pass'], { from: 'user' });323 expect(mockAction).toHaveBeenCalledWith(['arg', '--pass'], sub.opts(), sub);324 });325});326327// ------------------------------------------------------------------------------328329describe('program with action handler and positionalOptions and subcommand', () => {330 function makeProgram() {331 const program = new commander.Command();332 program333 .enablePositionalOptions()334 .option('-g, --global')335 .argument('<args...>')336 .action(() => {});337 const sub = program338 .command('sub')339 .argument('[arg]')340 .action(() => {});341 return { program, sub };342 }343344 test('when global option before parameter then global option parsed', () => {345 const { program } = makeProgram();346 program.parse(['--global', 'foo'], { from: 'user' });347 expect(program.opts().global).toBe(true);348 });349350 test('when global option after parameter then global option parsed', () => {351 const { program } = makeProgram();352 program.parse(['foo', '--global'], { from: 'user' });353 expect(program.opts().global).toBe(true);354 });355356 test('when global option after parameter with same name as subcommand then global option parsed', () => {357 const { program } = makeProgram();358 program.parse(['foo', 'sub', '--global'], { from: 'user' });359 expect(program.opts().global).toBe(true);360 });361});362363// ------------------------------------------------------------------------------364365describe('broken passThrough', () => {366 test('when program not positional and turn on passThroughOptions in subcommand then error', () => {367 const program = new commander.Command();368 const sub = program.command('sub');369370 expect(() => {371 sub.passThroughOptions();372 }).toThrow();373 });374375 test('when program not positional and add subcommand with passThroughOptions then error', () => {376 const program = new commander.Command();377 const sub = new commander.Command('sub')378 .passThroughOptions();379380 expect(() => {381 program.addCommand(sub);382 }).toThrow();383 });384});385386// ------------------------------------------------------------------------------387388describe('program with action handler and passThrough and subcommand', () => {389 function makeProgram() {390 const program = new commander.Command();391 program392 .passThroughOptions()393 .option('-g, --global')394 .argument('<args...>')395 .action(() => {});396 const sub = program397 .command('sub')398 .argument('[arg]')399 .option('-g, --group')400 .option('-d, --debug')401 .action(() => {});402 return { program, sub };403 }404405 test('when global option before parameter then global option parsed', () => {406 const { program } = makeProgram();407 program.parse(['--global', 'foo'], { from: 'user' });408 expect(program.opts().global).toBe(true);409 });410411 test('when global option after parameter then passed through', () => {412 const { program } = makeProgram();413 program.parse(['foo', '--global'], { from: 'user' });414 expect(program.args).toEqual(['foo', '--global']);415 });416417 test('when subcommand option after subcommand then option parsed', () => {418 const { program, sub } = makeProgram();419 program.parse(['sub', '--debug'], { from: 'user' });420 expect(sub.opts().debug).toBe(true);421 });422423 // This is somewhat of a side-affect of supporting previous test.424 test('when shared option after subcommand then parsed by subcommand', () => {425 const { program, sub } = makeProgram();426 program.parse(['sub', '-g'], { from: 'user' });427 expect(sub.opts().group).toBe(true);428 expect(program.opts().global).toBeUndefined();429 });430});431432// ------------------------------------------------------------------------------433434describe('program with allowUnknownOption', () => {435 test('when passThroughOptions and unknown option then arguments from unknown passed through', () => {436 const program = new commander.Command();437 program438 .passThroughOptions()439 .allowUnknownOption()440 .option('--debug');441442 program.parse(['--unknown', '--debug'], { from: 'user' });443 expect(program.args).toEqual(['--unknown', '--debug']);444 });445446 test('when positionalOptions and unknown option then known options then known option parsed', () => {447 const program = new commander.Command();448 program449 .enablePositionalOptions()450 .allowUnknownOption()451 .option('--debug');452453 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();464 program465 .option('-d, --debug')466 .argument('<args...>');467 return program;468 }469470 test('when passThroughOptions() then option passed through', () => {471 const program = makeProgram();472 program.passThroughOptions();473 program.parse(['foo', '--debug'], { from: 'user' });474 expect(program.args).toEqual(['foo', '--debug']);475 });476477 test('when passThroughOptions(true) then option passed through', () => {478 const program = makeProgram();479 program.passThroughOptions(true);480 program.parse(['foo', '--debug'], { from: 'user' });481 expect(program.args).toEqual(['foo', '--debug']);482 });483484 test('when passThroughOptions(false) then option parsed', () => {485 const program = makeProgram();486 program.passThroughOptions(false);487 program.parse(['foo', '--debug'], { from: 'user' });488 expect(program.args).toEqual(['foo']);489 expect(program.opts().debug).toEqual(true);490 });491});492493// ------------------------------------------------------------------------------494495describe('enablePositionalOptions(xxx) and shared option after subcommand', () => {496 function makeProgram() {497 const program = new commander.Command();498 program499 .option('-d, --debug');500 const sub = program501 .command('sub')502 .option('-d, --debug');503 return { program, sub };504 }505506 test('when enablePositionalOptions() then option parsed by subcommand', () => {507 const { program, sub } = makeProgram();508 program.enablePositionalOptions();509 program.parse(['sub', '--debug'], { from: 'user' });510 expect(sub.opts().debug).toEqual(true);511 });512513 test('when enablePositionalOptions(true) then option parsed by subcommand', () => {514 const { program, sub } = makeProgram();515 program.enablePositionalOptions(true);516 program.parse(['sub', '--debug'], { from: 'user' });517 expect(sub.opts().debug).toEqual(true);518 });519520 test('when enablePositionalOptions(false) then option parsed by program', () => {521 const { program, sub } = makeProgram();522 program.enablePositionalOptions(false);523 program.parse(['sub', '--debug'], { from: 'user' });524 expect(sub.opts().debug).toBeUndefined();525 expect(program.opts().debug).toEqual(true);526 });527});