Skip to content
Myr

command.parseOptions.test.js

This file

Path

tests/command.parseOptions.test.js

335 lines · 13.9 KiB · JavaScript

Last changed by

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

History of this file

Take it

The walk stopped after 200 commits: lines older than 3103c6b point at it.

Add more tests for implies (#1730)John Geebefore adoption3103c6b

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

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 5–6why, one step back
5const { test, describe } = require('node:test');
6const assert = require('node:assert/strict');

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 7–9why, one step back
7
8const execFileAsync = util.promisify(childProcess.execFile);9

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

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

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 11–13why, one step back
11// than high level behaviours which are tested elsewhere.
1213// Tests created from reported bugs

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 14why, one step back
14describe('parsing regression tests', () => {

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 15–20why, one step back
15 // https://github.com/tj/commander.js/issues/1032
16 function createProgram1032() {17 const program = new commander.Command();18 program19 .command('doit [id]')20 .option('--better', 'do it better')

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

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

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 22–28why, one step back
22 return program;
23 }2425 // https://github.com/tj/commander.js/issues/103226 test('when specify subcommand and argument then program.args not empty', () => {27 const program = createProgram1032();28 program.parse(['node', 'test.js', 'doit', 'myid']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 29why, one step back
29 assert(program.args.length > 0);

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 30–35why, one step back
30 });
3132 // https://github.com/tj/commander.js/issues/103233 test('when specify subcommand and option and argument then program.args not empty', () => {34 const program = createProgram1032();35 program.parse(['node', 'test.js', 'doit', '--better', 'myid']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 36why, one step back
36 assert(program.args.length > 0);

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 37–39why, one step back
37 });
3839 // https://github.com/tj/commander.js/issues/508

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

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

