Skip to content
Myr

command.helpCommand.test.js

This file

Path

tests/command.helpCommand.test.js

163 lines · 5.5 KiB · JavaScript

Last changed by

Have help command call help directly for subcommands, when possible (#1864)John Geebefore adoption63abdac

History of this file

Take it

The walk stopped after 200 commits: lines older than 0b0fbbe point at it.

Update dependencies (#1524)John Geebefore adoption0b0fbbe

lines 1–125why, one step back
1const commander = require('../');
23describe('help command listed in helpInformation', () => {4 test('when program has no subcommands then no automatic help command', () => {5 const program = new commander.Command();6 const helpInformation = program.helpInformation();7 expect(helpInformation).not.toMatch(/help \[command\]/);8 });910 test('when program has no subcommands and add help command then has help command', () => {11 const program = new commander.Command();12 program.addHelpCommand(true);13 const helpInformation = program.helpInformation();14 expect(helpInformation).toMatch(/help \[command\]/);15 });1617 test('when program has subcommands then has automatic help command', () => {18 const program = new commander.Command();19 program.command('foo');20 const helpInformation = program.helpInformation();21 expect(helpInformation).toMatch(/help \[command\]/);22 });2324 test('when program has subcommands and specify only unknown option then display help', () => {25 const program = new commander.Command();26 program27 .configureHelp({ formatHelp: () => '' })28 .exitOverride()29 .allowUnknownOption()30 .command('foo');31 let caughtErr;32 try {33 program.parse(['--unknown'], { from: 'user' });34 } catch (err) {35 caughtErr = err;36 }37 expect(caughtErr.code).toEqual('commander.help');38 });3940 test('when program has subcommands and suppress help command then no help command', () => {41 const program = new commander.Command();42 program.addHelpCommand(false);43 program.command('foo');44 const helpInformation = program.helpInformation();45 expect(helpInformation).not.toMatch(/help \[command\]/);46 });4748 test('when add custom help command then custom help command', () => {49 const program = new commander.Command();50 program.addHelpCommand('myHelp', 'help description');51 const helpInformation = program.helpInformation();52 expect(helpInformation).toMatch(/myHelp +help description/);53 });54});5556describe('help command processed on correct command', () => {57 // Use internal knowledge to suppress output to keep test output clean.58 let writeErrorSpy;59 let writeSpy;6061 beforeAll(() => {62 writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });63 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });64 });6566 afterEach(() => {67 writeErrorSpy.mockClear();68 writeSpy.mockClear();69 });7071 afterAll(() => {72 writeErrorSpy.mockRestore();73 writeSpy.mockRestore();74 });7576 test('when "program help" then program', () => {77 const program = new commander.Command();78 program.exitOverride();79 program.command('sub1');80 program.exitOverride(() => { throw new Error('program'); });81 expect(() => {82 program.parse('node test.js help'.split(' '));83 }).toThrow('program');84 });8586 test('when "program help unknown" then program', () => {87 const program = new commander.Command();88 program.exitOverride();89 program.command('sub1');90 program.exitOverride(() => { throw new Error('program'); });91 expect(() => {92 program.parse('node test.js help unknown'.split(' '));93 }).toThrow('program');94 });9596 test('when "program help sub1" then sub1', () => {97 const program = new commander.Command();98 program.exitOverride();99 const sub1 = program.command('sub1');100 sub1.exitOverride(() => { throw new Error('sub1'); });101 expect(() => {102 program.parse('node test.js help sub1'.split(' '));103 }).toThrow('sub1');104 });105106 test('when "program sub1 help sub2" then sub2', () => {107 const program = new commander.Command();108 program.exitOverride();109 const sub1 = program.command('sub1');110 const sub2 = sub1.command('sub2');111 sub2.exitOverride(() => { throw new Error('sub2'); });112 expect(() => {113 program.parse('node test.js sub1 help sub2'.split(' '));114 }).toThrow('sub2');115 });116117 test('when default command and "program help" then program', () => {118 const program = new commander.Command();119 program.exitOverride();120 program.command('sub1', { isDefault: true });121 program.exitOverride(() => { throw new Error('program'); });122 expect(() => {123 program.parse('node test.js help'.split(' '));124 }).toThrow('program');125 });

Have help command call help directly for subcommands, when possible (#1864)John Geebefore adoption63abdac

lines 126–162why, one step back
126
127 test('when no long help flag then "help sub" works', () => {128 const program = new commander.Command();129 program.exitOverride();130 program.helpOption('-H');131 const sub = program.command('sub');132 // Patch help for easy way to check called.133 sub.help = () => { throw new Error('sub help'); };134 expect(() => {135 program.parse(['help', 'sub'], { from: 'user' });136 }).toThrow('sub help');137 });138139 test('when no help options in sub then "help sub" works', () => {140 const program = new commander.Command();141 program.exitOverride();142 const sub = program.command('sub')143 .helpOption(false);144 // Patch help for easy way to check called.145 sub.help = () => { throw new Error('sub help'); };146 expect(() => {147 program.parse(['help', 'sub'], { from: 'user' });148 }).toThrow('sub help');149 });150151 test('when different help options in sub then "help sub" works', () => {152 const program = new commander.Command();153 program.exitOverride();154 const sub = program.command('sub');155 program.helpOption('-h, --help');156 sub.helpOption('-a, --assist');157 // Patch help for easy way to check called.158 sub.help = () => { throw new Error('sub help'); };159 expect(() => {160 program.parse(['help', 'sub'], { from: 'user' });161 }).toThrow('sub help');162 });

Update dependencies (#1524)John Geebefore adoption0b0fbbe

line 163why, one step back
163});