argument.variadic.test.js
This file
Path
tests/argument.variadic.test.js
133 lines · 4.6 KiB · JavaScript
Last changed by
Merge from develop into release/15.x (with minor conflicts and test migrations)John Geebefore adoption26e3ba3
History of this fileTake it
The walk stopped after 200 commits: lines older than 471ec40 point at it.
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–3why, one step back1import * as commander from '../index.js';2import { test, describe } from 'node:test';3import assert from 'node:assert/strict';Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 4–6why, one step back45// Testing variadic arguments. Testing all the action arguments, but could test just variadicArg.6Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 7why, one step back7describe('Command variadic argument using .argument()', (t) => {Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 8–9why, one step back8 test('when no extra arguments specified for program then variadic arg is empty array', (t) => {9 const actionMock = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 10why, one step back10 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 11why, one step back11 program.argument('<id>').argument('[variadicArg...]').action(actionMock);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 12–14why, one step back1213 program.parse(['node', 'test', 'id']);14Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 15–17why, one step back15 const callArgs = actionMock.mock.calls[0].arguments;16 assert.equal(callArgs[0], 'id');17 assert.deepEqual(callArgs[1], []);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 18–19why, one step back18 });19Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 20–21why, one step back20 test('when extra arguments specified for program then variadic arg is array of values', (t) => {21 const actionMock = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 22–30why, one step back22 const program = new commander.Command();23 program24 .addArgument(new commander.Argument('<id>'))25 .argument('[variadicArg...]')26 .action(actionMock);27 const extraArguments = ['a', 'b', 'c'];2829 program.parse(['node', 'test', 'id', ...extraArguments]);30Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 31–33why, one step back31 const callArgs = actionMock.mock.calls[0].arguments;32 assert.equal(callArgs[0], 'id');33 assert.deepEqual(callArgs[1], extraArguments);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 34–35why, one step back34 });35Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 36–37why, one step back36 test('when no extra arguments specified for command then variadic arg is empty array', (t) => {37 const actionMock = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 38why, one step back38 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 39why, one step back39 const cmd = program.command('sub [variadicArg...]').action(actionMock);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 40–42why, one step back4041 program.parse(['node', 'test', 'sub']);42Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 43–44why, one step back43 const callArgs = actionMock.mock.calls[0].arguments;44 assert.deepEqual(callArgs[0], []);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 45–46why, one step back45 });46Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 47–48why, one step back47 test('when extra arguments specified for command then variadic arg is array of values', (t) => {48 const actionMock = t.mock.fn();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 49why, one step back49 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 50why, one step back50 const cmd = program.command('sub [variadicArg...]').action(actionMock);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 51–54why, one step back51 const extraArguments = ['a', 'b', 'c'];5253 program.parse(['node', 'test', 'sub', ...extraArguments]);54Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 55–56why, one step back55 const callArgs = actionMock.mock.calls[0].arguments;56 assert.deepEqual(callArgs[0], extraArguments);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 57–61why, one step back57 });5859 test('when program variadic argument not last then error', () => {60 const program = new commander.Command();61Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 62why, one step back62 assert.throws(() => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 63why, one step back63 program.argument('<variadicArg...>').argument('[optionalArg]');Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 64why, one step back64 }, /only the last argument can be variadic 'variadicArg'/);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 65–69why, one step back65 });6667 test('when command variadic argument not last then error', () => {68 const program = new commander.Command();69Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 70why, one step back70 assert.throws(() => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 71why, one step back71 program.command('sub <variadicArg...> [optionalArg]');Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 72why, one step back72 }, /only the last argument can be variadic 'variadicArg'/);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 73–76why, one step back73 });7475 test('when variadic argument then usage shows variadic', () => {76 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 77why, one step back77 program.name('foo').argument('[args...]');Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 78why, one step back78Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 79why, one step back79 assert.equal(program.usage(), '[options] [args...]');Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 80–86why, one step back80 });8182 test('when variadic used with choices and one value then set in array', () => {83 const program = new commander.Command();84 let passedArg;85 program86 .addArgument(new commander.Argument('<value...>').choices(['one', 'two']))Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 87–89why, one step back87 .action((value) => {88 passedArg = value;89 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 90–91why, one step back9091 program.parse(['one'], { from: 'user' });Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 92why, one step back92 assert.deepEqual(passedArg, ['one']);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 93–99why, one step back93 });9495 test('when variadic used with choices and two values then set in array', () => {96 const program = new commander.Command();97 let passedArg;98 program99 .addArgument(new commander.Argument('<value...>').choices(['one', 'two']))Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 100–102why, one step back100 .action((value) => {101 passedArg = value;102 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 103–104why, one step back103104 program.parse(['one', 'two'], { from: 'user' });Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 105why, one step back105 assert.deepEqual(passedArg, ['one', 'two']);Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 106why, one step back106 });Merge from develop (#2482)John Geebefore adoptionb51c0ea
lines 107–119why, one step back107108 test('when variadic has default array then specified value is used instead of default (not appended)', () => {109 const program = new commander.Command();110 let passedArg;111 program112 .addArgument(new commander.Argument('[value...]').default(['DEFAULT']))113 .action((value) => {114 passedArg = value;115 });116117 program.parse(['one', 'two'], { from: 'user' });118 assert.deepEqual(passedArg, ['one', 'two']);119 });Merge from develop into release/15.x (with minor conflicts and test migrations)John Geebefore adoption26e3ba3
lines 120–132why, one step back120121 test('when variadic has default array then specified value is used instead of default (not appended)', () => {122 const program = new commander.Command();123 let passedArg;124 program125 .addArgument(new commander.Argument('[value...]').default(['DEFAULT']))126 .action((value) => {127 passedArg = value;128 });129130 program.parse(['one', 'two'], { from: 'user' });131 assert.deepEqual(passedArg, ['one', 'two']);132 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 133why, one step back133});