options.implies.test.js
This file
Path
tests/options.implies.test.js
220 lines · 8.2 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221import { Command, Option } from '../index.js';import { createTestCommand } from './testHelpers.js';import { test, describe } from 'node:test';import assert from 'node:assert/strict';
describe('Option.implies()', () => { describe('check priorities', () => { test('when source undefined and implied undefined then implied is undefined', () => { const program = new Command(); program .addOption(new Option('--foo').implies({ bar: 'implied' })) .option('--bar'); program.parse([], { from: 'user' }); assert.deepEqual(program.opts(), {}); });
test('when source default and implied undefined then implied is undefined', () => { const program = new Command(); program .addOption( new Option('--foo').implies({ bar: 'implied' }).default('default'), ) .option('--bar'); program.parse([], { from: 'user' }); assert.deepEqual(program.opts(), { foo: 'default' }); });
test('when source from env and implied undefined then implied is implied', () => { const program = new Command(); const envName = 'COMMANDER_TEST_DELETE_ME'; process.env[envName] = 'env'; program .addOption(new Option('--foo').implies({ bar: 'implied' }).env(envName)) .option('--bar'); program.parse([], { from: 'user' }); assert.deepEqual(program.opts(), { foo: true, bar: 'implied' }); delete process.env[envName]; });
test('when source from cli and implied undefined then implied is implied', () => { const program = new Command(); program .addOption(new Option('--foo').implies({ bar: 'implied' })) .option('--bar'); program.parse(['--foo'], { from: 'user' }); assert.deepEqual(program.opts(), { foo: true, bar: 'implied' }); });
test('when source cli and implied default then implied is implied', () => { const program = new Command(); program .addOption(new Option('--foo').implies({ bar: 'implied' })) .option('--bar', '', 'default'); program.parse(['--foo'], { from: 'user' }); assert.deepEqual(program.opts(), { foo: true, bar: 'implied' }); });
test('when source cli and env default then implied is env', () => { const program = new Command(); const envName = 'COMMANDER_TEST_DELETE_ME'; process.env[envName] = 'env'; program .addOption(new Option('--foo').implies({ bar: 'implied' })) .addOption(new Option('--bar <value>').env(envName)); program.parse(['--foo'], { from: 'user' }); assert.deepEqual(program.opts(), { foo: true, bar: 'env' }); delete process.env[envName]; }); });
test('when imply non-option then ok and stored', () => { const program = new Command(); program.addOption(new Option('--foo').implies({ bar: 'implied' })); program.parse(['--foo'], { from: 'user' }); assert.deepEqual(program.opts(), { foo: true, bar: 'implied' }); });
test('when imply multiple values then store multiple values', () => { const program = new Command(); program .addOption(new Option('--foo').implies({ one: 'ONE', two: 'TWO' })) .option('--one') .option('--two'); program.parse(['--foo'], { from: 'user' }); assert.deepEqual(program.opts(), { foo: true, one: 'ONE', two: 'TWO' }); });
test('when imply multiple times then store multiple values', () => { const program = new Command(); program .addOption( new Option('--foo').implies({ one: 'ONE' }).implies({ two: 'TWO' }), ) .option('--one') .option('--two'); program.parse(['--foo'], { from: 'user' }); assert.deepEqual(program.opts(), { foo: true, one: 'ONE', two: 'TWO' }); });
test('when imply from positive option then positive implied', () => { const program = new Command(); program .addOption(new Option('--foo').implies({ implied: 'POSITIVE' })) .addOption(new Option('--no-foo').implies({ implied: 'NEGATIVE' })); program.parse(['--foo'], { from: 'user' }); assert.deepEqual(program.opts(), { foo: true, implied: 'POSITIVE' }); });
test('when imply from negative option then negative implied', () => { const program = new Command(); program .addOption(new Option('--foo').implies({ implied: 'POSITIVE' })) .addOption(new Option('--no-foo').implies({ implied: 'NEGATIVE' })); program.parse(['--no-foo'], { from: 'user' }); assert.deepEqual(program.opts(), { foo: false, implied: 'NEGATIVE' }); });
test('when imply from lone negative option then negative implied', () => { const program = new Command(); program.addOption(new Option('--no-foo').implies({ implied: 'NEGATIVE' })); program.parse(['--no-foo'], { from: 'user' }); assert.deepEqual(program.opts(), { foo: false, implied: 'NEGATIVE' }); });
test('when imply from negative option with preset then negative implied', () => { const program = new Command(); program .addOption(new Option('--foo').implies({ implied: 'POSITIVE' })) .addOption( new Option('--no-foo').implies({ implied: 'NEGATIVE' }).preset('FALSE'), ); program.parse(['--no-foo'], { from: 'user' }); assert.deepEqual(program.opts(), { foo: 'FALSE', implied: 'NEGATIVE' }); });
test('when chained implies then only explicitly trigger', () => { const program = new Command(); program .addOption(new Option('--one').implies({ two: true })) .addOption(new Option('--two').implies({ three: true })) .addOption(new Option('--three')); program.parse(['--one'], { from: 'user' }); assert.deepEqual(program.opts(), { one: true, two: true }); });
test('when looped implies then no infinite loop', () => { const program = new Command(); program .addOption(new Option('--ying').implies({ yang: true })) .addOption(new Option('--yang').implies({ ying: true })); program.parse(['--ying'], { from: 'user' }); assert.deepEqual(program.opts(), { ying: true, yang: true }); });
test('when conflict with implied value then throw', () => { const program = createTestCommand(); program .addOption(new Option('--unary')) .addOption(new Option('--binary').conflicts('unary')) .addOption(new Option('--one').implies({ unary: true }));
assert.throws(() => { program.parse(['--binary', '--one'], { from: 'user' }); }); });
test('when requiredOption with implied value then not throw', () => { const program = new Command(); program .requiredOption('--target <target-file>') .addOption( new Option('--default-target').implies({ target: 'default-file' }), );
assert.doesNotThrow(() => { program.parse(['--default-target'], { from: 'user' }); }); });
test('when implies on program and use subcommand then program updated', () => { const program = new Command(); program.addOption(new Option('--foo').implies({ bar: 'implied' })); program.command('sub').action(() => {}); program.parse(['--foo', 'sub'], { from: 'user' }); assert.equal(program.opts().bar, 'implied'); });
test('when option with implies used multiple times then implied gets single value', () => { const program = new Command(); program .addOption(new Option('--foo').implies({ bar: 'implied' })) .option('-b, --bar <value...>'); program.parse(['--foo', '--foo'], { from: 'user' }); assert.deepEqual(program.opts().bar, 'implied'); });
test('when implied option has custom processing then custom processing not called', () => { let called = false; const program = new Command(); program .addOption(new Option('--foo').implies({ bar: true })) .option('-b, --bar', 'description', () => { called = true; }); program.parse(['--foo'], { from: 'user' }); assert.equal(program.opts().bar, true); assert.equal(called, false); });
test('when passed string then treat as boolean', () => { // Do something sensible instead of something silly if user passes just name of option. // https://github.com/tj/commander.js/issues/1852 const program = new Command(); program .addOption(new Option('--foo').implies('bar')) .option('-b, --bar', 'description'); program.parse(['--foo'], { from: 'user' }); assert.equal(program.opts().bar, true); });});