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
The walk stopped after 200 commits: lines older than 471ec40 point at it.
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–4why, one step back1import { Command, Option } from '../index.js';2import { createTestCommand } from './testHelpers.js';3import { test, describe } from 'node:test';4import assert from 'node:assert/strict';Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 5why, one step back5Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 6–71why, one step back6describe('Option.implies()', () => {7 describe('check priorities', () => {8 test('when source undefined and implied undefined then implied is undefined', () => {9 const program = new Command();10 program11 .addOption(new Option('--foo').implies({ bar: 'implied' }))12 .option('--bar');13 program.parse([], { from: 'user' });14 assert.deepEqual(program.opts(), {});15 });1617 test('when source default and implied undefined then implied is undefined', () => {18 const program = new Command();19 program20 .addOption(21 new Option('--foo').implies({ bar: 'implied' }).default('default'),22 )23 .option('--bar');24 program.parse([], { from: 'user' });25 assert.deepEqual(program.opts(), { foo: 'default' });26 });2728 test('when source from env and implied undefined then implied is implied', () => {29 const program = new Command();30 const envName = 'COMMANDER_TEST_DELETE_ME';31 process.env[envName] = 'env';32 program33 .addOption(new Option('--foo').implies({ bar: 'implied' }).env(envName))34 .option('--bar');35 program.parse([], { from: 'user' });36 assert.deepEqual(program.opts(), { foo: true, bar: 'implied' });37 delete process.env[envName];38 });3940 test('when source from cli and implied undefined then implied is implied', () => {41 const program = new Command();42 program43 .addOption(new Option('--foo').implies({ bar: 'implied' }))44 .option('--bar');45 program.parse(['--foo'], { from: 'user' });46 assert.deepEqual(program.opts(), { foo: true, bar: 'implied' });47 });4849 test('when source cli and implied default then implied is implied', () => {50 const program = new Command();51 program52 .addOption(new Option('--foo').implies({ bar: 'implied' }))53 .option('--bar', '', 'default');54 program.parse(['--foo'], { from: 'user' });55 assert.deepEqual(program.opts(), { foo: true, bar: 'implied' });56 });5758 test('when source cli and env default then implied is env', () => {59 const program = new Command();60 const envName = 'COMMANDER_TEST_DELETE_ME';61 process.env[envName] = 'env';62 program63 .addOption(new Option('--foo').implies({ bar: 'implied' }))64 .addOption(new Option('--bar <value>').env(envName));65 program.parse(['--foo'], { from: 'user' });66 assert.deepEqual(program.opts(), { foo: true, bar: 'env' });67 delete process.env[envName];68 });69 });7071 test('when imply non-option then ok and stored', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 72why, one step back72 const program = new Command();Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 73–79why, one step back73 program.addOption(new Option('--foo').implies({ bar: 'implied' }));74 program.parse(['--foo'], { from: 'user' });75 assert.deepEqual(program.opts(), { foo: true, bar: 'implied' });76 });7778 test('when imply multiple values then store multiple values', () => {79 const program = new Command();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 80why, one step back80 programAdd describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 81–85why, one step back81 .addOption(new Option('--foo').implies({ one: 'ONE', two: 'TWO' }))82 .option('--one')83 .option('--two');84 program.parse(['--foo'], { from: 'user' });85 assert.deepEqual(program.opts(), { foo: true, one: 'ONE', two: 'TWO' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 86–87why, one step back86 });87Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 88why, one step back88 test('when imply multiple times then store multiple values', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 89–90why, one step back89 const program = new Command();90 programReformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 91why, one step back91 .addOption(Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 92why, one step back92 new Option('--foo').implies({ one: 'ONE' }).implies({ two: 'TWO' }),Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 93why, one step back93 )Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 94–97why, one step back94 .option('--one')95 .option('--two');96 program.parse(['--foo'], { from: 'user' });97 assert.deepEqual(program.opts(), { foo: true, one: 'ONE', two: 'TWO' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 98–99why, one step back98 });99Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 100why, one step back100 test('when imply from positive option then positive implied', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 101–102why, one step back101 const program = new Command();102 programAdd describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 103–106why, one step back103 .addOption(new Option('--foo').implies({ implied: 'POSITIVE' }))104 .addOption(new Option('--no-foo').implies({ implied: 'NEGATIVE' }));105 program.parse(['--foo'], { from: 'user' });106 assert.deepEqual(program.opts(), { foo: true, implied: 'POSITIVE' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 107–108why, one step back107 });108Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 109why, one step back109 test('when imply from negative option then negative implied', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 110–111why, one step back110 const program = new Command();111 programAdd describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 112–115why, one step back112 .addOption(new Option('--foo').implies({ implied: 'POSITIVE' }))113 .addOption(new Option('--no-foo').implies({ implied: 'NEGATIVE' }));114 program.parse(['--no-foo'], { from: 'user' });115 assert.deepEqual(program.opts(), { foo: false, implied: 'NEGATIVE' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 116–117why, one step back116 });117Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 118why, one step back118 test('when imply from lone negative option then negative implied', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 119why, one step back119 const program = new Command();Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 120–126why, one step back120 program.addOption(new Option('--no-foo').implies({ implied: 'NEGATIVE' }));121 program.parse(['--no-foo'], { from: 'user' });122 assert.deepEqual(program.opts(), { foo: false, implied: 'NEGATIVE' });123 });124125 test('when imply from negative option with preset then negative implied', () => {126 const program = new Command();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 127why, one step back127 programAdd describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 128–133why, one step back128 .addOption(new Option('--foo').implies({ implied: 'POSITIVE' }))129 .addOption(130 new Option('--no-foo').implies({ implied: 'NEGATIVE' }).preset('FALSE'),131 );132 program.parse(['--no-foo'], { from: 'user' });133 assert.deepEqual(program.opts(), { foo: 'FALSE', implied: 'NEGATIVE' });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 134–135why, one step back134 });135Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 136why, one step back136 test('when chained implies then only explicitly trigger', () => {Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 137–138why, one step back137 const program = new Command();138 programAdd describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 139–143why, one step back139 .addOption(new Option('--one').implies({ two: true }))140 .addOption(new Option('--two').implies({ three: true }))141 .addOption(new Option('--three'));142 program.parse(['--one'], { from: 'user' });143 assert.deepEqual(program.opts(), { one: true, two: true });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 144–145why, one step back144 });145Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 146–153why, one step back146 test('when looped implies then no infinite loop', () => {147 const program = new Command();148 program149 .addOption(new Option('--ying').implies({ yang: true }))150 .addOption(new Option('--yang').implies({ ying: true }));151 program.parse(['--ying'], { from: 'user' });152 assert.deepEqual(program.opts(), { ying: true, yang: true });153 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 154why, one step back154Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 155–160why, one step back155 test('when conflict with implied value then throw', () => {156 const program = createTestCommand();157 program158 .addOption(new Option('--unary'))159 .addOption(new Option('--binary').conflicts('unary'))160 .addOption(new Option('--one').implies({ unary: true }));Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 161why, one step back161Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 162–165why, one step back162 assert.throws(() => {163 program.parse(['--binary', '--one'], { from: 'user' });164 });165 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 166why, one step back166Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 167–173why, one step back167 test('when requiredOption with implied value then not throw', () => {168 const program = new Command();169 program170 .requiredOption('--target <target-file>')171 .addOption(172 new Option('--default-target').implies({ target: 'default-file' }),173 );Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 174why, one step back174Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 175–178why, one step back175 assert.doesNotThrow(() => {176 program.parse(['--default-target'], { from: 'user' });177 });178 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 179why, one step back179Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 180–186why, one step back180 test('when implies on program and use subcommand then program updated', () => {181 const program = new Command();182 program.addOption(new Option('--foo').implies({ bar: 'implied' }));183 program.command('sub').action(() => {});184 program.parse(['--foo', 'sub'], { from: 'user' });185 assert.equal(program.opts().bar, 'implied');186 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 187why, one step back187Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 188–194why, one step back188 test('when option with implies used multiple times then implied gets single value', () => {189 const program = new Command();190 program191 .addOption(new Option('--foo').implies({ bar: 'implied' }))192 .option('-b, --bar <value...>');193 program.parse(['--foo', '--foo'], { from: 'user' });194 assert.deepEqual(program.opts().bar, 'implied');Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 195why, one step back195 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 196why, one step back196Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 197–207why, one step back197 test('when implied option has custom processing then custom processing not called', () => {198 let called = false;199 const program = new Command();200 program201 .addOption(new Option('--foo').implies({ bar: true }))202 .option('-b, --bar', 'description', () => {203 called = true;204 });205 program.parse(['--foo'], { from: 'user' });206 assert.equal(program.opts().bar, true);207 assert.equal(called, false);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 208why, one step back208 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 209why, one step back209Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 210–219why, one step back210 test('when passed string then treat as boolean', () => {211 // Do something sensible instead of something silly if user passes just name of option.212 // https://github.com/tj/commander.js/issues/1852213 const program = new Command();214 program215 .addOption(new Option('--foo').implies('bar'))216 .option('-b, --bar', 'description');217 program.parse(['--foo'], { from: 'user' });218 assert.equal(program.opts().bar, true);219 });Assume a string parameter to implies is name of boolean option. (#1854)John Geebefore adoptionf4c7349
line 220why, one step back220});