Skip to content
Myr

command.parseOptions.test.js

This file

Path

tests/command.parseOptions.test.js

330 lines · 13.7 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 cc2db5b point at it.

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 1–7why, one step back
1const childProcess = require('child_process');
2const commander = require('../');3const path = require('path');4const util = require('util');56const execFileAsync = util.promisify(childProcess.execFile);7

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

line 8why, one step back
8// Combination of parse and parseOptions tests which are more about details

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 9–18why, one step back
9// 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')

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 19why, one step back
19 .action((id, cmd) => {});

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 20–37why, one step back
20 return program;
21 }2223 // https://github.com/tj/commander.js/issues/103224 test('when specify subcommand and argument then program.args not empty', () => {25 const program = createProgram1032();26 program.parse(['node', 'test.js', 'doit', 'myid']);27 expect(program.args.length).toBeGreaterThan(0);28 });2930 // https://github.com/tj/commander.js/issues/103231 test('when specify subcommand and option and argument then program.args not empty', () => {32 const program = createProgram1032();33 program.parse(['node', 'test.js', 'doit', '--better', 'myid']);34 expect(program.args.length).toBeGreaterThan(0);35 });3637 // https://github.com/tj/commander.js/issues/508

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 38why, one step back
38 test('when arguments to executable include option flags then argument order preserved', async () => {

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

line 39why, one step back
39 const pm = path.join(__dirname, 'fixtures/pm');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 40–50why, one step back
40 const { stdout } = await execFileAsync('node', [
41 pm,42 'echo',43 '1',44 '2',45 '--dry-run',46 '3',47 '4',48 '5',49 '6',50 ]);

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 51–62why, one step back
51 expect(stdout).toBe("[ '1', '2', '--dry-run', '3', '4', '5', '6' ]\n");
52 });53});5455describe('parseOptions', () => {56 function createProgram() {57 const program = new commander.Command();58 program59 .option('--global-flag')60 .option('--global-value <value>')61 .command('sub [args...]')62 .option('--sub-flag');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 63why, one step back
63 program.action(() => {});

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 64–161why, one step back
64 return program;
65 }6667 // Subcommands are just another potential operand as far as parseOptions is concerned, so limited testing of subcommand as such.6869 test('when empty args then empty results', () => {70 const program = createProgram();71 const result = program.parseOptions([]);72 expect(result).toEqual({ operands: [], unknown: [] });73 });7475 test('when only operands then results has all operands', () => {76 const program = createProgram();77 const result = program.parseOptions('one two three'.split(' '));78 expect(result).toEqual({ operands: ['one', 'two', 'three'], unknown: [] });79 });8081 test('when subcommand and operand then results has subcommand and operand', () => {82 const program = createProgram();83 const result = program.parseOptions('sub one'.split(' '));84 expect(result).toEqual({ operands: ['sub', 'one'], unknown: [] });85 });8687 test('when program has flag then option removed', () => {88 const program = createProgram();89 const result = program.parseOptions('--global-flag'.split(' '));90 expect(result).toEqual({ operands: [], unknown: [] });91 });9293 test('when program has option with value then option removed', () => {94 const program = createProgram();95 const result = program.parseOptions('--global-value foo'.split(' '));96 expect(result).toEqual({ operands: [], unknown: [] });97 });9899 test('when program has flag before operand then option removed', () => {100 const program = createProgram();101 const result = program.parseOptions('--global-flag arg'.split(' '));102 expect(result).toEqual({ operands: ['arg'], unknown: [] });103 });104105 test('when program has flag after operand then option removed', () => {106 const program = createProgram();107 const result = program.parseOptions('arg --global-flag'.split(' '));108 expect(result).toEqual({ operands: ['arg'], unknown: [] });109 });110111 test('when program has flag after subcommand then option removed', () => {112 const program = createProgram();113 const result = program.parseOptions('sub --global-flag'.split(' '));114 expect(result).toEqual({ operands: ['sub'], unknown: [] });115 });116117 test('when program has unknown option then option returned in unknown', () => {118 const program = createProgram();119 const result = program.parseOptions('--unknown'.split(' '));120 expect(result).toEqual({ operands: [], unknown: ['--unknown'] });121 });122123 test('when program has unknown option before operands then all unknown in same order', () => {124 const program = createProgram();125 const result = program.parseOptions('--unknown arg'.split(' '));126 expect(result).toEqual({ operands: [], unknown: ['--unknown', 'arg'] });127 });128129 test('when program has unknown option after operand then option returned in unknown', () => {130 const program = createProgram();131 const result = program.parseOptions('arg --unknown'.split(' '));132 expect(result).toEqual({ operands: ['arg'], unknown: ['--unknown'] });133 });134135 test('when program has flag after unknown option then flag removed', () => {136 const program = createProgram();137 const result = program.parseOptions('--unknown --global-flag'.split(' '));138 expect(result).toEqual({ operands: [], unknown: ['--unknown'] });139 });140141 test('when subcommand has flag then flag returned as unknown', () => {142 const program = createProgram();143 const result = program.parseOptions('sub --sub-flag'.split(' '));144 expect(result).toEqual({ operands: ['sub'], unknown: ['--sub-flag'] });145 });146147 test('when program has literal before known flag then option returned as operand', () => {148 const program = createProgram();149 const result = program.parseOptions('-- --global-flag'.split(' '));150 expect(result).toEqual({ operands: ['--global-flag'], unknown: [] });151 });152153 test('when program has literal before unknown option then option returned as operand', () => {154 const program = createProgram();155 const result = program.parseOptions('-- --unknown uuu'.split(' '));156 expect(result).toEqual({ operands: ['--unknown', 'uuu'], unknown: [] });157 });158159 test('when program has literal after unknown option then literal preserved too', () => {160 const program = createProgram();161 const result = program.parseOptions('--unknown1 -- --unknown2'.split(' '));

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 162–165why, one step back
162 expect(result).toEqual({
163 operands: [],164 unknown: ['--unknown1', '--', '--unknown2'],165 });

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 166–172why, one step back
166 });
167});168169// parse now sets program.args to the result of parseOptions (operands + unknown). Some limited testing.170describe('parse and program.args', () => {171 test('when program has known flag and operand then option removed and operand returned', () => {172 const program = new commander.Command();

