Skip to content
Myr

command.help.test.js

This file

Path

tests/command.help.test.js

299 lines · 10.8 KiB · JavaScript

Last changed by

Choices for arguments (#1525)John Geebefore adoption708527e

History of this file

Take it

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

Update dependencies (#1214)John Geebefore adoption2aad525

lines 1–28why, one step back
1const commander = require('../');
23// Testing various help incantations.4//5// Note there are also specific help tests included in many of the feature tests,6// such as the alias, version, usage, name, helpOption, and commandHelp tests.78// Avoid doing many full format tests as will be broken by any layout changes!9test('when call helpInformation for program then help format is as expected (usage, options, commands)', () => {10 const program = new commander.Command();11 program12 .command('my-command <file>');13 const expectedHelpInformation =14`Usage: test [options] [command]1516Options:17 -h, --help display help for command1819Commands:20 my-command <file>21 help [command] display help for command22`;2324 program.name('test');25 const helpInformation = program.helpInformation();26 expect(helpInformation).toBe(expectedHelpInformation);27});28

Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b

line 29why, one step back
29test('when use .description for command then help includes description', () => {

Update dependencies (#1214)John Geebefore adoption2aad525

lines 30–90why, one step back
30 const program = new commander.Command();
31 program32 .command('simple-command')33 .description('custom-description');34 program._help = 'test';35 const helpInformation = program.helpInformation();36 expect(helpInformation).toMatch(/simple-command +custom-description/);37});3839test('when call .help then exit', () => {40 // Optional. Suppress normal output to keep test output clean.41 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });42 const program = new commander.Command();43 program44 .exitOverride();45 expect(() => {46 program.help();47 }).toThrow('(outputHelp)');48 writeSpy.mockClear();49});5051test('when specify --help then exit', () => {52 // Optional. Suppress normal output to keep test output clean.53 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });54 const program = new commander.Command();55 program56 .exitOverride();57 expect(() => {58 program.parse(['node', 'test', '--help']);59 }).toThrow('(outputHelp)');60 writeSpy.mockClear();61});6263test('when call help(cb) then display cb output and exit', () => {64 // Using spy to detect custom output65 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });66 const helpReplacement = 'reformatted help';67 const program = new commander.Command();68 program69 .exitOverride();70 expect(() => {71 program.help((helpInformation) => {72 return helpReplacement;73 });74 }).toThrow('(outputHelp)');75 expect(writeSpy).toHaveBeenCalledWith(helpReplacement);76 writeSpy.mockClear();77});7879test('when call outputHelp(cb) then display cb output', () => {80 // Using spy to detect custom output81 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });82 const helpReplacement = 'reformatted help';83 const program = new commander.Command();84 program.outputHelp((helpInformation) => {85 return helpReplacement;86 });87 expect(writeSpy).toHaveBeenCalledWith(helpReplacement);88 writeSpy.mockClear();89});90

Increase test coverage, including incrementNodeInspectorPort (#1428)John Geebefore adoptiond8faba2

lines 91–97why, one step back
91test('when call deprecated outputHelp(cb) with wrong callback return type then throw', () => {
92 const program = new commander.Command();93 expect(() => {94 program.outputHelp((helpInformation) => 3);95 }).toThrow();96});97

Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc

line 98why, one step back
98test('when command sets deprecated noHelp then not displayed in helpInformation', () => {

Update dependencies (#1214)John Geebefore adoption2aad525

lines 99–101why, one step back
99 const program = new commander.Command();
100 program101 .command('secret', 'secret description', { noHelp: true });

Add opts to addCommand, and rename noHelp to hidden (#1232)John Geebefore adoptionb5d95ee

lines 102–119why, one step back
102 const helpInformation = program.helpInformation();
103 expect(helpInformation).not.toMatch('secret');104});105106test('when command sets hidden then not displayed in helpInformation', () => {107 const program = new commander.Command();108 program109 .command('secret', 'secret description', { hidden: true });110 const helpInformation = program.helpInformation();111 expect(helpInformation).not.toMatch('secret');112});113114test('when addCommand with hidden:true then not displayed in helpInformation', () => {115 const secretCmd = new commander.Command('secret');116117 const program = new commander.Command();118 program119 .addCommand(secretCmd, { hidden: true });

Update dependencies (#1214)John Geebefore adoption2aad525

lines 120–122why, one step back
120 const helpInformation = program.helpInformation();
121 expect(helpInformation).not.toMatch('secret');122});

Omit the help flags from help that are masked by other options (#1247)John Geebefore adoptione1966fc

lines 123–138why, one step back
123
124test('when help short flag masked then not displayed in helpInformation', () => {125 const program = new commander.Command();126 program127 .option('-h, --host', 'select host');128 const helpInformation = program.helpInformation();129 expect(helpInformation).not.toMatch(/\W-h\W.*display help/);130});131132test('when both help flags masked then not displayed in helpInformation', () => {133 const program = new commander.Command();134 program135 .option('-h, --help', 'custom');136 const helpInformation = program.helpInformation();137 expect(helpInformation).not.toMatch('display help');138});

Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c

line 139why, one step back
139

Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d

lines 140–163why, one step back
140test('when call .help then output on stdout', () => {
141 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });142 const program = new commander.Command();143 program144 .exitOverride();145 expect(() => {146 program.help();147 }).toThrow('(outputHelp)');148 expect(writeSpy).toHaveBeenCalledWith(program.helpInformation());149 writeSpy.mockClear();150});151152test('when call .help with { error: true } then output on stderr', () => {153 const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });154 const program = new commander.Command();155 program156 .exitOverride();157 expect(() => {158 program.help({ error: true });159 }).toThrow('(outputHelp)');160 expect(writeSpy).toHaveBeenCalledWith(program.helpInformation());161 writeSpy.mockClear();162});163

Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc

line 164why, one step back
164test('when no options then Options not included in helpInformation', () => {

Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c

lines 165–170why, one step back
165 const program = new commander.Command();
166 // No custom options, no version option, no help option167 program168 .helpOption(false);169 const helpInformation = program.helpInformation();170 expect(helpInformation).not.toMatch('Options');

Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc

lines 171–172why, one step back
171});
172

Increase test coverage (#1381)John Geebefore adoption7a0baab

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

Include tests for supplying optional param to .hideHelp (#1372)John Geebefore adoptionb0c5884

line 182why, one step back
182test('when option.hideHelp() then option not included in helpInformation', () => {

Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc

lines 183–187why, one step back
183 const program = new commander.Command();
184 program185 .addOption(new commander.Option('-s,--secret', 'secret option').hideHelp());186 const helpInformation = program.helpInformation();187 expect(helpInformation).not.toMatch('secret');

Include tests for supplying optional param to .hideHelp (#1372)John Geebefore adoptionb0c5884

lines 188–203why, one step back
188});
189190test('when option.hideHelp(true) then option not included in helpInformation', () => {191 const program = new commander.Command();192 program193 .addOption(new commander.Option('-s,--secret', 'secret option').hideHelp(true));194 const helpInformation = program.helpInformation();195 expect(helpInformation).not.toMatch('secret');196});197198test('when option.hideHelp(false) then option included in helpInformation', () => {199 const program = new commander.Command();200 program201 .addOption(new commander.Option('-s,--secret', 'secret option').hideHelp(false));202 const helpInformation = program.helpInformation();203 expect(helpInformation).toMatch('secret');

Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc

lines 204–235why, one step back
204});
205206test('when option has default value then default included in helpInformation', () => {207 const program = new commander.Command();208 program209 .option('-p, --port <portNumber>', 'port number', 80);210 const helpInformation = program.helpInformation();211 expect(helpInformation).toMatch('(default: 80)');212});213214test('when option has default value description then default description included in helpInformation', () => {215 const program = new commander.Command();216 program217 .addOption(new commander.Option('-a, --address <dotted>', 'ip address').default('127.0.0.1', 'home'));218 const helpInformation = program.helpInformation();219 expect(helpInformation).toMatch('(default: home)');220});221222test('when option has choices then choices included in helpInformation', () => {223 const program = new commander.Command();224 program225 .addOption(new commander.Option('-c, --colour <colour>').choices(['red', 'blue']));226 const helpInformation = program.helpInformation();227 expect(helpInformation).toMatch('(choices: "red", "blue")');228});229230test('when option has choices and default then both included in helpInformation', () => {231 const program = new commander.Command();232 program233 .addOption(new commander.Option('-c, --colour <colour>').choices(['red', 'blue']).default('red'));234 const helpInformation = program.helpInformation();235 expect(helpInformation).toMatch('(choices: "red", "blue", default: "red")');

Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c

line 236why, one step back
236});

Merge remote-tracking branch 'origin/develop' into release/7.xJohn Geebefore adoption091224d

line 237why, one step back
237

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 238why, one step back
238test('when argument then included in helpInformation', () => {

Merge remote-tracking branch 'origin/develop' into release/7.xJohn Geebefore adoption091224d

lines 239–241why, one step back
239 const program = new commander.Command();
240 program241 .name('foo')

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 242why, one step back
242 .argument('<file>');

Merge remote-tracking branch 'origin/develop' into release/7.xJohn Geebefore adoption091224d

lines 243–246why, one step back
243 const helpInformation = program.helpInformation();
244 expect(helpInformation).toMatch('Usage: foo [options] <file>');245});246

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 247why, one step back
247test('when argument described then included in helpInformation', () => {

Merge remote-tracking branch 'origin/develop' into release/7.xJohn Geebefore adoption091224d

lines 248–249why, one step back
248 const program = new commander.Command();
249 program

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

lines 250–255why, one step back
250 .argument('<file>', 'input source')
251 .helpOption(false);252 const helpInformation = program.helpInformation();253 expect(helpInformation).toMatch(/Arguments:\n +file +input source/);254});255

Extend .argument() to add default value and custom parse for command-argument (#1508)John Geebefore adoptionf99d6fa

lines 256–264why, one step back
256test('when argument described with default then included in helpInformation', () => {
257 const program = new commander.Command();258 program259 .argument('[file]', 'input source', 'test.txt')260 .helpOption(false);261 const helpInformation = program.helpInformation();262 expect(helpInformation).toMatch(/Arguments:\n +file +input source \(default: "test.txt"\)/);263});264

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

lines 265–267why, one step back
265test('when arguments described in deprecated way then included in helpInformation', () => {
266 const program = new commander.Command();267 program

Merge remote-tracking branch 'origin/develop' into release/7.xJohn Geebefore adoption091224d

lines 268–271why, one step back
268 .arguments('<file>')
269 .helpOption(false)270 .description('description', { file: 'input source' });271 const helpInformation = program.helpInformation();

Test for help for arguments without description (#1459)John Geebefore adoptiona0c1a77

lines 272–274why, one step back
272 expect(helpInformation).toMatch(/Arguments:\n +file +input source/);
273});274

More consistent way of adding command arguments (#1490)John Geebefore adoptionb286da9

line 275why, one step back
275test('when arguments described in deprecated way and empty description then arguments still included in helpInformation', () => {

Test for help for arguments without description (#1459)John Geebefore adoptiona0c1a77

lines 276–282why, one step back
276 const program = new commander.Command();
277 program278 .arguments('<file>')279 .helpOption(false)280 .description('', { file: 'input source' });281 const helpInformation = program.helpInformation();282 expect(helpInformation).toMatch(/Arguments:\n +file +input source/);

Merge remote-tracking branch 'origin/develop' into release/7.xJohn Geebefore adoption091224d

line 283why, one step back
283});

Choices for arguments (#1525)John Geebefore adoption708527e

lines 284–299why, one step back
284
285test('when argument has choices then choices included in helpInformation', () => {286 const program = new commander.Command();287 program288 .addArgument(new commander.Argument('<colour>', 'preferred colour').choices(['red', 'blue']));289 const helpInformation = program.helpInformation();290 expect(helpInformation).toMatch('(choices: "red", "blue")');291});292293test('when argument has choices and default then both included in helpInformation', () => {294 const program = new commander.Command();295 program296 .addArgument(new commander.Argument('<colour>', 'preferred colour').choices(['red', 'blue']).default('red'));297 const helpInformation = program.helpInformation();298 expect(helpInformation).toMatch('(choices: "red", "blue", default: "red")');299});