command.allowExcessArguments.test.js
This file
Path
tests/command.allowExcessArguments.test.js
115 lines · 3.8 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116import { createTestCommand } from './testHelpers.js';import { test, describe } from 'node:test';import assert from 'node:assert/strict';
// Not testing output, just testing whether an error is detected.
describe('Command.allowExcessArguments()', () => { const cases = [true, false]; cases.forEach((hasActionHandler) => { describe(`when ${hasActionHandler ? 'has' : 'no'} action handler`, () => { function configureCommand(cmd) { if (hasActionHandler) cmd.action(() => {}); }
test('when specify excess program argument then error by default', () => { const program = createTestCommand(); configureCommand(program);
assert.throws(() => { program.parse(['excess'], { from: 'user' }); }); });
test('when specify excess program argument and allowExcessArguments(false) then error', () => { const program = createTestCommand(); configureCommand(program); program.allowExcessArguments(false);
assert.throws(() => { program.parse(['excess'], { from: 'user' }); }); });
test('when specify excess program argument and allowExcessArguments() then no error', () => { const program = createTestCommand(); configureCommand(program); program.allowExcessArguments();
assert.doesNotThrow(() => { program.parse(['excess'], { from: 'user' }); }); });
test('when specify excess program argument and allowExcessArguments(true) then no error', () => { const program = createTestCommand(); configureCommand(program); program.allowExcessArguments(true);
assert.doesNotThrow(() => { program.parse(['excess'], { from: 'user' }); }); });
test('when specify excess command argument then error (by default)', () => { const program = createTestCommand(); const sub = program.command('sub'); configureCommand(sub);
assert.throws(() => { program.parse(['sub', 'excess'], { from: 'user' }); }); });
test('when specify excess command argument and allowExcessArguments(false) then error', () => { const program = createTestCommand(); const sub = program.command('sub').allowExcessArguments(false); configureCommand(sub);
assert.throws(() => { program.parse(['sub', 'excess'], { from: 'user' }); }); });
test('when specify expected arg and allowExcessArguments(false) then no error', () => { const program = createTestCommand(); configureCommand(program); program.argument('<file>').allowExcessArguments(false);
assert.doesNotThrow(() => { program.parse(['file'], { from: 'user' }); }); });
test('when specify excess after <arg> and allowExcessArguments(false) then error', () => { const program = createTestCommand(); configureCommand(program); program.argument('<file>').allowExcessArguments(false);
assert.throws(() => { program.parse(['file', 'excess'], { from: 'user' }); }); });
test('when specify excess after [arg] and allowExcessArguments(false) then error', () => { const program = createTestCommand(); configureCommand(program); program.argument('[file]').allowExcessArguments(false);
assert.throws(() => { program.parse(['file', 'excess'], { from: 'user' }); }); });
test('when specify args for [args...] and allowExcessArguments(false) then no error', () => { const program = createTestCommand(); configureCommand(program); program.argument('[files...]').allowExcessArguments(false);
assert.doesNotThrow(() => { program.parse(['file1', 'file2', 'file3'], { from: 'user' }); }); }); }); });});