Skip to content
Myr

command.help.test.js

This file

Path

tests/command.help.test.js

319 lines · 11.5 KiB · JavaScript

Last changed by

Use simple match in test (to avoid warning about expensive regex) (#2485)John Geebefore adoption987f289

History of this file

Take it

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

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

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

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 5–10why, one step back
5
6// Testing various help incantations.7//8// Note there are also specific help tests included in many of the feature tests,9// such as the alias, version, usage, name, helpOption, and commandHelp tests.10

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

lines 11–16why, one step back
11describe('help handling', () => {
12 // Avoid doing many full format tests as will be broken by any layout changes!13 test('when call helpInformation for program then help format is as expected (usage, options, commands)', () => {14 const program = new commander.Command();15 program.command('my-command <file>');16 const expectedHelpInformation = `Usage: test [options] [command]

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 17–25why, one step back
17
18Options:19 -h, --help display help for command2021Commands:22 my-command <file>23 help [command] display help for command24`;25

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

lines 26–29why, one step back
26 program.name('test');
27 const helpInformation = program.helpInformation();28 assert.equal(helpInformation, expectedHelpInformation);29 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 30why, one step back
30

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

lines 31–37why, one step back
31 test('when use .description for command then help includes description', () => {
32 const program = new commander.Command();33 program.command('simple-command').description('custom-description');34 program._help = 'test';35 const helpInformation = program.helpInformation();36 assert.match(helpInformation, /simple-command +custom-description/);37 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 38why, one step back
38

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

lines 39–47why, one step back
39 test('when call .help then exit', (t) => {
40 const program = createTestCommand();41 assert.throws(42 () => {43 program.help();44 },45 { message: '(outputHelp)' },46 );47 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 48why, one step back
48

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

lines 49–57why, one step back
49 test('when specify --help then exit', (t) => {
50 const program = createTestCommand();51 assert.throws(52 () => {53 program.parse(['node', 'test', '--help']);54 },55 { message: '(outputHelp)' },56 );57 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 58why, one step back
58

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

lines 59–75why, one step back
59 test('when call help(cb) then display cb output and exit', (t) => {
60 // Using spy to detect custom output61 const writeSpy = t.mock.method(process.stdout, 'write', () => {});62 const helpReplacement = 'reformatted help';63 const program = new commander.Command();64 program.exitOverride();65 assert.throws(66 () => {67 program.help((helpInformation) => {68 return helpReplacement;69 });70 },71 { message: '(outputHelp)' },72 );73 assert.equal(writeSpy.mock.callCount(), 1);74 assert.deepEqual(writeSpy.mock.calls[0].arguments, [helpReplacement]);75 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 76why, one step back
76

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

lines 77–86why, one step back
77 test('when call outputHelp(cb) then display cb output', (t) => {
78 // Using spy to detect custom output79 const writeSpy = t.mock.method(process.stdout, 'write', () => {});80 const helpReplacement = 'reformatted help';81 const program = new commander.Command();82 program.outputHelp((helpInformation) => {83 return helpReplacement;84 });85 assert.equal(writeSpy.mock.callCount(), 1);86 assert.deepEqual(writeSpy.mock.calls[0].arguments, [helpReplacement]);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 87–88why, one step back
87 });
88

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

lines 89–93why, one step back
89 test('when call deprecated outputHelp(cb) with wrong callback return type then throw', () => {
90 const program = new commander.Command();91 assert.throws(() => {92 program.outputHelp((helpInformation) => 3);93 });

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

line 94why, one step back
94 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 95why, one step back
95

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

lines 96–101why, one step back
96 test('when command sets deprecated noHelp then not displayed in helpInformation', () => {
97 const program = new commander.Command();98 program.command('secret', 'secret description', { noHelp: true });99 const helpInformation = program.helpInformation();100 assert(!helpInformation.includes('secret'));101 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 102why, one step back
102

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

lines 103–108why, one step back
103 test('when command sets hidden then not displayed in helpInformation', () => {
104 const program = new commander.Command();105 program.command('secret', 'secret description', { hidden: true });106 const helpInformation = program.helpInformation();107 assert(!helpInformation.includes('secret'));108 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 109why, one step back
109

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

lines 110–111why, one step back
110 test('when addCommand with hidden:true then not displayed in helpInformation', () => {
111 const secretCmd = new commander.Command('secret');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 112why, one step back
112

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

lines 113–117why, one step back
113 const program = new commander.Command();
114 program.addCommand(secretCmd, { hidden: true });115 const helpInformation = program.helpInformation();116 assert(!helpInformation.includes('secret'));117 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 118why, one step back
118

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

lines 119–122why, one step back
119 test('when help short flag masked then not displayed in helpInformation', () => {
120 const program = new commander.Command();121 program.option('-h, --host', 'select host');122 const helpInformation = program.helpInformation();

Use simple match in test (to avoid warning about expensive regex) (#2485)John Geebefore adoption987f289

lines 123–125why, one step back
123 assert(helpInformation.includes(' -h, --host'));
124 assert(!helpInformation.includes(' -h, --help'));125 assert(helpInformation.includes(' --help'));

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

line 126why, one step back
126 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 127why, one step back
127

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

lines 128–133why, one step back
128 test('when both help flags masked then not displayed in helpInformation', () => {
129 const program = new commander.Command();130 program.option('-h, --help', 'custom');131 const helpInformation = program.helpInformation();132 assert(!helpInformation.includes('display help'));133 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 134why, one step back
134

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

lines 135–149why, one step back
135 test('when call .help then output on stdout', (t) => {
136 const writeSpy = t.mock.method(process.stdout, 'write', () => {});137 const program = new commander.Command();138 program.exitOverride();139 assert.throws(140 () => {141 program.help();142 },143 { message: '(outputHelp)' },144 );145 assert.equal(writeSpy.mock.callCount(), 1);146 assert.deepEqual(writeSpy.mock.calls[0].arguments, [147 program.helpInformation(),148 ]);149 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 150why, one step back
150

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

lines 151–165why, one step back
151 test('when call .help with { error: true } then output on stderr', (t) => {
152 const writeSpy = t.mock.method(process.stderr, 'write', () => {});153 const program = new commander.Command();154 program.exitOverride();155 assert.throws(156 () => {157 program.help({ error: true });158 },159 { message: '(outputHelp)' },160 );161 assert.equal(writeSpy.mock.callCount(), 1);162 assert.deepEqual(writeSpy.mock.calls[0].arguments, [163 program.helpInformation(),164 ]);165 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 166why, one step back
166

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

lines 167–173why, one step back
167 test('when no options then Options not included in helpInformation', () => {
168 const program = new commander.Command();169 // No custom options, no version option, no help option170 program.helpOption(false);171 const helpInformation = program.helpInformation();172 assert(!helpInformation.includes('Options'));173 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 174why, one step back
174

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

lines 175–181why, one step back
175 test('when negated option then option included in helpInformation', () => {
176 const program = new commander.Command();177 program.option('-C, --no-colour', 'colourless');178 const helpInformation = program.helpInformation();179 assert(helpInformation.includes('--no-colour'));180 assert(helpInformation.includes('colourless'));181 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 182why, one step back
182

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

lines 183–190why, one step back
183 test('when option.hideHelp() then option not included in helpInformation', () => {
184 const program = new commander.Command();185 program.addOption(186 new commander.Option('-s,--secret', 'secret option').hideHelp(),187 );188 const helpInformation = program.helpInformation();189 assert(!helpInformation.includes('secret'));190 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 191why, one step back
191

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

lines 192–199why, one step back
192 test('when option.hideHelp(true) then option not included in helpInformation', () => {
193 const program = new commander.Command();194 program.addOption(195 new commander.Option('-s,--secret', 'secret option').hideHelp(true),196 );197 const helpInformation = program.helpInformation();198 assert(!helpInformation.includes('secret'));199 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 200why, one step back
200

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

lines 201–208why, one step back
201 test('when option.hideHelp(false) then option included in helpInformation', () => {
202 const program = new commander.Command();203 program.addOption(204 new commander.Option('-s,--secret', 'secret option').hideHelp(false),205 );206 const helpInformation = program.helpInformation();207 assert(helpInformation.includes('secret'));208 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 209why, one step back
209

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

lines 210–215why, one step back
210 test('when option has default value then default included in helpInformation', () => {
211 const program = new commander.Command();212 program.option('-p, --port <portNumber>', 'port number', 80);213 const helpInformation = program.helpInformation();214 assert(helpInformation.includes('(default: 80)'));215 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 216why, one step back
216

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

lines 217–227why, one step back
217 test('when option has default value description then default description included in helpInformation', () => {
218 const program = new commander.Command();219 program.addOption(220 new commander.Option('-a, --address <dotted>', 'ip address').default(221 '127.0.0.1',222 'home',223 ),224 );225 const helpInformation = program.helpInformation();226 assert(helpInformation.includes('(default: home)'));227 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 228why, one step back
228

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

lines 229–236why, one step back
229 test('when option has choices then choices included in helpInformation', () => {
230 const program = new commander.Command();231 program.addOption(232 new commander.Option('-c, --colour <colour>').choices(['red', 'blue']),233 );234 const helpInformation = program.helpInformation();235 assert(helpInformation.includes('(choices: "red", "blue")'));236 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 237why, one step back
237

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

lines 238–249why, one step back
238 test('when option has choices and default then both included in helpInformation', () => {
239 const program = new commander.Command();240 program.addOption(241 new commander.Option('-c, --colour <colour>')242 .choices(['red', 'blue'])243 .default('red'),244 );245 const helpInformation = program.helpInformation();246 assert(247 helpInformation.includes('(choices: "red", "blue", default: "red")'),248 );249 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 250why, one step back
250

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

lines 251–256why, one step back
251 test('when argument then included in helpInformation', () => {
252 const program = new commander.Command();253 program.name('foo').argument('<file>');254 const helpInformation = program.helpInformation();255 assert(helpInformation.includes('Usage: foo [options] <file>'));256 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 257why, one step back
257

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

lines 258–263why, one step back
258 test('when argument described then included in helpInformation', () => {
259 const program = new commander.Command();260 program.argument('<file>', 'input source').helpOption(false);261 const helpInformation = program.helpInformation();262 assert.match(helpInformation, /Arguments:\n +file +input source/);263 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 264why, one step back
264

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

lines 265–273why, one step back
265 test('when argument described with default then included in helpInformation', () => {
266 const program = new commander.Command();267 program.argument('[file]', 'input source', 'test.txt').helpOption(false);268 const helpInformation = program.helpInformation();269 assert.match(270 helpInformation,271 /Arguments:\n +file +input source \(default: "test.txt"\)/,272 );273 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 274why, one step back
274

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

lines 275–283why, one step back
275 test('when arguments described in deprecated way then included in helpInformation', () => {
276 const program = new commander.Command();277 program278 .arguments('<file>')279 .helpOption(false)280 .description('description', { file: 'input source' });281 const helpInformation = program.helpInformation();282 assert.match(helpInformation, /Arguments:\n +file +input source/);283 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 284why, one step back
284

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

lines 285–293why, one step back
285 test('when arguments described in deprecated way and empty description then arguments still included in helpInformation', () => {
286 const program = new commander.Command();287 program288 .arguments('<file>')289 .helpOption(false)290 .description('', { file: 'input source' });291 const helpInformation = program.helpInformation();292 assert.match(helpInformation, /Arguments:\n +file +input source/);293 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 294why, one step back
294

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

lines 295–305why, one step back
295 test('when argument has choices then choices included in helpInformation', () => {
296 const program = new commander.Command();297 program.addArgument(298 new commander.Argument('<colour>', 'preferred colour').choices([299 'red',300 'blue',301 ]),302 );303 const helpInformation = program.helpInformation();304 assert(helpInformation.includes('(choices: "red", "blue")'));305 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 306why, one step back
306

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

lines 307–318why, one step back
307 test('when argument has choices and default then both included in helpInformation', () => {
308 const program = new commander.Command();309 program.addArgument(310 new commander.Argument('<colour>', 'preferred colour')311 .choices(['red', 'blue'])312 .default('red'),313 );314 const helpInformation = program.helpInformation();315 assert(316 helpInformation.includes('(choices: "red", "blue", default: "red")'),317 );318 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 319why, one step back
319});