command.allowExcessArguments.test.js
This file
Path
tests/command.allowExcessArguments.test.js
129 lines · 3.6 KiB · JavaScript
Last changed by
Check command-arguments even without action handler (#1502)John Geebefore adoption0504945
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130const commander = require('../');
// Not testing output, just testing whether an error is detected.
describe.each([true, false])('allowExcessArguments when action handler: %s', (hasActionHandler) => { function configureCommand(cmd) { cmd .exitOverride() .configureOutput({ writeErr: () => {} }); if (hasActionHandler) { cmd.action(() => {}); } }
test('when specify excess program argument then no error by default', () => { const program = new commander.Command(); configureCommand(program);
expect(() => { program.parse(['excess'], { from: 'user' }); }).not.toThrow(); });
test('when specify excess program argument and allowExcessArguments(false) then error', () => { const program = new commander.Command(); configureCommand(program); program .allowExcessArguments(false);
expect(() => { program.parse(['excess'], { from: 'user' }); }).toThrow(); });
test('when specify excess program argument and allowExcessArguments() then no error', () => { const program = new commander.Command(); configureCommand(program); program .allowExcessArguments();
expect(() => { program.parse(['excess'], { from: 'user' }); }).not.toThrow(); });
test('when specify excess program argument and allowExcessArguments(true) then no error', () => { const program = new commander.Command(); configureCommand(program); program .allowExcessArguments(true);
expect(() => { program.parse(['excess'], { from: 'user' }); }).not.toThrow(); });
test('when specify excess command argument then no error (by default)', () => { const program = new commander.Command(); const sub = program .command('sub'); configureCommand(sub);
expect(() => { program.parse(['sub', 'excess'], { from: 'user' }); }).not.toThrow(); });
test('when specify excess command argument and allowExcessArguments(false) then error', () => { const program = new commander.Command(); const sub = program .command('sub') .allowExcessArguments(false); configureCommand(sub);
expect(() => { program.parse(['sub', 'excess'], { from: 'user' }); }).toThrow(); });
test('when specify expected arg and allowExcessArguments(false) then no error', () => { const program = new commander.Command(); configureCommand(program); program .argument('<file>') .allowExcessArguments(false);
expect(() => { program.parse(['file'], { from: 'user' }); }).not.toThrow(); });
test('when specify excess after <arg> and allowExcessArguments(false) then error', () => { const program = new commander.Command(); configureCommand(program); program .argument('<file>') .allowExcessArguments(false);
expect(() => { program.parse(['file', 'excess'], { from: 'user' }); }).toThrow(); });
test('when specify excess after [arg] and allowExcessArguments(false) then error', () => { const program = new commander.Command(); configureCommand(program); program .argument('[file]') .allowExcessArguments(false);
expect(() => { program.parse(['file', 'excess'], { from: 'user' }); }).toThrow(); });
test('when specify args for [args...] and allowExcessArguments(false) then no error', () => { const program = new commander.Command(); configureCommand(program); program .argument('[files...]') .allowExcessArguments(false);
expect(() => { program.parse(['file1', 'file2', 'file3'], { from: 'user' }); }).not.toThrow(); });});