command.helpCommand.test.js
This file
Path
tests/command.helpCommand.test.js
180 lines · 5.6 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 cc2db5b point at it.
Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 1–61why, one step back1const 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(() => {Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 62–65why, one step back62 writeErrorSpy = jest63 .spyOn(process.stderr, 'write')64 .mockImplementation(() => {});65 writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => {});Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 66–81why, one step back66 });6768 afterEach(() => {69 writeErrorSpy.mockClear();70 writeSpy.mockClear();71 });7273 afterAll(() => {74 writeErrorSpy.mockRestore();75 writeSpy.mockRestore();76 });7778 test('when "program help" then program', () => {79 const program = new commander.Command();80 program.exitOverride();81 program.command('sub1');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 82–84why, one step back82 program.exitOverride(() => {83 throw new Error('program');84 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 85–93why, one step back85 expect(() => {86 program.parse('node test.js help'.split(' '));87 }).toThrow('program');88 });8990 test('when "program help unknown" then program', () => {91 const program = new commander.Command();92 program.exitOverride();93 program.command('sub1');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 94–96why, one step back94 program.exitOverride(() => {95 throw new Error('program');96 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 97–105why, one step back97 expect(() => {98 program.parse('node test.js help unknown'.split(' '));99 }).toThrow('program');100 });101102 test('when "program help sub1" then sub1', () => {103 const program = new commander.Command();104 program.exitOverride();105 const sub1 = program.command('sub1');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 106–108why, one step back106 sub1.exitOverride(() => {107 throw new Error('sub1');108 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 109–118why, one step back109 expect(() => {110 program.parse('node test.js help sub1'.split(' '));111 }).toThrow('sub1');112 });113114 test('when "program sub1 help sub2" then sub2', () => {115 const program = new commander.Command();116 program.exitOverride();117 const sub1 = program.command('sub1');118 const sub2 = sub1.command('sub2');Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 119–121why, one step back119 sub2.exitOverride(() => {120 throw new Error('sub2');121 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 122–130why, one step back122 expect(() => {123 program.parse('node test.js sub1 help sub2'.split(' '));124 }).toThrow('sub2');125 });126127 test('when default command and "program help" then program', () => {128 const program = new commander.Command();129 program.exitOverride();130 program.command('sub1', { isDefault: true });Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 131–133why, one step back131 program.exitOverride(() => {132 throw new Error('program');133 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
lines 134–137why, one step back134 expect(() => {135 program.parse('node test.js help'.split(' '));136 }).toThrow('program');137 });Have help command call help directly for subcommands, when possible (#1864)John Geebefore adoption63abdac
lines 138–144why, one step back138139 test('when no long help flag then "help sub" works', () => {140 const program = new commander.Command();141 program.exitOverride();142 program.helpOption('-H');143 const sub = program.command('sub');144 // Patch help for easy way to check called.Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 145–147why, one step back145 sub.help = () => {146 throw new Error('sub help');147 };Have help command call help directly for subcommands, when possible (#1864)John Geebefore adoption63abdac
lines 148–155why, one step back148 expect(() => {149 program.parse(['help', 'sub'], { from: 'user' });150 }).toThrow('sub help');151 });152153 test('when no help options in sub then "help sub" works', () => {154 const program = new commander.Command();155 program.exitOverride();Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 156why, one step back156 const sub = program.command('sub').helpOption(false);Have help command call help directly for subcommands, when possible (#1864)John Geebefore adoption63abdac
line 157why, one step back157 // Patch help for easy way to check called.Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 158–160why, one step back158 sub.help = () => {159 throw new Error('sub help');160 };Have help command call help directly for subcommands, when possible (#1864)John Geebefore adoption63abdac
lines 161–172why, one step back161 expect(() => {162 program.parse(['help', 'sub'], { from: 'user' });163 }).toThrow('sub help');164 });165166 test('when different help options in sub then "help sub" works', () => {167 const program = new commander.Command();168 program.exitOverride();169 const sub = program.command('sub');170 program.helpOption('-h, --help');171 sub.helpOption('-a, --assist');172 // Patch help for easy way to check called.Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 173–175why, one step back173 sub.help = () => {174 throw new Error('sub help');175 };Have help command call help directly for subcommands, when possible (#1864)John Geebefore adoption63abdac
lines 176–179why, one step back176 expect(() => {177 program.parse(['help', 'sub'], { from: 'user' });178 }).toThrow('sub help');179 });Add optional summary for short form of subcommand description. (#1726)John Geebefore adoptioncc2db5b
line 180why, one step back180});