Skip to content
Myr

command.positionalOptions.test.js

This file

Path

tests/command.positionalOptions.test.js

515 lines · 18.0 KiB · JavaScript

Last changed by

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

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) {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// ------------------------------------------------------------------------------364365test('when program not positional and turn on passthrough in subcommand then error', () => {366  const program = new commander.Command();367  const sub = program.command('sub');368369  expect(() => {370    sub.passThroughOptions();371  }).toThrow();372});373374// ------------------------------------------------------------------------------375376describe('program with action handler and passThrough and subcommand', () => {377  function makeProgram() {378    const program = new commander.Command();379    program380      .passThroughOptions()381      .option('-g, --global')382      .argument('<args...>')383      .action(() => {});384    const sub = program385      .command('sub')386      .argument('[arg]')387      .option('-g, --group')388      .option('-d, --debug')389      .action(() => {});390    return { program, sub };391  }392393  test('when global option before parameter then global option parsed', () => {394    const { program } = makeProgram();395    program.parse(['--global', 'foo'], { from: 'user' });396    expect(program.opts().global).toBe(true);397  });398399  test('when global option after parameter then passed through', () => {400    const { program } = makeProgram();401    program.parse(['foo', '--global'], { from: 'user' });402    expect(program.args).toEqual(['foo', '--global']);403  });404405  test('when subcommand option after subcommand then option parsed', () => {406    const { program, sub } = makeProgram();407    program.parse(['sub', '--debug'], { from: 'user' });408    expect(sub.opts().debug).toBe(true);409  });410411  // This is somewhat of a side-affect of supporting previous test.412  test('when shared option after subcommand then parsed by subcommand', () => {413    const { program, sub } = makeProgram();414    program.parse(['sub', '-g'], { from: 'user' });415    expect(sub.opts().group).toBe(true);416    expect(program.opts().global).toBeUndefined();417  });418});419420// ------------------------------------------------------------------------------421422describe('program with allowUnknownOption', () => {423  test('when passThroughOptions and unknown option then arguments from unknown passed through', () => {424    const program = new commander.Command();425    program426      .passThroughOptions()427      .allowUnknownOption()428      .option('--debug');429430    program.parse(['--unknown', '--debug'], { from: 'user' });431    expect(program.args).toEqual(['--unknown', '--debug']);432  });433434  test('when positionalOptions and unknown option then known options then known option parsed', () => {435    const program = new commander.Command();436    program437      .enablePositionalOptions()438      .allowUnknownOption()439      .option('--debug');440441    program.parse(['--unknown', '--debug'], { from: 'user' });442    expect(program.opts().debug).toBe(true);443    expect(program.args).toEqual(['--unknown']);444  });445});446447// ------------------------------------------------------------------------------448449describe('passThroughOptions(xxx) and option after command-argument', () => {450  function makeProgram() {451    const program = new commander.Command();452    program453      .option('-d, --debug')454      .argument('<args...>');455    return program;456  }457458  test('when passThroughOptions() then option passed through', () => {459    const program = makeProgram();460    program.passThroughOptions();461    program.parse(['foo', '--debug'], { from: 'user' });462    expect(program.args).toEqual(['foo', '--debug']);463  });464465  test('when passThroughOptions(true) then option passed through', () => {466    const program = makeProgram();467    program.passThroughOptions(true);468    program.parse(['foo', '--debug'], { from: 'user' });469    expect(program.args).toEqual(['foo', '--debug']);470  });471472  test('when passThroughOptions(false) then option parsed', () => {473    const program = makeProgram();474    program.passThroughOptions(false);475    program.parse(['foo', '--debug'], { from: 'user' });476    expect(program.args).toEqual(['foo']);477    expect(program.opts().debug).toEqual(true);478  });479});480481// ------------------------------------------------------------------------------482483describe('enablePositionalOptions(xxx) and shared option after subcommand', () => {484  function makeProgram() {485    const program = new commander.Command();486    program487      .option('-d, --debug');488    const sub = program489      .command('sub')490      .option('-d, --debug');491    return { program, sub };492  }493494  test('when enablePositionalOptions() then option parsed by subcommand', () => {495    const { program, sub } = makeProgram();496    program.enablePositionalOptions();497    program.parse(['sub', '--debug'], { from: 'user' });498    expect(sub.opts().debug).toEqual(true);499  });500501  test('when enablePositionalOptions(true) then option parsed by subcommand', () => {502    const { program, sub } = makeProgram();503    program.enablePositionalOptions(true);504    program.parse(['sub', '--debug'], { from: 'user' });505    expect(sub.opts().debug).toEqual(true);506  });507508  test('when enablePositionalOptions(false) then option parsed by program', () => {509    const { program, sub } = makeProgram();510    program.enablePositionalOptions(false);511    program.parse(['sub', '--debug'], { from: 'user' });512    expect(sub.opts().debug).toBeUndefined();513    expect(program.opts().debug).toEqual(true);514  });515});