command.help.test.js
This file
Path
tests/command.help.test.js
317 lines · 11.4 KiB · JavaScript
Last changed by
Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
History of this fileTake it
The walk stopped after 200 commits: lines older than 3103c6b point at it.
Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 1why, one step back1const commander = require('../');Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 2why, one step back2const { createTestCommand } = require('./testHelpers');Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 3why, one step back3const { test, describe } = require('node:test');Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 4why, one step back4const assert = require('node:assert/strict');Add more tests for implies (#1730)John Geebefore adoption3103c6b
lines 5–10why, one step back56// 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.10Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 11–16why, one step back11describe('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 more tests for implies (#1730)John Geebefore adoption3103c6b
lines 17–25why, one step back1718Options:19 -h, --help display help for command2021Commands:22 my-command <file>23 help [command] display help for command24`;25Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 26–29why, one step back26 program.name('test');27 const helpInformation = program.helpInformation();28 assert.equal(helpInformation, expectedHelpInformation);29 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 30why, one step back30Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 31–37why, one step back31 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 more tests for implies (#1730)John Geebefore adoption3103c6b
line 38why, one step back38Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 39–47why, one step back39 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 more tests for implies (#1730)John Geebefore adoption3103c6b
line 48why, one step back48Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 49–57why, one step back49 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 more tests for implies (#1730)John Geebefore adoption3103c6b
line 58why, one step back58Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 59–75why, one step back59 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 more tests for implies (#1730)John Geebefore adoption3103c6b
line 76why, one step back76Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 77–86why, one step back77 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 more tests for implies (#1730)John Geebefore adoption3103c6b
lines 87–88why, one step back87 });88Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 89–93why, one step back89 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 back94 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 95why, one step back95Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 96–101why, one step back96 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 more tests for implies (#1730)John Geebefore adoption3103c6b
line 102why, one step back102Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 103–108why, one step back103 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 more tests for implies (#1730)John Geebefore adoption3103c6b
line 109why, one step back109Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 110–111why, one step back110 test('when addCommand with hidden:true then not displayed in helpInformation', () => {111 const secretCmd = new commander.Command('secret');Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 112why, one step back112Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 113–117why, one step back113 const program = new commander.Command();114 program.addCommand(secretCmd, { hidden: true });115 const helpInformation = program.helpInformation();116 assert(!helpInformation.includes('secret'));117 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 118why, one step back118Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 119–124why, one step back119 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();123 assert(!/\W-h\W.*display help/.test(helpInformation));124 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 125why, one step back125Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 126–131why, one step back126 test('when both help flags masked then not displayed in helpInformation', () => {127 const program = new commander.Command();128 program.option('-h, --help', 'custom');129 const helpInformation = program.helpInformation();130 assert(!helpInformation.includes('display help'));131 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 132why, one step back132Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 133–147why, one step back133 test('when call .help then output on stdout', (t) => {134 const writeSpy = t.mock.method(process.stdout, 'write', () => {});135 const program = new commander.Command();136 program.exitOverride();137 assert.throws(138 () => {139 program.help();140 },141 { message: '(outputHelp)' },142 );143 assert.equal(writeSpy.mock.callCount(), 1);144 assert.deepEqual(writeSpy.mock.calls[0].arguments, [145 program.helpInformation(),146 ]);147 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 148why, one step back148Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 149–163why, one step back149 test('when call .help with { error: true } then output on stderr', (t) => {150 const writeSpy = t.mock.method(process.stderr, 'write', () => {});151 const program = new commander.Command();152 program.exitOverride();153 assert.throws(154 () => {155 program.help({ error: true });156 },157 { message: '(outputHelp)' },158 );159 assert.equal(writeSpy.mock.callCount(), 1);160 assert.deepEqual(writeSpy.mock.calls[0].arguments, [161 program.helpInformation(),162 ]);163 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 164why, one step back164Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 165–171why, one step back165 test('when no options then Options not included in helpInformation', () => {166 const program = new commander.Command();167 // No custom options, no version option, no help option168 program.helpOption(false);169 const helpInformation = program.helpInformation();170 assert(!helpInformation.includes('Options'));171 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 172why, one step back172Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 173–179why, one step back173 test('when negated option then option included in helpInformation', () => {174 const program = new commander.Command();175 program.option('-C, --no-colour', 'colourless');176 const helpInformation = program.helpInformation();177 assert(helpInformation.includes('--no-colour'));178 assert(helpInformation.includes('colourless'));179 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 180why, one step back180Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 181–188why, one step back181 test('when option.hideHelp() then option not included in helpInformation', () => {182 const program = new commander.Command();183 program.addOption(184 new commander.Option('-s,--secret', 'secret option').hideHelp(),185 );186 const helpInformation = program.helpInformation();187 assert(!helpInformation.includes('secret'));188 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 189why, one step back189Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 190–197why, one step back190 test('when option.hideHelp(true) then option not included in helpInformation', () => {191 const program = new commander.Command();192 program.addOption(193 new commander.Option('-s,--secret', 'secret option').hideHelp(true),194 );195 const helpInformation = program.helpInformation();196 assert(!helpInformation.includes('secret'));197 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 198why, one step back198Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 199–206why, one step back199 test('when option.hideHelp(false) then option included in helpInformation', () => {200 const program = new commander.Command();201 program.addOption(202 new commander.Option('-s,--secret', 'secret option').hideHelp(false),203 );204 const helpInformation = program.helpInformation();205 assert(helpInformation.includes('secret'));206 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 207why, one step back207Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 208–213why, one step back208 test('when option has default value then default included in helpInformation', () => {209 const program = new commander.Command();210 program.option('-p, --port <portNumber>', 'port number', 80);211 const helpInformation = program.helpInformation();212 assert(helpInformation.includes('(default: 80)'));213 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 214why, one step back214Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 215–225why, one step back215 test('when option has default value description then default description included in helpInformation', () => {216 const program = new commander.Command();217 program.addOption(218 new commander.Option('-a, --address <dotted>', 'ip address').default(219 '127.0.0.1',220 'home',221 ),222 );223 const helpInformation = program.helpInformation();224 assert(helpInformation.includes('(default: home)'));225 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 226why, one step back226Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 227–234why, one step back227 test('when option has choices then choices included in helpInformation', () => {228 const program = new commander.Command();229 program.addOption(230 new commander.Option('-c, --colour <colour>').choices(['red', 'blue']),231 );232 const helpInformation = program.helpInformation();233 assert(helpInformation.includes('(choices: "red", "blue")'));234 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 235why, one step back235Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 236–247why, one step back236 test('when option has choices and default then both included in helpInformation', () => {237 const program = new commander.Command();238 program.addOption(239 new commander.Option('-c, --colour <colour>')240 .choices(['red', 'blue'])241 .default('red'),242 );243 const helpInformation = program.helpInformation();244 assert(245 helpInformation.includes('(choices: "red", "blue", default: "red")'),246 );247 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 248why, one step back248Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 249–254why, one step back249 test('when argument then included in helpInformation', () => {250 const program = new commander.Command();251 program.name('foo').argument('<file>');252 const helpInformation = program.helpInformation();253 assert(helpInformation.includes('Usage: foo [options] <file>'));254 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 255why, one step back255Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 256–261why, one step back256 test('when argument described then included in helpInformation', () => {257 const program = new commander.Command();258 program.argument('<file>', 'input source').helpOption(false);259 const helpInformation = program.helpInformation();260 assert.match(helpInformation, /Arguments:\n +file +input source/);261 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 262why, one step back262Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 263–271why, one step back263 test('when argument described with default then included in helpInformation', () => {264 const program = new commander.Command();265 program.argument('[file]', 'input source', 'test.txt').helpOption(false);266 const helpInformation = program.helpInformation();267 assert.match(268 helpInformation,269 /Arguments:\n +file +input source \(default: "test.txt"\)/,270 );271 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 272why, one step back272Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 273–281why, one step back273 test('when arguments described in deprecated way then included in helpInformation', () => {274 const program = new commander.Command();275 program276 .arguments('<file>')277 .helpOption(false)278 .description('description', { file: 'input source' });279 const helpInformation = program.helpInformation();280 assert.match(helpInformation, /Arguments:\n +file +input source/);281 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 282why, one step back282Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 283–291why, one step back283 test('when arguments described in deprecated way and empty description then arguments still included in helpInformation', () => {284 const program = new commander.Command();285 program286 .arguments('<file>')287 .helpOption(false)288 .description('', { file: 'input source' });289 const helpInformation = program.helpInformation();290 assert.match(helpInformation, /Arguments:\n +file +input source/);291 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 292why, one step back292Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 293–303why, one step back293 test('when argument has choices then choices included in helpInformation', () => {294 const program = new commander.Command();295 program.addArgument(296 new commander.Argument('<colour>', 'preferred colour').choices([297 'red',298 'blue',299 ]),300 );301 const helpInformation = program.helpInformation();302 assert(helpInformation.includes('(choices: "red", "blue")'));303 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 304why, one step back304Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 305–316why, one step back305 test('when argument has choices and default then both included in helpInformation', () => {306 const program = new commander.Command();307 program.addArgument(308 new commander.Argument('<colour>', 'preferred colour')309 .choices(['red', 'blue'])310 .default('red'),311 );312 const helpInformation = program.helpInformation();313 assert(314 helpInformation.includes('(choices: "red", "blue", default: "red")'),315 );316 });Add more tests for implies (#1730)John Geebefore adoption3103c6b
line 317why, one step back317});