Add more tests for implies (#1730)John Geebefore adoption3103c6b

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

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

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

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 53why, one step back
53 assert.equal(stdout, "[ '1', '2', '--dry-run', '3', '4', '5', '6' ]\n");

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 54–56why, one step back
54 });
55});56

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 57why, one step back
57describe('Command.parseOptions()', () => {

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 58–64why, one step back
58 function createProgram() {
59 const program = new commander.Command();60 program61 .option('--global-flag')62 .option('--global-value <value>')63 .command('sub [args...]')64 .option('--sub-flag');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

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

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 66–73why, one step back
66 return program;
67 }6869 // Subcommands are just another potential operand as far as parseOptions is concerned, so limited testing of subcommand as such.7071 test('when empty args then empty results', () => {72 const program = createProgram();73 const result = program.parseOptions([]);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 74why, one step back
74 assert.deepEqual(result, { operands: [], unknown: [] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 75–79why, one step back
75 });
7677 test('when only operands then results has all operands', () => {78 const program = createProgram();79 const result = program.parseOptions('one two three'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 80–83why, one step back
80 assert.deepEqual(result, {
81 operands: ['one', 'two', 'three'],82 unknown: [],83 });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 84–88why, one step back
84 });
8586 test('when subcommand and operand then results has subcommand and operand', () => {87 const program = createProgram();88 const result = program.parseOptions('sub one'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 89why, one step back
89 assert.deepEqual(result, { operands: ['sub', 'one'], unknown: [] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 90–94why, one step back
90 });
9192 test('when program has flag then option removed', () => {93 const program = createProgram();94 const result = program.parseOptions('--global-flag'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 95why, one step back
95 assert.deepEqual(result, { operands: [], unknown: [] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 96–100why, one step back
96 });
9798 test('when program has option with value then option removed', () => {99 const program = createProgram();100 const result = program.parseOptions('--global-value foo'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 101why, one step back
101 assert.deepEqual(result, { operands: [], unknown: [] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 102–106why, one step back
102 });
103104 test('when program has flag before operand then option removed', () => {105 const program = createProgram();106 const result = program.parseOptions('--global-flag arg'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 107why, one step back
107 assert.deepEqual(result, { operands: ['arg'], unknown: [] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 108–112why, one step back
108 });
109110 test('when program has flag after operand then option removed', () => {111 const program = createProgram();112 const result = program.parseOptions('arg --global-flag'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 113why, one step back
113 assert.deepEqual(result, { operands: ['arg'], unknown: [] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 114–118why, one step back
114 });
115116 test('when program has flag after subcommand then option removed', () => {117 const program = createProgram();118 const result = program.parseOptions('sub --global-flag'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 119why, one step back
119 assert.deepEqual(result, { operands: ['sub'], unknown: [] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 120–124why, one step back
120 });
121122 test('when program has unknown option then option returned in unknown', () => {123 const program = createProgram();124 const result = program.parseOptions('--unknown'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 125why, one step back
125 assert.deepEqual(result, { operands: [], unknown: ['--unknown'] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 126–130why, one step back
126 });
127128 test('when program has unknown option before operands then all unknown in same order', () => {129 const program = createProgram();130 const result = program.parseOptions('--unknown arg'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 131why, one step back
131 assert.deepEqual(result, { operands: [], unknown: ['--unknown', 'arg'] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 132–136why, one step back
132 });
133134 test('when program has unknown option after operand then option returned in unknown', () => {135 const program = createProgram();136 const result = program.parseOptions('arg --unknown'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 137why, one step back
137 assert.deepEqual(result, { operands: ['arg'], unknown: ['--unknown'] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 138–142why, one step back
138 });
139140 test('when program has flag after unknown option then flag removed', () => {141 const program = createProgram();142 const result = program.parseOptions('--unknown --global-flag'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 143why, one step back
143 assert.deepEqual(result, { operands: [], unknown: ['--unknown'] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 144–148why, one step back
144 });
145146 test('when subcommand has flag then flag returned as unknown', () => {147 const program = createProgram();148 const result = program.parseOptions('sub --sub-flag'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 149why, one step back
149 assert.deepEqual(result, { operands: ['sub'], unknown: ['--sub-flag'] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 150–154why, one step back
150 });
151152 test('when program has literal before known flag then option returned as operand', () => {153 const program = createProgram();154 const result = program.parseOptions('-- --global-flag'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 155why, one step back
155 assert.deepEqual(result, { operands: ['--global-flag'], unknown: [] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 156–160why, one step back
156 });
157158 test('when program has literal before unknown option then option returned as operand', () => {159 const program = createProgram();160 const result = program.parseOptions('-- --unknown uuu'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 161why, one step back
161 assert.deepEqual(result, { operands: ['--unknown', 'uuu'], unknown: [] });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 162–166why, one step back
162 });
163164 test('when program has literal after unknown option then literal preserved too', () => {165 const program = createProgram();166 const result = program.parseOptions('--unknown1 -- --unknown2'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 167why, one step back
167 assert.deepEqual(result, {

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 168–170why, one step back
168 operands: [],
169 unknown: ['--unknown1', '--', '--unknown2'],170 });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 171–174why, one step back
171 });
172});173174// parse now sets program.args to the result of parseOptions (operands + unknown). Some limited testing.

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 175why, one step back
175describe('Command.args after parsing', () => {

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 176–177why, one step back
176 test('when program has known flag and operand then option removed and operand returned', () => {
177 const program = new commander.Command();

Disallow excess arguments (#2223)John Geebefore adoption58c28eb

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

Add more tests for implies (#1730)John Geebefore adoption3103c6b

line 179why, one step back
179 program.parse('node test.js --global-flag arg'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 180why, one step back
180 assert.deepEqual(program.args, ['arg']);

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 181–187why, one step back
181 });
182183 test('when program has mixed arguments then known options removed and rest returned in same order', () => {184 const program = new commander.Command();185 program186 .allowUnknownOption()187 .option('--global-flag')

Disallow excess arguments (#2223)John Geebefore adoption58c28eb

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

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 190–194why, one step back
190 program.parse(
191 'node test.js aaa --global-flag bbb --unknown ccc --global-value value'.split(192 ' ',193 ),194 );

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 195why, one step back
195 assert.deepEqual(program.args, ['aaa', 'bbb', '--unknown', 'ccc']);

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 196–204why, one step back
196 });
197198 test('when subcommand has mixed arguments then program flags removed and rest returned in same order', () => {199 const program = new commander.Command();200 program201 .option('--global-flag')202 .option('--global-value <value>')203 .command('sub [args...]')204 .option('--sub-flag');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 205–209why, one step back
205 program.parse(
206 'node test.js --global-flag sub --sub-flag arg --global-value value'.split(207 ' ',208 ),209 );

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 210why, one step back
210 assert.deepEqual(program.args, ['sub', '--sub-flag', 'arg']);

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 211–239why, one step back
211 });
212});213214// Utility Conventions: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_01215//216// 12.1 Utility Argument Syntax217// 2. Option-arguments are shown separated from their options by <blank> characters, except when the option-argument is218// enclosed in the '[' and ']' notation to indicate that it is optional. This reflects the situation in which an219// optional option-argument (if present) is included within the same argument string as the option; for a mandatory option-argument,220// it is the next argument. The Utility Syntax Guidelines in Utility Syntax Guidelines require that the option be a separate argument221// from its option-argument and that option-arguments not be optional, but there are some exceptions in POSIX.1-2017 to ensure222// continued operation of historical applications:223//224// a. If the SYNOPSIS of a standard utility shows an option with a mandatory option-argument (as with [ -c option_argument] in the example),225// a conforming application shall use separate arguments for that option and its option-argument. However, a conforming implementation shall226// also permit applications to specify the option and option-argument in the same argument string without intervening <blank> characters.227//228// b. If the SYNOPSIS shows an optional option-argument (as with [ -f[ option_argument]] in the example), a conforming application229// shall place any option-argument for that option directly adjacent to the option in the same argument string, without intervening230// <blank> characters. If the utility receives an argument containing only the option, it shall behave as specified in its description231// for an omitted option-argument; it shall not treat the next argument (if any) as the option-argument for that option.232//233// 12.2 Utility Syntax Guidelines, Guideline 5:234// One or more options without option-arguments, followed by at most one option that takes an option-argument, should be accepted when235// grouped behind one '-' delimiter.236237// Commander conformance:238// - allows separate argument for required option-argument239// - allows value in same argument for short flag with required option-argument

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

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

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 241–246why, one step back
241// - allows value in same argument for short flag with optional option-argument
242// - allows short flags as per 12.2243//244// The focus in this file is testing the behaviours with known vs unknown options.245// See options.values.test.js for more general testing of known options.246

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 247why, one step back
247describe('Posix Utility Conventions', () => {

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 248–254why, one step back
248 function createProgram() {
249 const program = new commander.Command();250 program251 .option('-a,--aaa')252 .option('-b,--bbb')253 .option('-c,--ccc <value>')254 .option('-d,--ddd [value]');

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

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

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 256–261why, one step back
256 return program;
257 }258259 test('when program has combo known boolean short flags then arg removed', () => {260 const program = createProgram();261 const result = program.parseOptions(['-ab']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 262–263why, one step back
262 assert.deepEqual(result, { operands: [], unknown: [] });
263 assert.deepEqual(program.opts(), { aaa: true, bbb: true });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 264–268why, one step back
264 });
265266 test('when program has combo unknown short flags then arg preserved', () => {267 const program = createProgram();268 const result = program.parseOptions(['-pq']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 269–270why, one step back
269 assert.deepEqual(result, { operands: [], unknown: ['-pq'] });
270 assert.deepEqual(program.opts(), {});

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 271–275why, one step back
271 });
272273 test('when program has combo known short option and required value then arg removed', () => {274 const program = createProgram();275 const result = program.parseOptions(['-cvalue']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 276–277why, one step back
276 assert.deepEqual(result, { operands: [], unknown: [] });
277 assert.deepEqual(program.opts(), { ccc: 'value' });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 278–282why, one step back
278 });
279280 test('when program has combo known short option and optional value then arg removed', () => {281 const program = createProgram();282 const result = program.parseOptions(['-dvalue']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 283–284why, one step back
283 assert.deepEqual(result, { operands: [], unknown: [] });
284 assert.deepEqual(program.opts(), { ddd: 'value' });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 285–289why, one step back
285 });
286287 test('when program has known combo short boolean flags and required value then arg removed', () => {288 const program = createProgram();289 const result = program.parseOptions(['-abcvalue']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 290–291why, one step back
290 assert.deepEqual(result, { operands: [], unknown: [] });
291 assert.deepEqual(program.opts(), { aaa: true, bbb: true, ccc: 'value' });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 292–296why, one step back
292 });
293294 test('when program has known combo short boolean flags and optional value then arg removed', () => {295 const program = createProgram();296 const result = program.parseOptions(['-abdvalue']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 297–298why, one step back
297 assert.deepEqual(result, { operands: [], unknown: [] });
298 assert.deepEqual(program.opts(), { aaa: true, bbb: true, ddd: 'value' });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 299–303why, one step back
299 });
300301 test('when program has known long flag=value then arg removed', () => {302 const program = createProgram();303 const result = program.parseOptions(['--ccc=value']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 304–305why, one step back
304 assert.deepEqual(result, { operands: [], unknown: [] });
305 assert.deepEqual(program.opts(), { ccc: 'value' });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 306–310why, one step back
306 });
307308 test('when program has unknown long flag=value then arg preserved', () => {309 const program = createProgram();310 const result = program.parseOptions(['--rrr=value']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 311–312why, one step back
311 assert.deepEqual(result, { operands: [], unknown: ['--rrr=value'] });
312 assert.deepEqual(program.opts(), {});

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 313–318why, one step back
313 });
314315 test('when program has combo optional and combineFlagAndOptionalValue() then treat as value', () => {316 const program = createProgram();317 program.combineFlagAndOptionalValue();318 program.parseOptions(['-db']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 319why, one step back
319 assert.deepEqual(program.opts(), { ddd: 'b' });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 320–325why, one step back
320 });
321322 test('when program has combo optional and combineFlagAndOptionalValue(true) then treat as value', () => {323 const program = createProgram();324 program.combineFlagAndOptionalValue(true);325 program.parseOptions(['-db']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 326why, one step back
326 assert.deepEqual(program.opts(), { ddd: 'b' });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 327–332why, one step back
327 });
328329 test('when program has combo optional and combineFlagAndOptionalValue(false) then treat as boolean', () => {330 const program = createProgram();331 program.combineFlagAndOptionalValue(false);332 program.parseOptions(['-db']);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 333why, one step back
333 assert.deepEqual(program.opts(), { ddd: true, bbb: true });

Add more tests for implies (#1730)John Geebefore adoption3103c6b

lines 334–335why, one step back
334 });
335});