command.parseOptions.test.js
This file
Path
tests/command.parseOptions.test.js
291 lines · 12.8 KiB · JavaScript
Last changed by
Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7
History of this fileTake it
parseOptions rework phase 1 (#1138)John Geebefore adoption62234f6
line 1why, one step back1const childProcess = require('child_process');Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7
line 2why, one step back2const commander = require('../');parseOptions rework phase 1 (#1138)John Geebefore adoption62234f6
line 3why, one step back3const path = require('path');Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7
line 4why, one step back4const util = require('util');parseOptions rework phase 1 (#1138)John Geebefore adoption62234f6
line 5why, one step back5Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7
lines 6–7why, one step back6const execFileAsync = util.promisify(childProcess.execFile);7parseOptions rework phase 1 (#1138)John Geebefore adoption62234f6
lines 8–38why, one step back8// Combination of parse and parseOptions tests which are are more about details9// than high level behaviours which are tested elsewhere.1011// Tests created from reported bugs12describe('regression tests', () => {13 // https://github.com/tj/commander.js/issues/103214 function createProgram1032() {15 const program = new commander.Command();16 program17 .command('doit [id]')18 .option('--better', 'do it better')19 .action((id, cmd) => {20 });21 return program;22 };2324 // https://github.com/tj/commander.js/issues/103225 test('when specify subcommand and argument then program.args not empty', () => {26 const program = createProgram1032();27 program.parse(['node', 'test.js', 'doit', 'myid']);28 expect(program.args.length).toBeGreaterThan(0);29 });3031 // https://github.com/tj/commander.js/issues/103232 test('when specify subcommand and option and argument then program.args not empty', () => {33 const program = createProgram1032();34 program.parse(['node', 'test.js', 'doit', '--better', 'myid']);35 expect(program.args.length).toBeGreaterThan(0);36 });3738 // https://github.com/tj/commander.js/issues/508Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7
line 39why, one step back39 test('when arguments to executable include option flags then argument order preserved', async() => {parseOptions rework phase 1 (#1138)John Geebefore adoption62234f6
line 40why, one step back40 const pm = path.join(__dirname, 'fixtures/pm');Update dependencies and rewrite tests (#1167)John Geebefore adoptiond736ff7
lines 41–42why, one step back41 const { stdout } = await execFileAsync('node', [pm, 'echo', '1', '2', '--dry-run', '3', '4', '5', '6']);42 expect(stdout).toBe("[ '1', '2', '--dry-run', '3', '4', '5', '6' ]\n");parseOptions rework phase 1 (#1138)John Geebefore adoption62234f6
lines 43–186why, one step back43 });44});4546describe('parseOptions', () => {47 function createProgram() {48 const program = new commander.Command();49 program50 .option('--global-flag')51 .option('--global-value <value>')52 .command('sub [args...]')53 .option('--sub-flag');54 program55 .action(() => { });56 return program;57 };5859 // Subcommands are just another potential operand as far as parseOptions is concerned, so limited testing of subcommand as such.6061 test('when empty args then empty results', () => {62 const program = createProgram();63 const result = program.parseOptions([]);64 expect(result).toEqual({ operands: [], unknown: [] });65 });6667 test('when only operands then results has all operands', () => {68 const program = createProgram();69 const result = program.parseOptions('one two three'.split(' '));70 expect(result).toEqual({ operands: ['one', 'two', 'three'], unknown: [] });71 });7273 test('when subcommand and operand then results has subcommand and operand', () => {74 const program = createProgram();75 const result = program.parseOptions('sub one'.split(' '));76 expect(result).toEqual({ operands: ['sub', 'one'], unknown: [] });77 });7879 test('when program has flag then option removed', () => {80 const program = createProgram();81 const result = program.parseOptions('--global-flag'.split(' '));82 expect(result).toEqual({ operands: [], unknown: [] });83 });8485 test('when program has option with value then option removed', () => {86 const program = createProgram();87 const result = program.parseOptions('--global-value foo'.split(' '));88 expect(result).toEqual({ operands: [], unknown: [] });89 });9091 test('when program has flag before operand then option removed', () => {92 const program = createProgram();93 const result = program.parseOptions('--global-flag arg'.split(' '));94 expect(result).toEqual({ operands: ['arg'], unknown: [] });95 });9697 test('when program has flag after operand then option removed', () => {98 const program = createProgram();99 const result = program.parseOptions('arg --global-flag'.split(' '));100 expect(result).toEqual({ operands: ['arg'], unknown: [] });101 });102103 test('when program has flag after subcommand then option removed', () => {104 const program = createProgram();105 const result = program.parseOptions('sub --global-flag'.split(' '));106 expect(result).toEqual({ operands: ['sub'], unknown: [] });107 });108109 test('when program has unknown option then option returned in unknown', () => {110 const program = createProgram();111 const result = program.parseOptions('--unknown'.split(' '));112 expect(result).toEqual({ operands: [], unknown: ['--unknown'] });113 });114115 test('when program has unknown option before operands then all unknown in same order', () => {116 const program = createProgram();117 const result = program.parseOptions('--unknown arg'.split(' '));118 expect(result).toEqual({ operands: [], unknown: ['--unknown', 'arg'] });119 });120121 test('when program has unknown option after operand then option returned in unknown', () => {122 const program = createProgram();123 const result = program.parseOptions('arg --unknown'.split(' '));124 expect(result).toEqual({ operands: ['arg'], unknown: ['--unknown'] });125 });126127 test('when program has flag after unknown option then flag removed', () => {128 const program = createProgram();129 const result = program.parseOptions('--unknown --global-flag'.split(' '));130 expect(result).toEqual({ operands: [], unknown: ['--unknown'] });131 });132133 test('when subcommand has flag then flag returned as unknown', () => {134 const program = createProgram();135 const result = program.parseOptions('sub --sub-flag'.split(' '));136 expect(result).toEqual({ operands: ['sub'], unknown: ['--sub-flag'] });137 });138139 test('when program has literal before known flag then option returned as operand', () => {140 const program = createProgram();141 const result = program.parseOptions('-- --global-flag'.split(' '));142 expect(result).toEqual({ operands: ['--global-flag'], unknown: [] });143 });144145 test('when program has literal before unknown option then option returned as operand', () => {146 const program = createProgram();147 const result = program.parseOptions('-- --unknown uuu'.split(' '));148 expect(result).toEqual({ operands: ['--unknown', 'uuu'], unknown: [] });149 });150151 test('when program has literal after unknown option then literal preserved too', () => {152 const program = createProgram();153 const result = program.parseOptions('--unknown1 -- --unknown2'.split(' '));154 expect(result).toEqual({ operands: [], unknown: ['--unknown1', '--', '--unknown2'] });155 });156});157158// parse now sets program.args to the result of parseOptions (operands + unknown). Some limited testing.159describe('parse and program.args', () => {160 test('when program has known flag and operand then option removed and operand returned', () => {161 const program = new commander.Command();162 program163 .option('--global-flag');164 program.parse('node test.js --global-flag arg'.split(' '));165 expect(program.args).toEqual(['arg']);166 });167168 test('when program has mixed arguments then known options removed and rest returned in same order', () => {169 const program = new commander.Command();170 program171 .allowUnknownOption()172 .option('--global-flag')173 .option('--global-value <value>');174 program.parse('node test.js aaa --global-flag bbb --unknown ccc --global-value value'.split(' '));175 expect(program.args).toEqual(['aaa', 'bbb', '--unknown', 'ccc']);176 });177178 test('when subcommand has mixed arguments then program flags removed and rest returned in same order', () => {179 const program = new commander.Command();180 program181 .option('--global-flag')182 .option('--global-value <value>')183 .command('sub [args...]')184 .option('--sub-flag');185 program.parse('node test.js --global-flag sub --sub-flag arg --global-value value'.split(' '));186 expect(program.args).toEqual(['sub', '--sub-flag', 'arg']);Combine normalize and parseoptions, and update combined option handling (#1152)John Geebefore adoption1691344
lines 187–289why, one step back187 });188});189190// Utility Conventions: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_01191//192// 12.1 Utility Argument Syntax193// 2. Option-arguments are shown separated from their options by <blank> characters, except when the option-argument is194// enclosed in the '[' and ']' notation to indicate that it is optional. This reflects the situation in which an195// optional option-argument (if present) is included within the same argument string as the option; for a mandatory option-argument,196// it is the next argument. The Utility Syntax Guidelines in Utility Syntax Guidelines require that the option be a separate argument197// from its option-argument and that option-arguments not be optional, but there are some exceptions in POSIX.1-2017 to ensure198// continued operation of historical applications:199//200// a. If the SYNOPSIS of a standard utility shows an option with a mandatory option-argument (as with [ -c option_argument] in the example),201// a conforming application shall use separate arguments for that option and its option-argument. However, a conforming implementation shall202// also permit applications to specify the option and option-argument in the same argument string without intervening <blank> characters.203//204// b. If the SYNOPSIS shows an optional option-argument (as with [ -f[ option_argument]] in the example), a conforming application205// shall place any option-argument for that option directly adjacent to the option in the same argument string, without intervening206// <blank> characters. If the utility receives an argument containing only the option, it shall behave as specified in its description207// for an omitted option-argument; it shall not treat the next argument (if any) as the option-argument for that option.208//209// 12.2 Utility Syntax Guidelines, Guideline 5:210// One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when211// grouped behind one '-' delimiter.212213// Commander conformance:214// - allows separate argument for required option-argument215// - allows value in same argument for short flag with required option-argument216// - non-conforming: allows separate argument for optional option-argument if does not start with '-'217// - allows value in same argument for short flag with optional option-argument218// - allows short flags as per 12.2219//220// The focus in this file is testing the behaviours with known vs unknown options.221// See options.values.test.js for more general testing of known options.222223describe('Utility Conventions', () => {224 function createProgram() {225 const program = new commander.Command();226 program227 .option('-a,--aaa')228 .option('-b,--bbb')229 .option('-c,--ccc <value>')230 .option('-d,--ddd [value]');231 program232 .action(() => { });233 return program;234 };235236 test('when program has combo known boolean short flags then arg removed', () => {237 const program = createProgram();238 const result = program.parseOptions(['-ab']);239 expect(result).toEqual({ operands: [], unknown: [] });240 expect(program.opts()).toEqual({ aaa: true, bbb: true });241 });242243 test('when program has combo unknown short flags then arg preserved', () => {244 const program = createProgram();245 const result = program.parseOptions(['-pq']);246 expect(result).toEqual({ operands: [], unknown: ['-pq'] });247 expect(program.opts()).toEqual({ });248 });249250 test('when program has combo known short option and required value then arg removed', () => {251 const program = createProgram();252 const result = program.parseOptions(['-cvalue']);253 expect(result).toEqual({ operands: [], unknown: [] });254 expect(program.opts()).toEqual({ ccc: 'value' });255 });256257 test('when program has combo known short option and optional value then arg removed', () => {258 const program = createProgram();259 const result = program.parseOptions(['-dvalue']);260 expect(result).toEqual({ operands: [], unknown: [] });261 expect(program.opts()).toEqual({ ddd: 'value' });262 });263264 test('when program has known combo short boolean flags and required value then arg removed', () => {265 const program = createProgram();266 const result = program.parseOptions(['-abcvalue']);267 expect(result).toEqual({ operands: [], unknown: [] });268 expect(program.opts()).toEqual({ aaa: true, bbb: true, ccc: 'value' });269 });270271 test('when program has known combo short boolean flags and optional value then arg removed', () => {272 const program = createProgram();273 const result = program.parseOptions(['-abdvalue']);274 expect(result).toEqual({ operands: [], unknown: [] });275 expect(program.opts()).toEqual({ aaa: true, bbb: true, ddd: 'value' });276 });277278 test('when program has known long flag=value then arg removed', () => {279 const program = createProgram();280 const result = program.parseOptions(['--ccc=value']);281 expect(result).toEqual({ operands: [], unknown: [] });282 expect(program.opts()).toEqual({ ccc: 'value' });283 });284285 test('when program has unknown long flag=value then arg preserved', () => {286 const program = createProgram();287 const result = program.parseOptions(['--rrr=value']);288 expect(result).toEqual({ operands: [], unknown: ['--rrr=value'] });289 expect(program.opts()).toEqual({ });parseOptions rework phase 1 (#1138)John Geebefore adoption62234f6
lines 290–291why, one step back290 });291});