Disallow excess arguments (#2223)John Geebefore adoption58c28eb

line 173why, one step back
173 program.option('--global-flag').argument('[arg...]');

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 174–182why, one step back
174 program.parse('node test.js --global-flag arg'.split(' '));
175 expect(program.args).toEqual(['arg']);176 });177178 test('when program has mixed arguments then known options removed and rest returned in same order', () => {179 const program = new commander.Command();180 program181 .allowUnknownOption()182 .option('--global-flag')

Disallow excess arguments (#2223)John Geebefore adoption58c28eb

lines 183–184why, one step back
183 .option('--global-value <value>')
184 .argument('[arg...]');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 185–189why, one step back
185 program.parse(
186 'node test.js aaa --global-flag bbb --unknown ccc --global-value value'.split(187 ' ',188 ),189 );

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 190–199why, one step back
190 expect(program.args).toEqual(['aaa', 'bbb', '--unknown', 'ccc']);
191 });192193 test('when subcommand has mixed arguments then program flags removed and rest returned in same order', () => {194 const program = new commander.Command();195 program196 .option('--global-flag')197 .option('--global-value <value>')198 .command('sub [args...]')199 .option('--sub-flag');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 200–204why, one step back
200 program.parse(
201 'node test.js --global-flag sub --sub-flag arg --global-value value'.split(202 ' ',203 ),204 );

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 205–234why, one step back
205 expect(program.args).toEqual(['sub', '--sub-flag', 'arg']);
206 });207});208209// Utility Conventions: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_01210//211// 12.1 Utility Argument Syntax212// 2. Option-arguments are shown separated from their options by <blank> characters, except when the option-argument is213// enclosed in the '[' and ']' notation to indicate that it is optional. This reflects the situation in which an214// optional option-argument (if present) is included within the same argument string as the option; for a mandatory option-argument,215// it is the next argument. The Utility Syntax Guidelines in Utility Syntax Guidelines require that the option be a separate argument216// from its option-argument and that option-arguments not be optional, but there are some exceptions in POSIX.1-2017 to ensure217// continued operation of historical applications:218//219// a. If the SYNOPSIS of a standard utility shows an option with a mandatory option-argument (as with [ -c option_argument] in the example),220// a conforming application shall use separate arguments for that option and its option-argument. However, a conforming implementation shall221// also permit applications to specify the option and option-argument in the same argument string without intervening <blank> characters.222//223// b. If the SYNOPSIS shows an optional option-argument (as with [ -f[ option_argument]] in the example), a conforming application224// shall place any option-argument for that option directly adjacent to the option in the same argument string, without intervening225// <blank> characters. If the utility receives an argument containing only the option, it shall behave as specified in its description226// for an omitted option-argument; it shall not treat the next argument (if any) as the option-argument for that option.227//228// 12.2 Utility Syntax Guidelines, Guideline 5:229// One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when230// grouped behind one '-' delimiter.231232// Commander conformance:233// - allows separate argument for required option-argument234// - allows value in same argument for short flag with required option-argument

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

