Skip to content
Myr

command.parse.test.js

This file

Path

tests/command.parse.test.js

442 lines · 16.0 KiB · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

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

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

lines 1–3why, one step back
1import * as commander from '../index.js';
2import { test, describe } from 'node:test';3import assert from 'node:assert/strict';

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

lines 4–8why, one step back
4
5// Testing some Electron conventions but not directly using Electron to avoid overheads.6// https://github.com/electron/electron/issues/4690#issuecomment-2174352227// https://www.electronjs.org/docs/api/process#processdefaultapp-readonly8

Add auto-detection of args when node evaluating script code on command-line (#2164)John Geebefore adoptionb9ca390

lines 9–11why, one step back
9// (If mutating process.argv and process.execArgv causes problems, could add utility
10// functions to get them and then mock the functions for tests.)11

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

lines 12–22why, one step back
12describe('Command.parse()', () => {
13 describe('.parse() explicit (from:) and autodetection of args format parsing', () => {14 test('when no args then use process.argv and app/script/args', () => {15 const program = new commander.Command();16 program.argument('[args...]');17 const hold = process.argv;18 process.argv = 'node script.js user'.split(' ');19 program.parse();20 process.argv = hold;21 assert.deepEqual(program.args, ['user']);22 });

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

line 23why, one step back
23

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

lines 24–34why, one step back
24 test('when no args and electron properties and not default app then use process.argv and app/args', () => {
25 const program = new commander.Command();26 program.argument('[args...]');27 const holdArgv = process.argv;28 process.versions.electron = '1.2.3';29 process.argv = 'node user'.split(' ');30 program.parse();31 delete process.versions.electron;32 process.argv = holdArgv;33 assert.deepEqual(program.args, ['user']);34 });

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

line 35why, one step back
35

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

lines 36–41why, one step back
36 test('when args then app/script/args', () => {
37 const program = new commander.Command();38 program.argument('[args...]');39 program.parse('node script.js user'.split(' '));40 assert.deepEqual(program.args, ['user']);41 });

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

line 42why, one step back
42

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

lines 43–48why, one step back
43 test('when args from "node" then app/script/args', () => {
44 const program = new commander.Command();45 program.argument('[args...]');46 program.parse('node script.js user'.split(' '), { from: 'node' });47 assert.deepEqual(program.args, ['user']);48 });

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

line 49why, one step back
49

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

lines 50–58why, one step back
50 test('when args from "electron" and not default app then app/args', () => {
51 const program = new commander.Command();52 program.argument('[args...]');53 const hold = process.defaultApp;54 process.defaultApp = undefined;55 program.parse('customApp user'.split(' '), { from: 'electron' });56 process.defaultApp = hold;57 assert.deepEqual(program.args, ['user']);58 });

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

line 59why, one step back
59

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

lines 60–68why, one step back
60 test('when args from "electron" and default app then app/script/args', () => {
61 const program = new commander.Command();62 program.argument('[args...]');63 const hold = process.defaultApp;64 process.defaultApp = true;65 program.parse('electron script user'.split(' '), { from: 'electron' });66 process.defaultApp = hold;67 assert.deepEqual(program.args, ['user']);68 });

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

line 69why, one step back
69

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

lines 70–75why, one step back
70 test('when args from "user" then args', () => {
71 const program = new commander.Command();72 program.argument('[args...]');73 program.parse('user'.split(' '), { from: 'user' });74 assert.deepEqual(program.args, ['user']);75 });

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

line 76why, one step back
76

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

lines 77–82why, one step back
77 test('when args from "silly" then throw', () => {
78 const program = new commander.Command();79 program.argument('[args...]');80 assert.throws(() => {81 program.parse(['node', 'script.js'], { from: 'silly' });82 });

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

line 83why, one step back
83 });

Add auto-detection of args when node evaluating script code on command-line (#2164)John Geebefore adoptionb9ca390

line 84why, one step back
84

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

lines 85–99why, one step back
85 describe('when node execArgv includes node flags', () => {
86 ['-e', '--eval', '-p', '--print'].forEach((flag) => {87 test(`when node execArgv includes ${flag} then app/args`, () => {88 const program = new commander.Command();89 program.argument('[args...]');90 const holdExecArgv = process.execArgv;91 const holdArgv = process.argv;92 process.argv = ['node', 'user-arg'];93 process.execArgv = [flag, 'console.log("hello, world")'];94 program.parse();95 process.argv = holdArgv;96 process.execArgv = holdExecArgv;97 assert.deepEqual(program.args, ['user-arg']);98 process.execArgv = holdExecArgv;99 });

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

lines 100–102why, one step back
100 });
101 });102 });

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

line 103why, one step back
103

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

lines 104–108why, one step back
104 describe('return type', () => {
105 test('when call .parse then returns program', () => {106 const program = new commander.Command();107 program.argument('[args...]');108 program.action(() => {});

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

line 109why, one step back
109

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

lines 110–112why, one step back
110 const result = program.parse(['node', 'test']);
111 assert.equal(result, program);112 });

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

line 113why, one step back
113

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

lines 114–117why, one step back
114 test('when await .parseAsync then returns program', async () => {
115 const program = new commander.Command();116 program.argument('[args...]');117 program.action(() => {});

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

line 118why, one step back
118

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

lines 119–121why, one step back
119 const result = await program.parseAsync(['node', 'test']);
120 assert.equal(result, program);121 });

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

lines 122–123why, one step back
122 });
123

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

lines 124–125why, one step back
124 // Easy mistake to make when writing unit tests
125 test('when parse strings instead of array then throw', () => {

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

line 126why, one step back
126 const program = new commander.Command();

Disallow excess arguments (#2223)John Geebefore adoption58c28eb

line 127why, one step back
127 program.argument('[args...]');

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

lines 128–130why, one step back
128 assert.throws(() => {
129 program.parse('node', 'test');130 });

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

lines 131–132why, one step back
131 });
132

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

lines 133–142why, one step back
133 describe('parse parameter is treated as readonly, per TypeScript declaration', () => {
134 test('when parse called then parameter does not change', () => {135 const program = new commander.Command();136 program.argument('[args...]');137 program.option('--debug');138 const original = ['node', '--debug', 'arg'];139 const param = original.slice();140 program.parse(param);141 assert.deepEqual(param, original);142 });

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

line 143why, one step back
143

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

lines 144–154why, one step back
144 test('when parse called and parsed args later changed then parameter does not change', () => {
145 const program = new commander.Command();146 program.argument('[args...]');147 program.option('--debug');148 const original = ['node', '--debug', 'arg'];149 const param = original.slice();150 program.parse(param);151 program.args.length = 0;152 program.rawArgs.length = 0;153 assert.deepEqual(param, original);154 });

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

line 155why, one step back
155

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

lines 156–167why, one step back
156 test('when parse called and param later changed then parsed args do not change', () => {
157 const program = new commander.Command();158 program.argument('[args...]');159 program.option('--debug');160 const param = ['node', '--debug', 'arg'];161 program.parse(param);162 const oldArgs = program.args.slice();163 const oldRawArgs = program.rawArgs.slice();164 param.length = 0;165 assert.deepEqual(program.args, oldArgs);166 assert.deepEqual(program.rawArgs, oldRawArgs);167 });

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

lines 168–169why, one step back
168 });
169

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

lines 170–179why, one step back
170 describe('parseAsync parameter is treated as readonly, per TypeScript declaration', () => {
171 test('when parse called then parameter does not change', async () => {172 const program = new commander.Command();173 program.argument('[args...]');174 program.option('--debug');175 const original = ['node', '--debug', 'arg'];176 const param = original.slice();177 await program.parseAsync(param);178 assert.deepEqual(param, original);179 });

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

line 180why, one step back
180

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

lines 181–204why, one step back
181 test('when parseAsync called and parsed args later changed then parameter does not change', async () => {
182 const program = new commander.Command();183 program.argument('[args...]');184 program.option('--debug');185 const original = ['node', '--debug', 'arg'];186 const param = original.slice();187 await program.parseAsync(param);188 program.args.length = 0;189 program.rawArgs.length = 0;190 assert.deepEqual(param, original);191 });192193 test('when parseAsync called and param later changed then parsed args do not change', async () => {194 const program = new commander.Command();195 program.argument('[args...]');196 program.option('--debug');197 const param = ['node', '--debug', 'arg'];198 await program.parseAsync(param);199 const oldArgs = program.args.slice();200 const oldRawArgs = program.rawArgs.slice();201 param.length = 0;202 assert.deepEqual(program.args, oldArgs);203 assert.deepEqual(program.rawArgs, oldRawArgs);204 });

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

line 205why, one step back
205 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 206why, one step back
206

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

lines 207–211why, one step back
207 describe('.parse() called multiple times', () => {
208 test('when use boolean options then option values reset', () => {209 const program = new commander.Command()210 .option('--black')211 .option('--white');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 212why, one step back
212

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

lines 213–214why, one step back
213 program.parse(['--black'], { from: 'user' });
214 assert.deepEqual(program.opts(), { black: true });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 215why, one step back
215

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

lines 216–218why, one step back
216 program.parse(['--white'], { from: 'user' });
217 assert.deepEqual(program.opts(), { white: true });218 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 219why, one step back
219

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

lines 220–223why, one step back
220 test('when use options with option-argument then option values and sources reset', () => {
221 const program = new commander.Command()222 .option('-f, --foo <value>')223 .option('-b, --bar <value>');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 224why, one step back
224

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

lines 225–228why, one step back
225 program.parse(['--foo', 'FOO'], { from: 'user' });
226 assert.deepEqual(program.opts(), { foo: 'FOO' });227 assert.equal(program.getOptionValueSource('foo'), 'cli');228 assert.equal(program.getOptionValueSource('bar'), undefined);

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 229why, one step back
229

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

lines 230–234why, one step back
230 program.parse(['--bar', 'BAR'], { from: 'user' });
231 assert.deepEqual(program.opts(), { bar: 'BAR' });232 assert.equal(program.getOptionValueSource('foo'), undefined);233 assert.equal(program.getOptionValueSource('bar'), 'cli');234 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 235why, one step back
235

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

lines 236–239why, one step back
236 test('when use options with option-argument and default then option values and sources reset', () => {
237 const program = new commander.Command()238 .option('-f, --foo <value>', 'description', 'default-FOO')239 .option('-b, --bar <value>', 'description', 'default-BAR');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 240why, one step back
240

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

lines 241–244why, one step back
241 program.parse(['--foo', 'FOO'], { from: 'user' });
242 assert.deepEqual(program.opts(), { foo: 'FOO', bar: 'default-BAR' });243 assert.equal(program.getOptionValueSource('foo'), 'cli');244 assert.equal(program.getOptionValueSource('bar'), 'default');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 245why, one step back
245

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

lines 246–250why, one step back
246 program.parse(['--bar', 'BAR'], { from: 'user' });
247 assert.deepEqual(program.opts(), { foo: 'default-FOO', bar: 'BAR' });248 assert.equal(program.getOptionValueSource('foo'), 'default');249 assert.equal(program.getOptionValueSource('bar'), 'cli');250 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 251why, one step back
251

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

lines 252–255why, one step back
252 test('when use negated options then option values reset', () => {
253 const program = new commander.Command()254 .option('--no-foo')255 .option('--no-bar');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 256why, one step back
256

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

lines 257–258why, one step back
257 program.parse(['--no-foo'], { from: 'user' });
258 assert.deepEqual(program.opts(), { foo: false, bar: true });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 259why, one step back
259

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

lines 260–262why, one step back
260 program.parse(['--no-bar'], { from: 'user' });
261 assert.deepEqual(program.opts(), { foo: true, bar: false });262 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 263why, one step back
263

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

lines 264–265why, one step back
264 test('when use variadic option then option values reset', () => {
265 const program = new commander.Command().option('--var <items...>');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 266why, one step back
266

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

lines 267–268why, one step back
267 program.parse(['--var', 'a', 'b'], { from: 'user' });
268 assert.deepEqual(program.opts(), { var: ['a', 'b'] });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 269why, one step back
269

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

lines 270–272why, one step back
270 program.parse(['--var', 'c'], { from: 'user' });
271 assert.deepEqual(program.opts(), { var: ['c'] });272 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 273why, one step back
273

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

lines 274–279why, one step back
274 test('when use collect example then option value resets', () => {
275 function collect(value, previous) {276 return previous.concat([value]);277 }278 const program = new commander.Command();279 program.option('-c, --collect <value>', 'repeatable value', collect, []);

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 280why, one step back
280

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

lines 281–282why, one step back
281 program.parse(['-c', 'a', '-c', 'b'], { from: 'user' });
282 assert.deepEqual(program.opts(), { collect: ['a', 'b'] });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 283why, one step back
283

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

lines 284–286why, one step back
284 program.parse(['-c', 'c'], { from: 'user' });
285 assert.deepEqual(program.opts(), { collect: ['c'] });286 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 287why, one step back
287

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

lines 288–298why, one step back
288 test('when use increaseVerbosity example then option value resets', () => {
289 function increaseVerbosity(dummyValue, previous) {290 return previous + 1;291 }292 const program = new commander.Command();293 program.option(294 '-v, --verbose',295 'verbosity that can be increased',296 increaseVerbosity,297 0,298 );

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 299why, one step back
299

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

lines 300–302why, one step back
300 program.parse(['-vvv'], { from: 'user' });
301 assert.deepEqual(program.opts(), { verbose: 3 });302 program.parse(['-vv'], { from: 'user' });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 303why, one step back
303

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

lines 304–307why, one step back
304 assert.deepEqual(program.opts(), { verbose: 2 });
305 program.parse([], { from: 'user' });306 assert.deepEqual(program.opts(), { verbose: 0 });307 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 308why, one step back
308

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

lines 309–312why, one step back
309 test('when use parse and parseAsync then option values reset', async () => {
310 const program = new commander.Command()311 .option('--black')312 .option('--white');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 313why, one step back
313

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

lines 314–318why, one step back
314 program.parse(['--black'], { from: 'user' });
315 assert.deepEqual(program.opts(), { black: true });316 await program.parseAsync(['--white'], { from: 'user' });317 assert.deepEqual(program.opts(), { white: true });318 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 319why, one step back
319

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

lines 320–327why, one step back
320 test('when call subcommand then option values reset (program and subcommand)', () => {
321 const program = new commander.Command()322 .option('--black')323 .option('--white');324 const subcommand = program325 .command('sub')326 .option('--red')327 .option('--green');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 328why, one step back
328

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

lines 329–333why, one step back
329 program.parse(['--black', 'sub', '--red'], { from: 'user' });
330 assert.deepEqual(subcommand.optsWithGlobals(), {331 black: true,332 red: true,333 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 334why, one step back
334

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

lines 335–339why, one step back
335 program.parse(['--white', 'sub', '--green'], { from: 'user' });
336 assert.deepEqual(subcommand.optsWithGlobals(), {337 white: true,338 green: true,339 });

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

line 340why, one step back
340 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 341why, one step back
341

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

lines 342–346why, one step back
342 test('when call different subcommand then no reset because lazy', () => {
343 // This is not a required behaviour, but is the intended behaviour.344 const program = new commander.Command();345 const sub1 = program.command('sub1').option('--red');346 const sub2 = program.command('sub2').option('--green');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 347why, one step back
347

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

lines 348–350why, one step back
348 program.parse(['sub1', '--red'], { from: 'user' });
349 assert.deepEqual(sub1.opts(), { red: true });350 assert.deepEqual(sub2.opts(), {});

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 351why, one step back
351

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

lines 352–355why, one step back
352 program.parse(['sub2', '--green'], { from: 'user' });
353 assert.deepEqual(sub1.opts(), { red: true });354 assert.deepEqual(sub2.opts(), { green: true });355 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 356why, one step back
356

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

lines 357–358why, one step back
357 test('when parse with different implied program name then name changes', () => {
358 const program = new commander.Command();

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 359why, one step back
359

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

lines 360–361why, one step back
360 program.parse(['node', 'script1.js']);
361 assert.equal(program.name(), 'script1');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 362why, one step back
362

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

lines 363–365why, one step back
363 program.parse(['electron', 'script2.js']);
364 assert.equal(program.name(), 'script2');365 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 366why, one step back
366

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

lines 367–371why, one step back
367 test('when parse with different arguments then args change', () => {
368 // weak test, would work without store/reset!369 const program = new commander.Command()370 .argument('<first>')371 .argument('[second]');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 372why, one step back
372

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

lines 373–374why, one step back
373 program.parse(['one', 'two'], { from: 'user' });
374 assert.deepEqual(program.args, ['one', 'two']);

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 375why, one step back
375

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

lines 376–378why, one step back
376 program.parse(['alpha'], { from: 'user' });
377 assert.deepEqual(program.args, ['alpha']);378 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 379why, one step back
379

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

lines 380–385why, one step back
380 test('when parse with different arguments then rawArgs change', () => {
381 // weak test, would work without store/reset!382 const program = new commander.Command()383 .argument('<first>')384 .option('--white')385 .option('--black');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 386why, one step back
386

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

lines 387–388why, one step back
387 program.parse(['--white', 'one'], { from: 'user' });
388 assert.deepEqual(program.rawArgs, ['--white', 'one']);

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 389why, one step back
389

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

lines 390–392why, one step back
390 program.parse(['--black', 'two'], { from: 'user' });
391 assert.deepEqual(program.rawArgs, ['--black', 'two']);392 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 393why, one step back
393

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

lines 394–400why, one step back
394 test('when parse with different arguments then processedArgs change', () => {
395 // weak test, would work without store/reset!396 const program = new commander.Command().argument(397 '<first>',398 'first arg',399 parseFloat,400 );

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 401why, one step back
401

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

lines 402–403why, one step back
402 program.parse([123], { from: 'user' });
403 assert.deepEqual(program.processedArgs, [123]);

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 404why, one step back
404

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

lines 405–407why, one step back
405 program.parse([456], { from: 'user' });
406 assert.deepEqual(program.processedArgs, [456]);407 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 408why, one step back
408

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

lines 409–421why, one step back
409 test('when parse subcommand then reset state before preSubcommand hook called', () => {
410 let hookCalled = false;411 const program = new commander.Command().hook(412 'preSubcommand',413 (thisCommand, subcommand) => {414 hookCalled = true;415 assert.deepEqual(subcommand.opts(), {});416 },417 );418 const subcommand = program419 .command('sub')420 .option('--red')421 .option('--green');

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 422why, one step back
422

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

lines 423–426why, one step back
423 hookCalled = false;
424 program.parse(['sub', '--red'], { from: 'user' });425 assert.equal(hookCalled, true);426 assert.deepEqual(subcommand.opts(), { red: true });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 427why, one step back
427

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

lines 428–432why, one step back
428 hookCalled = false;
429 program.parse(['sub', '--green'], { from: 'user' });430 assert.equal(hookCalled, true);431 assert.deepEqual(subcommand.opts(), { green: true });432 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 433why, one step back
433

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

lines 434–435why, one step back
434 test('when using storeOptionsAsProperties then throw on second parse', () => {
435 const program = new commander.Command().storeOptionsAsProperties();

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

line 436why, one step back
436 program.parse();

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

lines 437–439why, one step back
437 assert.throws(() => {
438 program.parse();439 });

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

line 440why, one step back
440 });

Add save/restore state to allow multiple calls to parse (#2299)John Geebefore adoption49423a2

lines 441–442why, one step back
441 });
442});