command.help.test.js
This file
Path
tests/command.help.test.js
308 lines · 10.9 KiB · JavaScript
Last changed by
Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
History of this fileTake it
The walk stopped after 200 commits: lines older than 78e335f point at it.
Fix minor typo (#1579)John Geebefore adoption78e335f
lines 1–10why, one step back1const 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();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 11–12why, one step back11 program.command('my-command <file>');12 const expectedHelpInformation = `Usage: test [options] [command]Fix minor typo (#1579)John Geebefore adoption78e335f
lines 13–28why, one step back1314Options:15 -h, --help display help for command1617Commands:18 my-command <file>19 help [command] display help for command20`;2122 program.name('test');23 const helpInformation = program.helpInformation();24 expect(helpInformation).toBe(expectedHelpInformation);25});2627test('when use .description for command then help includes description', () => {28 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 29why, one step back29 program.command('simple-command').description('custom-description');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 30–36why, one step back30 program._help = 'test';31 const helpInformation = program.helpInformation();32 expect(helpInformation).toMatch(/simple-command +custom-description/);33});3435test('when call .help then exit', () => {36 // Optional. Suppress normal output to keep test output clean.Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 37–39why, one step back37 const writeSpy = jest38 .spyOn(process.stdout, 'write')39 .mockImplementation(() => {});Fix minor typo (#1579)John Geebefore adoption78e335f
line 40why, one step back40 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 41why, one step back41 program.exitOverride();Fix minor typo (#1579)John Geebefore adoption78e335f
lines 42–49why, one step back42 expect(() => {43 program.help();44 }).toThrow('(outputHelp)');45 writeSpy.mockClear();46});4748test('when specify --help then exit', () => {49 // Optional. Suppress normal output to keep test output clean.Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 50–52why, one step back50 const writeSpy = jest51 .spyOn(process.stdout, 'write')52 .mockImplementation(() => {});Fix minor typo (#1579)John Geebefore adoption78e335f
line 53why, one step back53 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 54why, one step back54 program.exitOverride();Fix minor typo (#1579)John Geebefore adoption78e335f
lines 55–62why, one step back55 expect(() => {56 program.parse(['node', 'test', '--help']);57 }).toThrow('(outputHelp)');58 writeSpy.mockClear();59});6061test('when call help(cb) then display cb output and exit', () => {62 // Using spy to detect custom outputReformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 63–65why, one step back63 const writeSpy = jest64 .spyOn(process.stdout, 'write')65 .mockImplementation(() => {});Fix minor typo (#1579)John Geebefore adoption78e335f
lines 66–67why, one step back66 const helpReplacement = 'reformatted help';67 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 68why, one step back68 program.exitOverride();Fix minor typo (#1579)John Geebefore adoption78e335f
lines 69–79why, one step back69 expect(() => {70 program.help((helpInformation) => {71 return helpReplacement;72 });73 }).toThrow('(outputHelp)');74 expect(writeSpy).toHaveBeenCalledWith(helpReplacement);75 writeSpy.mockClear();76});7778test('when call outputHelp(cb) then display cb output', () => {79 // Using spy to detect custom outputReformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 80–82why, one step back80 const writeSpy = jest81 .spyOn(process.stdout, 'write')82 .mockImplementation(() => {});Fix minor typo (#1579)John Geebefore adoption78e335f
lines 83–100why, one step back83 const helpReplacement = 'reformatted help';84 const program = new commander.Command();85 program.outputHelp((helpInformation) => {86 return helpReplacement;87 });88 expect(writeSpy).toHaveBeenCalledWith(helpReplacement);89 writeSpy.mockClear();90});9192test('when call deprecated outputHelp(cb) with wrong callback return type then throw', () => {93 const program = new commander.Command();94 expect(() => {95 program.outputHelp((helpInformation) => 3);96 }).toThrow();97});9899test('when command sets deprecated noHelp then not displayed in helpInformation', () => {100 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 101why, one step back101 program.command('secret', 'secret description', { noHelp: true });Fix minor typo (#1579)John Geebefore adoption78e335f
lines 102–107why, one step back102 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();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 108why, one step back108 program.command('secret', 'secret description', { hidden: true });Fix minor typo (#1579)John Geebefore adoption78e335f
lines 109–116why, one step back109 const helpInformation = program.helpInformation();110 expect(helpInformation).not.toMatch('secret');111});112113test('when addCommand with hidden:true then not displayed in helpInformation', () => {114 const secretCmd = new commander.Command('secret');115116 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 117why, one step back117 program.addCommand(secretCmd, { hidden: true });Fix minor typo (#1579)John Geebefore adoption78e335f
lines 118–123why, one step back118 const helpInformation = program.helpInformation();119 expect(helpInformation).not.toMatch('secret');120});121122test('when help short flag masked then not displayed in helpInformation', () => {123 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 124why, one step back124 program.option('-h, --host', 'select host');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 125–130why, one step back125 const helpInformation = program.helpInformation();126 expect(helpInformation).not.toMatch(/\W-h\W.*display help/);127});128129test('when both help flags masked then not displayed in helpInformation', () => {130 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 131why, one step back131 program.option('-h, --help', 'custom');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 132–136why, one step back132 const helpInformation = program.helpInformation();133 expect(helpInformation).not.toMatch('display help');134});135136test('when call .help then output on stdout', () => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 137–139why, one step back137 const writeSpy = jest138 .spyOn(process.stdout, 'write')139 .mockImplementation(() => {});Fix minor typo (#1579)John Geebefore adoption78e335f
line 140why, one step back140 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 141why, one step back141 program.exitOverride();Fix minor typo (#1579)John Geebefore adoption78e335f
lines 142–149why, one step back142 expect(() => {143 program.help();144 }).toThrow('(outputHelp)');145 expect(writeSpy).toHaveBeenCalledWith(program.helpInformation());146 writeSpy.mockClear();147});148149test('when call .help with { error: true } then output on stderr', () => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 150–152why, one step back150 const writeSpy = jest151 .spyOn(process.stderr, 'write')152 .mockImplementation(() => {});Fix minor typo (#1579)John Geebefore adoption78e335f
line 153why, one step back153 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 154why, one step back154 program.exitOverride();Fix minor typo (#1579)John Geebefore adoption78e335f
lines 155–164why, one step back155 expect(() => {156 program.help({ error: true });157 }).toThrow('(outputHelp)');158 expect(writeSpy).toHaveBeenCalledWith(program.helpInformation());159 writeSpy.mockClear();160});161162test('when no options then Options not included in helpInformation', () => {163 const program = new commander.Command();164 // No custom options, no version option, no help optionReformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 165why, one step back165 program.helpOption(false);Fix minor typo (#1579)John Geebefore adoption78e335f
lines 166–171why, one step back166 const helpInformation = program.helpInformation();167 expect(helpInformation).not.toMatch('Options');168});169170test('when negated option then option included in helpInformation', () => {171 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 172why, one step back172 program.option('-C, --no-colour', 'colourless');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 173–179why, one step back173 const helpInformation = program.helpInformation();174 expect(helpInformation).toMatch('--no-colour');175 expect(helpInformation).toMatch('colourless');176});177178test('when option.hideHelp() then option not included in helpInformation', () => {179 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 180–182why, one step back180 program.addOption(181 new commander.Option('-s,--secret', 'secret option').hideHelp(),182 );Fix minor typo (#1579)John Geebefore adoption78e335f
lines 183–188why, one step back183 const helpInformation = program.helpInformation();184 expect(helpInformation).not.toMatch('secret');185});186187test('when option.hideHelp(true) then option not included in helpInformation', () => {188 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 189–191why, one step back189 program.addOption(190 new commander.Option('-s,--secret', 'secret option').hideHelp(true),191 );Fix minor typo (#1579)John Geebefore adoption78e335f
lines 192–197why, one step back192 const helpInformation = program.helpInformation();193 expect(helpInformation).not.toMatch('secret');194});195196test('when option.hideHelp(false) then option included in helpInformation', () => {197 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 198–200why, one step back198 program.addOption(199 new commander.Option('-s,--secret', 'secret option').hideHelp(false),200 );Fix minor typo (#1579)John Geebefore adoption78e335f
lines 201–206why, one step back201 const helpInformation = program.helpInformation();202 expect(helpInformation).toMatch('secret');203});204205test('when option has default value then default included in helpInformation', () => {206 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 207why, one step back207 program.option('-p, --port <portNumber>', 'port number', 80);Fix minor typo (#1579)John Geebefore adoption78e335f
lines 208–213why, one step back208 const helpInformation = program.helpInformation();209 expect(helpInformation).toMatch('(default: 80)');210});211212test('when option has default value description then default description included in helpInformation', () => {213 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 214–219why, one step back214 program.addOption(215 new commander.Option('-a, --address <dotted>', 'ip address').default(216 '127.0.0.1',217 'home',218 ),219 );Fix minor typo (#1579)John Geebefore adoption78e335f
lines 220–225why, one step back220 const helpInformation = program.helpInformation();221 expect(helpInformation).toMatch('(default: home)');222});223224test('when option has choices then choices included in helpInformation', () => {225 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 226–228why, one step back226 program.addOption(227 new commander.Option('-c, --colour <colour>').choices(['red', 'blue']),228 );Fix minor typo (#1579)John Geebefore adoption78e335f
lines 229–234why, one step back229 const helpInformation = program.helpInformation();230 expect(helpInformation).toMatch('(choices: "red", "blue")');231});232233test('when option has choices and default then both included in helpInformation', () => {234 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 235–239why, one step back235 program.addOption(236 new commander.Option('-c, --colour <colour>')237 .choices(['red', 'blue'])238 .default('red'),239 );Fix minor typo (#1579)John Geebefore adoption78e335f
lines 240–245why, one step back240 const helpInformation = program.helpInformation();241 expect(helpInformation).toMatch('(choices: "red", "blue", default: "red")');242});243244test('when argument then included in helpInformation', () => {245 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 246why, one step back246 program.name('foo').argument('<file>');Fix minor typo (#1579)John Geebefore adoption78e335f
lines 247–252why, one step back247 const helpInformation = program.helpInformation();248 expect(helpInformation).toMatch('Usage: foo [options] <file>');249});250251test('when argument described then included in helpInformation', () => {252 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 253why, one step back253 program.argument('<file>', 'input source').helpOption(false);Fix minor typo (#1579)John Geebefore adoption78e335f
lines 254–259why, one step back254 const helpInformation = program.helpInformation();255 expect(helpInformation).toMatch(/Arguments:\n +file +input source/);256});257258test('when argument described with default then included in helpInformation', () => {259 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 260why, one step back260 program.argument('[file]', 'input source', 'test.txt').helpOption(false);Fix minor typo (#1579)John Geebefore adoption78e335f
line 261why, one step back261 const helpInformation = program.helpInformation();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 262–264why, one step back262 expect(helpInformation).toMatch(263 /Arguments:\n +file +input source \(default: "test.txt"\)/,264 );Fix minor typo (#1579)John Geebefore adoption78e335f
lines 265–288why, one step back265});266267test('when arguments described in deprecated way then included in helpInformation', () => {268 const program = new commander.Command();269 program270 .arguments('<file>')271 .helpOption(false)272 .description('description', { file: 'input source' });273 const helpInformation = program.helpInformation();274 expect(helpInformation).toMatch(/Arguments:\n +file +input source/);275});276277test('when arguments described in deprecated way and empty description then arguments still included in helpInformation', () => {278 const program = new commander.Command();279 program280 .arguments('<file>')281 .helpOption(false)282 .description('', { file: 'input source' });283 const helpInformation = program.helpInformation();284 expect(helpInformation).toMatch(/Arguments:\n +file +input source/);285});286287test('when argument has choices then choices included in helpInformation', () => {288 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 289–294why, one step back289 program.addArgument(290 new commander.Argument('<colour>', 'preferred colour').choices([291 'red',292 'blue',293 ]),294 );Fix minor typo (#1579)John Geebefore adoption78e335f
lines 295–300why, one step back295 const helpInformation = program.helpInformation();296 expect(helpInformation).toMatch('(choices: "red", "blue")');297});298299test('when argument has choices and default then both included in helpInformation', () => {300 const program = new commander.Command();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 301–305why, one step back301 program.addArgument(302 new commander.Argument('<colour>', 'preferred colour')303 .choices(['red', 'blue'])304 .default('red'),305 );Fix minor typo (#1579)John Geebefore adoption78e335f
lines 306–308why, one step back306 const helpInformation = program.helpInformation();307 expect(helpInformation).toMatch('(choices: "red", "blue", default: "red")');308});