Skip to content
Myr

command.positionalOptions.test.js

This file

Path

tests/command.positionalOptions.test.js

524 lines · 18.4 KiB · JavaScript

Last changed by

ESLint changes preparing for Prettier (#2153)John Geebefore adoption6fc933e

History of this file

Take 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) { /* empty */ }54    expect(mockHelp).toHaveBeenCalled();55  });5657  test('when help option after command-argument then option passed through', () => {58    const program = makeProgram();59    program.parse(['arg', '--help'], { from: 'user' });60    expect(program.args).toEqual(['arg', '--help']);61  });6263  test('when version option after command-argument then option passed through', () => {64    const program = makeProgram();65    program.version('1.2.3');66    program.parse(['arg', '--version'], { from: 'user' });67    expect(program.args).toEqual(['arg', '--version']);68  });69});7071// -----------------------------------------------------------7273describe('program with positionalOptions and subcommand', () => {74  function makeProgram() {75    const program = new commander.Command();76    program77      .enablePositionalOptions()78      .option('-s, --shared <value>')79      .argument('<args...>');80    const sub = program81      .command('sub')82      .argument('[arg]')83      .option('-s, --shared <value>')84      .action(() => {}); // Not used, but normal to have action handler on subcommand.85    return { program, sub };86  }8788  test('when global option before subcommand then global option parsed', () => {89    const { program } = makeProgram();90    program.parse(['--shared', 'program', 'sub'], { from: 'user' });91    expect(program.opts().shared).toEqual('program');92  });9394  test('when shared option after subcommand then parsed by subcommand', () => {95    const { program, sub } = makeProgram();96    program.parse(['sub', '--shared', 'local'], { from: 'user' });97    expect(sub.opts().shared).toEqual('local');98    expect(program.opts().shared).toBeUndefined();99  });100101  test('when shared option after subcommand argument then parsed by subcommand', () => {102    const { program, sub } = makeProgram();103    program.parse(['sub', 'arg', '--shared', 'local'], { from: 'user' });104    expect(sub.opts().shared).toEqual('local');105    expect(sub.args).toEqual(['arg']);106    expect(program.opts().shared).toBeUndefined();107  });108109  test('when shared option before and after subcommand then both parsed', () => {110    const { program, sub } = makeProgram();111    program.parse(['--shared', 'program', 'sub', '--shared', 'local'], { from: 'user' });112    expect(program.opts().shared).toEqual('program');113    expect(sub.opts().shared).toEqual('local');114  });115116  test.each([117    [[], 1, 0],118    [['sub'], 0, 0],119    [['--help'], 1, 0],120    [['sub', '--help'], 0, 1],121    [['sub', 'foo', '--help'], 0, 1],122    [['help'], 1, 0],123    [['help', 'sub'], 0, 1]124  ])('help: when user args %p then program/sub help called %p/%p', (userArgs, expectProgramHelpCount, expectSubHelpCount) => {125    const { program, sub } = makeProgram();126    const mockProgramHelp = jest.fn();127    program128      .exitOverride()129      .configureHelp({ formatHelp: mockProgramHelp });130    const mockSubHelp = jest.fn();131    sub132      .exitOverride()133      .configureHelp({ formatHelp: mockSubHelp });134135    try {136      program.parse(userArgs, { from: 'user' });137    } catch (err) { /* empty */ }138    expect(mockProgramHelp).toHaveBeenCalledTimes(expectProgramHelpCount);139    expect(mockSubHelp).toHaveBeenCalledTimes(expectSubHelpCount);140  });141});142143// ---------------------------------------------------------------144145describe('program with positionalOptions and default subcommand (called sub)', () => {146  function makeProgram() {147    const program = new commander.Command();148    program149      .enablePositionalOptions()150      .option('-s, --shared')151      .option('-g, --global')152      .argument('<args...>');153    const sub = program154      .command('sub', { isDefault: true })155      .argument('[args...]')156      .option('-s, --shared')157      .option('-d, --default')158      .action(() => {}); // Not used, but normal to have action handler on subcommand.159    program.command('another'); // Not used, but normal to have more than one subcommand if have a default.160    return { program, sub };161  }162163  test('when program option before sub option then program option read by program', () => {164    const { program } = makeProgram();165    program.parse(['--global', '--default'], { from: 'user' });166    expect(program.opts().global).toBe(true);167  });168169  test('when program option before sub option then sub option read by sub', () => {170    const { program, sub } = makeProgram();171    program.parse(['--global', '--default'], { from: 'user' });172    expect(sub.opts().default).toBe(true);173  });174175  test('when shared option before sub argument then option read by program', () => {176    const { program } = makeProgram();177    program.parse(['--shared', 'foo'], { from: 'user' });178    expect(program.opts().shared).toBe(true);179  });180181  test('when shared option after sub argument then option read by sub', () => {182    const { program, sub } = makeProgram();183    program.parse(['foo', '--shared'], { from: 'user' });184    expect(sub.opts().shared).toBe(true);185  });186187  test.each([188    [[], 0, 0],189    [['--help'], 1, 0],190    [['help'], 1, 0]191  ])('help: when user args %p then program/sub help called %p/%p', (userArgs, expectProgramHelpCount, expectSubHelpCount) => {192    const { program, sub } = makeProgram();193    const mockProgramHelp = jest.fn();194    program195      .exitOverride()196      .configureHelp({ formatHelp: mockProgramHelp });197    const mockSubHelp = jest.fn();198    sub199      .exitOverride()200      .configureHelp({ formatHelp: mockSubHelp });201202    try {203      program.parse(userArgs, { from: 'user' });204    } catch (err) {  /* empty */ }205    expect(mockProgramHelp).toHaveBeenCalledTimes(expectProgramHelpCount);206    expect(mockSubHelp).toHaveBeenCalledTimes(expectSubHelpCount);207  });208});209210// ------------------------------------------------------------------------------211212describe('subcommand with passThrough', () => {213  function makeProgram() {214    const program = new commander.Command();215    program216      .enablePositionalOptions()217      .option('-s, --shared <value>')218      .argument('<args...>');219    const sub = program220      .command('sub')221      .passThroughOptions()222      .argument('[args...]')223      .option('-s, --shared <value>')224      .option('-d, --debug')225      .action(() => {}); // Not used, but normal to have action handler on subcommand.226    return { program, sub };227  }228229  test('when option before command-argument then option parsed', () => {230    const { program, sub } = makeProgram();231    program.parse(['sub', '--debug', 'arg'], { from: 'user' });232    expect(sub.args).toEqual(['arg']);233    expect(sub.opts().debug).toBe(true);234  });235236  test('when known option after command-argument then option passed through', () => {237    const { program, sub } = makeProgram();238    program.parse(['sub', 'arg', '--debug'], { from: 'user' });239    expect(sub.args).toEqual(['arg', '--debug']);240    expect(sub.opts().debug).toBeUndefined();241  });242243  test('when unknown option after command-argument then option passed through', () => {244    const { program, sub } = makeProgram();245    program.parse(['sub', 'arg', '--pass'], { from: 'user' });246    expect(sub.args).toEqual(['arg', '--pass']);247  });248249  test('when action handler and unknown option after command-argument then option passed through', () => {250    const { program, sub } = makeProgram();251    const mockAction = jest.fn();252    sub.action(mockAction);253    program.parse(['sub', 'arg', '--pass'], { from: 'user' });254    expect(mockAction).toHaveBeenCalledWith(['arg', '--pass'], sub.opts(), sub);255  });256257  test('when help option after command-argument then option passed through', () => {258    const { program, sub } = makeProgram();259    program.parse(['sub', 'arg', '--help'], { from: 'user' });260    expect(sub.args).toEqual(['arg', '--help']);261  });262263  test('when version option after command-argument then option passed through', () => {264    const { program, sub } = makeProgram();265    program.version('1.2.3');266    program.parse(['sub', 'arg', '--version'], { from: 'user' });267    expect(sub.args).toEqual(['arg', '--version']);268  });269270  test('when shared option before sub and after sub and after sub parameter then all three parsed', () => {271    const { program, sub } = makeProgram();272    program.parse(['--shared=global', 'sub', '--shared=local', 'arg', '--shared'], { from: 'user' });273    expect(program.opts().shared).toEqual('global');274    expect(sub.opts().shared).toEqual('local');275    expect(sub.args).toEqual(['arg', '--shared']);276  });277});278279// ------------------------------------------------------------------------------280281describe('default command with passThrough', () => {282  function makeProgram() {283    const program = new commander.Command();284    program285      .enablePositionalOptions();286    const sub = program287      .command('sub', { isDefault: true })288      .passThroughOptions()289      .argument('[args...]')290      .option('-d, --debug')291      .action(() => {}); // Not used, but normal to have action handler on subcommand.292    return { program, sub };293  }294295  test('when option before command-argument then option parsed', () => {296    const { program, sub } = makeProgram();297    program.parse(['--debug', 'arg'], { from: 'user' });298    expect(sub.args).toEqual(['arg']);299    expect(sub.opts().debug).toBe(true);300  });301302  test('when known option after command-argument then option passed through', () => {303    const { program, sub } = makeProgram();304    program.parse(['arg', '--debug'], { from: 'user' });305    expect(sub.args).toEqual(['arg', '--debug']);306    expect(sub.opts().debug).toBeUndefined();307  });308309  test('when unknown option after command-argument then option passed through', () => {310    const { program, sub } = makeProgram();311    program.parse(['arg', '--pass'], { from: 'user' });312    expect(sub.args).toEqual(['arg', '--pass']);313  });314315  test('when action handler and unknown option after command-argument then option passed through', () => {316    const { program, sub } = makeProgram();317    const mockAction = jest.fn();318    sub.action(mockAction);319    program.parse(['arg', '--pass'], { from: 'user' });320    expect(mockAction).toHaveBeenCalledWith(['arg', '--pass'], sub.opts(), sub);321  });322});323324// ------------------------------------------------------------------------------325326describe('program with action handler and positionalOptions and subcommand', () => {327  function makeProgram() {328    const program = new commander.Command();329    program330      .enablePositionalOptions()331      .option('-g, --global')332      .argument('<args...>')333      .action(() => {});334    const sub = program335      .command('sub')336      .argument('[arg]')337      .action(() => {});338    return { program, sub };339  }340341  test('when global option before parameter then global option parsed', () => {342    const { program } = makeProgram();343    program.parse(['--global', 'foo'], { from: 'user' });344    expect(program.opts().global).toBe(true);345  });346347  test('when global option after parameter then global option parsed', () => {348    const { program } = makeProgram();349    program.parse(['foo', '--global'], { from: 'user' });350    expect(program.opts().global).toBe(true);351  });352353  test('when global option after parameter with same name as subcommand then global option parsed', () => {354    const { program } = makeProgram();355    program.parse(['foo', 'sub', '--global'], { from: 'user' });356    expect(program.opts().global).toBe(true);357  });358});359360// ------------------------------------------------------------------------------361362describe('broken passThrough', () => {363  test('when program not positional and turn on passThroughOptions in subcommand then error', () => {364    const program = new commander.Command();365    const sub = program.command('sub');366367    expect(() => {368      sub.passThroughOptions();369    }).toThrow();370  });371372  test('when program not positional and add subcommand with passThroughOptions then error', () => {373    const program = new commander.Command();374    const sub = new commander.Command('sub')375      .passThroughOptions();376377    expect(() => {378      program.addCommand(sub);379    }).toThrow();380  });381});382383// ------------------------------------------------------------------------------384385describe('program with action handler and passThrough and subcommand', () => {386  function makeProgram() {387    const program = new commander.Command();388    program389      .passThroughOptions()390      .option('-g, --global')391      .argument('<args...>')392      .action(() => {});393    const sub = program394      .command('sub')395      .argument('[arg]')396      .option('-g, --group')397      .option('-d, --debug')398      .action(() => {});399    return { program, sub };400  }401402  test('when global option before parameter then global option parsed', () => {403    const { program } = makeProgram();404    program.parse(['--global', 'foo'], { from: 'user' });405    expect(program.opts().global).toBe(true);406  });407408  test('when global option after parameter then passed through', () => {409    const { program } = makeProgram();410    program.parse(['foo', '--global'], { from: 'user' });411    expect(program.args).toEqual(['foo', '--global']);412  });413414  test('when subcommand option after subcommand then option parsed', () => {415    const { program, sub } = makeProgram();416    program.parse(['sub', '--debug'], { from: 'user' });417    expect(sub.opts().debug).toBe(true);418  });419420  // This is somewhat of a side-affect of supporting previous test.421  test('when shared option after subcommand then parsed by subcommand', () => {422    const { program, sub } = makeProgram();423    program.parse(['sub', '-g'], { from: 'user' });424    expect(sub.opts().group).toBe(true);425    expect(program.opts().global).toBeUndefined();426  });427});428429// ------------------------------------------------------------------------------430431describe('program with allowUnknownOption', () => {432  test('when passThroughOptions and unknown option then arguments from unknown passed through', () => {433    const program = new commander.Command();434    program435      .passThroughOptions()436      .allowUnknownOption()437      .option('--debug');438439    program.parse(['--unknown', '--debug'], { from: 'user' });440    expect(program.args).toEqual(['--unknown', '--debug']);441  });442443  test('when positionalOptions and unknown option then known options then known option parsed', () => {444    const program = new commander.Command();445    program446      .enablePositionalOptions()447      .allowUnknownOption()448      .option('--debug');449450    program.parse(['--unknown', '--debug'], { from: 'user' });451    expect(program.opts().debug).toBe(true);452    expect(program.args).toEqual(['--unknown']);453  });454});455456// ------------------------------------------------------------------------------457458describe('passThroughOptions(xxx) and option after command-argument', () => {459  function makeProgram() {460    const program = new commander.Command();461    program462      .option('-d, --debug')463      .argument('<args...>');464    return program;465  }466467  test('when passThroughOptions() then option passed through', () => {468    const program = makeProgram();469    program.passThroughOptions();470    program.parse(['foo', '--debug'], { from: 'user' });471    expect(program.args).toEqual(['foo', '--debug']);472  });473474  test('when passThroughOptions(true) then option passed through', () => {475    const program = makeProgram();476    program.passThroughOptions(true);477    program.parse(['foo', '--debug'], { from: 'user' });478    expect(program.args).toEqual(['foo', '--debug']);479  });480481  test('when passThroughOptions(false) then option parsed', () => {482    const program = makeProgram();483    program.passThroughOptions(false);484    program.parse(['foo', '--debug'], { from: 'user' });485    expect(program.args).toEqual(['foo']);486    expect(program.opts().debug).toEqual(true);487  });488});489490// ------------------------------------------------------------------------------491492describe('enablePositionalOptions(xxx) and shared option after subcommand', () => {493  function makeProgram() {494    const program = new commander.Command();495    program496      .option('-d, --debug');497    const sub = program498      .command('sub')499      .option('-d, --debug');500    return { program, sub };501  }502503  test('when enablePositionalOptions() then option parsed by subcommand', () => {504    const { program, sub } = makeProgram();505    program.enablePositionalOptions();506    program.parse(['sub', '--debug'], { from: 'user' });507    expect(sub.opts().debug).toEqual(true);508  });509510  test('when enablePositionalOptions(true) then option parsed by subcommand', () => {511    const { program, sub } = makeProgram();512    program.enablePositionalOptions(true);513    program.parse(['sub', '--debug'], { from: 'user' });514    expect(sub.opts().debug).toEqual(true);515  });516517  test('when enablePositionalOptions(false) then option parsed by program', () => {518    const { program, sub } = makeProgram();519    program.enablePositionalOptions(false);520    program.parse(['sub', '--debug'], { from: 'user' });521    expect(sub.opts().debug).toBeUndefined();522    expect(program.opts().debug).toEqual(true);523  });524});