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 a71d592 point at it.

Improve basedir calculation to fix #1274 (#1322)John Geebefore adoptiona71d592

lines 1–23why, 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 });23

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

lines 24–39why, one step back
24 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 });39

Improve basedir calculation to fix #1274 (#1322)John Geebefore adoptiona71d592

lines 40–49why, one step back
40 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();

Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912

line 50why, one step back
50 program.addHelpCommand('myHelp', 'help description');

Improve basedir calculation to fix #1274 (#1322)John Geebefore adoptiona71d592

line 51why, one step back
51 const helpInformation = program.helpInformation();

Refactor help internals into separate interface/class (#1365)John Geebefore adoption19ae912

line 52why, one step back
52 expect(helpInformation).toMatch(/myHelp +help description/);

Improve basedir calculation to fix #1274 (#1322)John Geebefore adoptiona71d592

lines 53–57why, one step back
53 });
54});5556describe('help command processed on correct command', () => {57 // Use internal knowledge to suppress output to keep test output clean.

configure output (#1387)John Geebefore adoptioned7f13e

line 58why, one step back
58 let writeErrorSpy;

Improve basedir calculation to fix #1274 (#1322)John Geebefore adoptiona71d592

lines 59–61why, one step back
59 let writeSpy;
6061 beforeAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 62why, one step back
62 writeErrorSpy = jest.spyOn(process.stderr, 'write').mockImplementation(() => { });

Improve basedir calculation to fix #1274 (#1322)John Geebefore adoptiona71d592

lines 63–66why, one step back
63 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
64 });6566 afterEach(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 67why, one step back
67 writeErrorSpy.mockClear();

Improve basedir calculation to fix #1274 (#1322)John Geebefore adoptiona71d592

lines 68–71why, one step back
68 writeSpy.mockClear();
69 });7071 afterAll(() => {

configure output (#1387)John Geebefore adoptioned7f13e

line 72why, one step back
72 writeErrorSpy.mockRestore();

Improve basedir calculation to fix #1274 (#1322)John Geebefore adoptiona71d592

lines 73–82why, one step back
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(' '));

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

lines 83–92why, one step back
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(' '));

Improve basedir calculation to fix #1274 (#1322)John Geebefore adoptiona71d592

lines 93–125why, one step back
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 });

Improve basedir calculation to fix #1274 (#1322)John Geebefore adoptiona71d592

line 163why, one step back
163});