command.addHelpText.test.js
This file
Path
tests/command.addHelpText.test.js
239 lines · 7.6 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240import * as commander from '../index.js';import { test, describe, beforeEach } from 'node:test';import assert from 'node:assert/strict';
// Using outputHelp to simplify testing.
describe('Command.addHelpText(): program calls to addHelpText', () => { let writeSpy;
beforeEach((t) => { writeSpy = t.mock.method(process.stdout, 'write', () => {}); });
test('when "before" string then string before built-in help', () => { const program = new commander.Command(); program.addHelpText('before', 'text'); program.outputHelp(); assert.equal(writeSpy.mock.calls[0].arguments[0], 'text\n'); assert.equal( writeSpy.mock.calls[1].arguments[0], program.helpInformation(), ); });
test('when "before" function then function result before built-in help', () => { const program = new commander.Command(); program.addHelpText('before', () => 'text'); program.outputHelp(); assert.equal(writeSpy.mock.calls[0].arguments[0], 'text\n'); assert.equal( writeSpy.mock.calls[1].arguments[0], program.helpInformation(), ); });
test('when "before" function returns nothing then no effect', () => { const program = new commander.Command(); program.addHelpText('before', () => {}); program.outputHelp(); assert.equal( writeSpy.mock.calls[0].arguments[0], program.helpInformation(), ); });
test('when "beforeAll" string then string before built-in help', () => { const program = new commander.Command(); program.addHelpText('beforeAll', 'text'); program.outputHelp(); assert.equal(writeSpy.mock.calls[0].arguments[0], 'text\n'); assert.equal( writeSpy.mock.calls[1].arguments[0], program.helpInformation(), ); });
test('when "after" string then string after built-in help', () => { const program = new commander.Command(); program.addHelpText('after', 'text'); program.outputHelp(); assert.equal( writeSpy.mock.calls[0].arguments[0], program.helpInformation(), ); assert.equal(writeSpy.mock.calls[1].arguments[0], 'text\n'); });
test('when "afterAll" string then string after built-in help', () => { const program = new commander.Command(); program.addHelpText('afterAll', 'text'); program.outputHelp(); assert.equal( writeSpy.mock.calls[0].arguments[0], program.helpInformation(), ); assert.equal(writeSpy.mock.calls[1].arguments[0], 'text\n'); });
test('when all the simple positions then strings in order', () => { const program = new commander.Command(); program.addHelpText('before', 'before'); program.addHelpText('after', 'after'); program.addHelpText('beforeAll', 'beforeAll'); program.addHelpText('afterAll', 'afterAll'); program.outputHelp(); assert.equal(writeSpy.mock.calls[0].arguments[0], 'beforeAll\n'); assert.equal(writeSpy.mock.calls[1].arguments[0], 'before\n'); assert.equal( writeSpy.mock.calls[2].arguments[0], program.helpInformation(), ); assert.equal(writeSpy.mock.calls[3].arguments[0], 'after\n'); assert.equal(writeSpy.mock.calls[4].arguments[0], 'afterAll\n'); });
test('when "silly" position then throw', () => { const program = new commander.Command(); assert.throws(() => { program.addHelpText('silly', 'text'); }); });});
describe('Command.addHelpText(): program and subcommand calls to addHelpText', () => { let writeSpy;
beforeEach((t) => { writeSpy = t.mock.method(process.stdout, 'write', () => {}); });
test('when "before" on program then not called on subcommand', (t) => { const program = new commander.Command(); const sub = program.command('sub'); const testMock = t.mock.fn(); program.addHelpText('before', testMock); sub.outputHelp(); assert.equal(testMock.mock.callCount(), 0); });
test('when "beforeAll" on program then is called on subcommand', (t) => { const program = new commander.Command(); const sub = program.command('sub'); const testMock = t.mock.fn(); program.addHelpText('beforeAll', testMock); sub.outputHelp(); assert.equal(testMock.mock.callCount(), 1); });
test('when "after" on program then not called on subcommand', (t) => { const program = new commander.Command(); const sub = program.command('sub'); const testMock = t.mock.fn(); program.addHelpText('after', testMock); sub.outputHelp(); assert.equal(testMock.mock.callCount(), 0); });
test('when "afterAll" on program then is called on subcommand', (t) => { const program = new commander.Command(); const sub = program.command('sub'); const testMock = t.mock.fn(); program.addHelpText('afterAll', testMock); sub.outputHelp(); assert.equal(testMock.mock.callCount(), 1); });});
describe('Command.addHelpText(): context checks with full parse', () => { let stdoutSpy; let stderrSpy;
beforeEach((t) => { stdoutSpy = t.mock.method(process.stdout, 'write', () => {}); stderrSpy = t.mock.method(process.stderr, 'write', () => {}); });
test('when help requested then text is on stdout', () => { const program = new commander.Command(); program.exitOverride().addHelpText('before', 'text'); assert.throws(() => { program.parse(['--help'], { from: 'user' }); }); assert.equal(stdoutSpy.mock.calls[0].arguments[0], 'text\n'); });
test('when help for error then text is on stderr', () => { const program = new commander.Command(); program.exitOverride().addHelpText('before', 'text').command('sub'); assert.throws(() => { program.parse([], { from: 'user' }); }); assert.equal(stderrSpy.mock.calls[0].arguments[0], 'text\n'); });
test('when help requested then context.error is false', () => { let context; const program = new commander.Command(); program.exitOverride().addHelpText('before', (contextParam) => { context = contextParam; }); assert.throws(() => { program.parse(['--help'], { from: 'user' }); }); assert.equal(context.error, false); });
test('when help for error then context.error is true', () => { let context; const program = new commander.Command(); program .exitOverride() .addHelpText('before', (contextParam) => { context = contextParam; }) .command('sub'); assert.throws(() => { program.parse([], { from: 'user' }); }); assert.equal(context.error, true); });
test('when help on program then context.command is program', () => { let context; const program = new commander.Command(); program.exitOverride().addHelpText('before', (contextParam) => { context = contextParam; }); assert.throws(() => { program.parse(['--help'], { from: 'user' }); }); assert.equal(context.command, program); });
test('when help on subcommand and "before" subcommand then context.command is subcommand', () => { let context; const program = new commander.Command(); program.exitOverride(); const sub = program.command('sub').addHelpText('before', (contextParam) => { context = contextParam; }); assert.throws(() => { program.parse(['sub', '--help'], { from: 'user' }); }); assert.equal(context.command, sub); });
test('when help on subcommand and "beforeAll" on program then context.command is subcommand', () => { let context; const program = new commander.Command(); program.exitOverride().addHelpText('beforeAll', (contextParam) => { context = contextParam; }); const sub = program.command('sub'); assert.throws(() => { program.parse(['sub', '--help'], { from: 'user' }); }); assert.equal(context.command, sub); });});