command.help.test.js
This file
Path
tests/command.help.test.js
223 lines · 7.8 KiB · JavaScript
Last changed by
Merge branch 'develop' into release/7.xJohn Geebefore adoptione5512a4
History of this fileTake it
Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 1–16why, 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();11 program12 .command('my-command <file>');13 const expectedHelpInformation =14`Usage: test [options] [command]1516Options:Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 17why, one step back17 -h, --help display help for commandMove to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 18–20why, one step back1819Commands:20 my-command <file>Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 21why, one step back21 help [command] display help for commandMove to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 22–23why, one step back22`;23Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 24why, one step back24 program.name('test');Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 25–28why, one step back25 const helpInformation = program.helpInformation();26 expect(helpInformation).toBe(expectedHelpInformation);27});28Merge pull request #1305 from tj/release/6.xTomoaki Abebefore adoptionc5a5e7b
line 29why, one step back29test('when use .description for command then help includes description', () => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 30–33why, one step back30 const program = new commander.Command();31 program32 .command('simple-command')33 .description('custom-description');Parse rework for nested commands (#1149)John Geebefore adoption1345f98
line 34why, one step back34 program._help = 'test';Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 35–43why, one step back35 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 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 44why, one step back44 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 45–55why, one step back45 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 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 56why, one step back56 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 57–68why, one step back57 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 programAdd override to allow throw instead of process.exit (#1040)John Geebefore adoption8df692e
line 69why, one step back69 .exitOverride();Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 70–90why, one step back70 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});90Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
line 91why, one step back91test('when command sets deprecated noHelp then not displayed in helpInformation', () => {Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 92–94why, one step back92 const program = new commander.Command();93 program94 .command('secret', 'secret description', { noHelp: true });Add opts to addCommand, and rename noHelp to hidden (#1232)John Geebefore adoptionb5d95ee
lines 95–112why, one step back95 const helpInformation = program.helpInformation();96 expect(helpInformation).not.toMatch('secret');97});9899test('when command sets hidden then not displayed in helpInformation', () => {100 const program = new commander.Command();101 program102 .command('secret', 'secret description', { hidden: true });103 const helpInformation = program.helpInformation();104 expect(helpInformation).not.toMatch('secret');105});106107test('when addCommand with hidden:true then not displayed in helpInformation', () => {108 const secretCmd = new commander.Command('secret');109110 const program = new commander.Command();111 program112 .addCommand(secretCmd, { hidden: true });Move to Jest as testing framework (#1035)John Geebefore adoptiond7f9cd4
lines 113–115why, one step back113 const helpInformation = program.helpInformation();114 expect(helpInformation).not.toMatch('secret');115});Omit the help flags from help that are masked by other options (#1247)John Geebefore adoptione1966fc
lines 116–131why, one step back116117test('when help short flag masked then not displayed in helpInformation', () => {118 const program = new commander.Command();119 program120 .option('-h, --host', 'select host');121 const helpInformation = program.helpInformation();122 expect(helpInformation).not.toMatch(/\W-h\W.*display help/);123});124125test('when both help flags masked then not displayed in helpInformation', () => {126 const program = new commander.Command();127 program128 .option('-h, --help', 'custom');129 const helpInformation = program.helpInformation();130 expect(helpInformation).not.toMatch('display help');131});Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c
line 132why, one step back132Expand help customisation with .addHelpText (#1296)John Geebefore adoptiond26a26d
lines 133–156why, one step back133test('when call .help then output on stdout', () => {134 const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });135 const program = new commander.Command();136 program137 .exitOverride();138 expect(() => {139 program.help();140 }).toThrow('(outputHelp)');141 expect(writeSpy).toHaveBeenCalledWith(program.helpInformation());142 writeSpy.mockClear();143});144145test('when call .help with { error: true } then output on stderr', () => {146 const writeSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });147 const program = new commander.Command();148 program149 .exitOverride();150 expect(() => {151 program.help({ error: true });152 }).toThrow('(outputHelp)');153 expect(writeSpy).toHaveBeenCalledWith(program.helpInformation());154 writeSpy.mockClear();155});156Enhance Option class to allow hiding help, specifying choices, and change how default value displayed in help (#1331)John Geebefore adoptiona05a8bc
line 157why, one step back157test('when no options then Options not included in helpInformation', () => {Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c
lines 158–163why, one step back158 const program = new commander.Command();159 // No custom options, no version option, no help option160 program161 .helpOption(false);162 const helpInformation = program.helpInformation();163 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 164–203why, one step back164});165166test('when option hidden then option not included in helpInformation', () => {167 const program = new commander.Command();168 program169 .addOption(new commander.Option('-s,--secret', 'secret option').hideHelp());170 const helpInformation = program.helpInformation();171 expect(helpInformation).not.toMatch('secret');172});173174test('when option has default value then default included in helpInformation', () => {175 const program = new commander.Command();176 program177 .option('-p, --port <portNumber>', 'port number', 80);178 const helpInformation = program.helpInformation();179 expect(helpInformation).toMatch('(default: 80)');180});181182test('when option has default value description then default description included in helpInformation', () => {183 const program = new commander.Command();184 program185 .addOption(new commander.Option('-a, --address <dotted>', 'ip address').default('127.0.0.1', 'home'));186 const helpInformation = program.helpInformation();187 expect(helpInformation).toMatch('(default: home)');188});189190test('when option has choices then choices included in helpInformation', () => {191 const program = new commander.Command();192 program193 .addOption(new commander.Option('-c, --colour <colour>').choices(['red', 'blue']));194 const helpInformation = program.helpInformation();195 expect(helpInformation).toMatch('(choices: "red", "blue")');196});197198test('when option has choices and default then both included in helpInformation', () => {199 const program = new commander.Command();200 program201 .addOption(new commander.Option('-c, --colour <colour>').choices(['red', 'blue']).default('red'));202 const helpInformation = program.helpInformation();203 expect(helpInformation).toMatch('(choices: "red", "blue", default: "red")');Allow disabling the built-in help option (#1325)John Geebefore adoption6b2e42c
line 204why, one step back204});Merge remote-tracking branch 'origin/develop' into release/7.xJohn Geebefore adoption091224d
lines 205–221why, one step back205206test('when arguments then included in helpInformation', () => {207 const program = new commander.Command();208 program209 .name('foo')210 .arguments('<file>');211 const helpInformation = program.helpInformation();212 expect(helpInformation).toMatch('Usage: foo [options] <file>');213});214215test('when arguments described then included in helpInformation', () => {216 const program = new commander.Command();217 program218 .arguments('<file>')219 .helpOption(false)220 .description('description', { file: 'input source' });221 const helpInformation = program.helpInformation();Merge branch 'develop' into release/7.xJohn Geebefore adoptione5512a4
line 222why, one step back222 expect(helpInformation).toMatch(/Arguments:\n +file +input source/); // [sic], extra lineMerge remote-tracking branch 'origin/develop' into release/7.xJohn Geebefore adoption091224d
line 223why, one step back223});