line 235why, one step back
235// - non-conforming: allows separate argument for optional option-argument if it does not start with '-'

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 236–249why, one step back
236// - allows value in same argument for short flag with optional option-argument
237// - allows short flags as per 12.2238//239// The focus in this file is testing the behaviours with known vs unknown options.240// See options.values.test.js for more general testing of known options.241242describe('Utility Conventions', () => {243 function createProgram() {244 const program = new commander.Command();245 program246 .option('-a,--aaa')247 .option('-b,--bbb')248 .option('-c,--ccc <value>')249 .option('-d,--ddd [value]');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 250why, one step back
250 program.action(() => {});

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 251–264why, one step back
251 return program;
252 }253254 test('when program has combo known boolean short flags then arg removed', () => {255 const program = createProgram();256 const result = program.parseOptions(['-ab']);257 expect(result).toEqual({ operands: [], unknown: [] });258 expect(program.opts()).toEqual({ aaa: true, bbb: true });259 });260261 test('when program has combo unknown short flags then arg preserved', () => {262 const program = createProgram();263 const result = program.parseOptions(['-pq']);264 expect(result).toEqual({ operands: [], unknown: ['-pq'] });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 265why, one step back
265 expect(program.opts()).toEqual({});

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 266–306why, one step back
266 });
267268 test('when program has combo known short option and required value then arg removed', () => {269 const program = createProgram();270 const result = program.parseOptions(['-cvalue']);271 expect(result).toEqual({ operands: [], unknown: [] });272 expect(program.opts()).toEqual({ ccc: 'value' });273 });274275 test('when program has combo known short option and optional value then arg removed', () => {276 const program = createProgram();277 const result = program.parseOptions(['-dvalue']);278 expect(result).toEqual({ operands: [], unknown: [] });279 expect(program.opts()).toEqual({ ddd: 'value' });280 });281282 test('when program has known combo short boolean flags and required value then arg removed', () => {283 const program = createProgram();284 const result = program.parseOptions(['-abcvalue']);285 expect(result).toEqual({ operands: [], unknown: [] });286 expect(program.opts()).toEqual({ aaa: true, bbb: true, ccc: 'value' });287 });288289 test('when program has known combo short boolean flags and optional value then arg removed', () => {290 const program = createProgram();291 const result = program.parseOptions(['-abdvalue']);292 expect(result).toEqual({ operands: [], unknown: [] });293 expect(program.opts()).toEqual({ aaa: true, bbb: true, ddd: 'value' });294 });295296 test('when program has known long flag=value then arg removed', () => {297 const program = createProgram();298 const result = program.parseOptions(['--ccc=value']);299 expect(result).toEqual({ operands: [], unknown: [] });300 expect(program.opts()).toEqual({ ccc: 'value' });301 });302303 test('when program has unknown long flag=value then arg preserved', () => {304 const program = createProgram();305 const result = program.parseOptions(['--rrr=value']);306 expect(result).toEqual({ operands: [], unknown: ['--rrr=value'] });

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 307why, one step back
307 expect(program.opts()).toEqual({});

Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b

lines 308–330why, one step back
308 });
309310 test('when program has combo optional and combineFlagAndOptionalValue() then treat as value', () => {311 const program = createProgram();312 program.combineFlagAndOptionalValue();313 program.parseOptions(['-db']);314 expect(program.opts()).toEqual({ ddd: 'b' });315 });316317 test('when program has combo optional and combineFlagAndOptionalValue(true) then treat as value', () => {318 const program = createProgram();319 program.combineFlagAndOptionalValue(true);320 program.parseOptions(['-db']);321 expect(program.opts()).toEqual({ ddd: 'b' });322 });323324 test('when program has combo optional and combineFlagAndOptionalValue(false) then treat as boolean', () => {325 const program = createProgram();326 program.combineFlagAndOptionalValue(false);327 program.parseOptions(['-db']);328 expect(program.opts()).toEqual({ ddd: true, bbb: true });329 });330});