options.env.test.js
This file
Path
tests/options.env.test.js
371 lines · 14.3 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–3why, one step back1import * as commander from '../index.js';2import { test, describe } from 'node:test';3import assert from 'node:assert/strict';Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 4why, one step back4Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 5–16why, one step back5describe('Option.env()', () => {6 // treating optional same as required, treat as option taking value rather than as boolean7 describe('option declared with value', () => {8 const flagsList = ['-f, --foo <required-arg>', '-f, --foo [optional-arg]'];9 for (const fooFlags of flagsList) {10 describe(`option declared as: ${fooFlags}`, () => {11 test('when env undefined and no cli then option undefined', () => {12 const program = new commander.Command();13 program.addOption(new commander.Option(fooFlags).env('BAR'));14 program.parse([], { from: 'user' });15 assert.equal(program.opts().foo, undefined);16 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 17why, one step back17Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 18–25why, one step back18 test('when env defined and no cli then option from env', () => {19 const program = new commander.Command();20 process.env.BAR = 'env';21 program.addOption(new commander.Option(fooFlags).env('BAR'));22 program.parse([], { from: 'user' });23 assert.equal(program.opts().foo, 'env');24 delete process.env.BAR;25 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 26why, one step back26Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 27–34why, one step back27 test('when env defined and cli then option from cli', () => {28 const program = new commander.Command();29 process.env.BAR = 'env';30 program.addOption(new commander.Option(fooFlags).env('BAR'));31 program.parse(['--foo', 'cli'], { from: 'user' });32 assert.equal(program.opts().foo, 'cli');33 delete process.env.BAR;34 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 35why, one step back35Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 36–44why, one step back36 test('when env defined and value source is config then option from env', () => {37 const program = new commander.Command();38 process.env.BAR = 'env';39 program.addOption(new commander.Option(fooFlags).env('BAR'));40 program.setOptionValueWithSource('foo', 'config', 'config');41 program.parse([], { from: 'user' });42 assert.equal(program.opts().foo, 'env');43 delete process.env.BAR;44 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 45why, one step back45Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 46–54why, one step back46 test('when env defined and value source is unspecified then option unchanged', () => {47 const program = new commander.Command();48 process.env.BAR = 'env';49 program.addOption(new commander.Option(fooFlags).env('BAR'));50 program.setOptionValue('foo', 'client');51 program.parse([], { from: 'user' });52 assert.equal(program.opts().foo, 'client');53 delete process.env.BAR;54 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 55why, one step back55Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 56–63why, one step back56 test('when default and env undefined and no cli then option from default', () => {57 const program = new commander.Command();58 program.addOption(59 new commander.Option(fooFlags).env('BAR').default('default'),60 );61 program.parse([], { from: 'user' });62 assert.equal(program.opts().foo, 'default');63 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 64why, one step back64Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 65–74why, one step back65 test('when default and env defined and no cli then option from env', () => {66 const program = new commander.Command();67 process.env.BAR = 'env';68 program.addOption(69 new commander.Option(fooFlags).env('BAR').default('default'),70 );71 program.parse([], { from: 'user' });72 assert.equal(program.opts().foo, 'env');73 delete process.env.BAR;74 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 75why, one step back75Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 76–85why, one step back76 test('when default and env defined and cli then option from cli', () => {77 const program = new commander.Command();78 process.env.BAR = 'env';79 program.addOption(80 new commander.Option(fooFlags).env('BAR').default('default'),81 );82 program.parse(['--foo', 'cli'], { from: 'user' });83 assert.equal(program.opts().foo, 'cli');84 delete process.env.BAR;85 });Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 86why, one step back86 });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 87–95why, one step back87 }88 });8990 describe('boolean flag', () => {91 test('when env undefined and no cli then option undefined', () => {92 const program = new commander.Command();93 program.addOption(new commander.Option('-f, --foo').env('BAR'));94 program.parse([], { from: 'user' });95 assert.equal(program.opts().foo, undefined);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 96why, one step back96 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 97why, one step back97Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 98–105why, one step back98 test('when env defined with value and no cli then option true', () => {99 const program = new commander.Command();100 process.env.BAR = 'env';101 program.addOption(new commander.Option('-f, --foo').env('BAR'));102 program.parse([], { from: 'user' });103 assert.equal(program.opts().foo, true);104 delete process.env.BAR;105 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 106why, one step back106Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 107–115why, one step back107 test('when env is "" and no cli then option true', () => {108 // any string, including ""109 const program = new commander.Command();110 process.env.BAR = '';111 program.addOption(new commander.Option('-f, --foo').env('BAR'));112 program.parse([], { from: 'user' });113 assert.equal(program.opts().foo, true);114 delete process.env.BAR;115 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 116why, one step back116Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 117–125why, one step back117 test('when env is "0" and no cli then option true', () => {118 // any string, including "0"119 const program = new commander.Command();120 process.env.BAR = '0';121 program.addOption(new commander.Option('-f, --foo').env('BAR'));122 program.parse([], { from: 'user' });123 assert.equal(program.opts().foo, true);124 delete process.env.BAR;125 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 126why, one step back126Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 127–135why, one step back127 test('when env is "false" and no cli then option true', () => {128 // any string, including "false"129 const program = new commander.Command();130 process.env.BAR = 'false';131 program.addOption(new commander.Option('-f, --foo').env('BAR'));132 program.parse([], { from: 'user' });133 assert.equal(program.opts().foo, true);134 delete process.env.BAR;135 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 136–137why, one step back136 });137Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 138–144why, one step back138 describe('boolean no-flag', () => {139 test('when env undefined and no cli then option true', () => {140 const program = new commander.Command();141 program.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));142 program.parse([], { from: 'user' });143 assert.equal(program.opts().foo, true);144 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 145why, one step back145Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 146–153why, one step back146 test('when env defined and no cli then option false', () => {147 const program = new commander.Command();148 process.env.NO_BAR = 'env';149 program.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));150 program.parse([], { from: 'user' });151 assert.equal(program.opts().foo, false);152 delete process.env.NO_BAR;153 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 154–155why, one step back154 });155Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 156–164why, one step back156 describe('boolean flag and negatable', () => {157 test('when env undefined and no cli then option undefined', () => {158 const program = new commander.Command();159 program160 .addOption(new commander.Option('-f, --foo').env('BAR'))161 .addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));162 program.parse([], { from: 'user' });163 assert.equal(program.opts().foo, undefined);164 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 165why, one step back165Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 166–175why, one step back166 test('when env defined and no cli then option true', () => {167 const program = new commander.Command();168 process.env.BAR = 'env';169 program170 .addOption(new commander.Option('-f, --foo').env('BAR'))171 .addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));172 program.parse([], { from: 'user' });173 assert.equal(program.opts().foo, true);174 delete process.env.BAR;175 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 176why, one step back176Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 177–186why, one step back177 test('when env defined and cli --no-foo then option false', () => {178 const program = new commander.Command();179 process.env.BAR = 'env';180 program181 .addOption(new commander.Option('-f, --foo').env('BAR'))182 .addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));183 program.parse(['--no-foo'], { from: 'user' });184 assert.equal(program.opts().foo, false);185 delete process.env.BAR;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–197why, one step back188 test('when no_env defined and no cli then option false', () => {189 const program = new commander.Command();190 process.env.NO_BAR = 'env';191 program192 .addOption(new commander.Option('-f, --foo').env('BAR'))193 .addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));194 program.parse([], { from: 'user' });195 assert.equal(program.opts().foo, false);196 delete process.env.NO_BAR;197 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 198why, one step back198Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 199–208why, one step back199 test('when no_env defined and cli --foo then option true', () => {200 const program = new commander.Command();201 process.env.NO_BAR = 'env';202 program203 .addOption(new commander.Option('-f, --foo').env('BAR'))204 .addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));205 program.parse(['--foo'], { from: 'user' });206 assert.equal(program.opts().foo, true);207 delete process.env.NO_BAR;208 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 209–210why, one step back209 });210Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 211–223why, one step back211 describe('custom argParser', () => {212 test('when env defined and no cli then custom parse from env', () => {213 const program = new commander.Command();214 process.env.BAR = 'env';215 program.addOption(216 new commander.Option('-f, --foo <required>')217 .env('BAR')218 .argParser((str) => str.toUpperCase()),219 );220 program.parse([], { from: 'user' });221 assert.equal(program.opts().foo, 'ENV');222 delete process.env.BAR;223 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 224–225why, one step back224 });225Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 226–236why, one step back226 describe('variadic', () => {227 test('when env defined and no cli then array from env', () => {228 const program = new commander.Command();229 process.env.BAR = 'env';230 program.addOption(231 new commander.Option('-f, --foo <required...>').env('BAR'),232 );233 program.parse([], { from: 'user' });234 assert.deepEqual(program.opts().foo, ['env']);235 delete process.env.BAR;236 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 237why, one step back237Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 238–247why, one step back238 test('when env defined and cli then array from cli', () => {239 const program = new commander.Command();240 process.env.BAR = 'env';241 program.addOption(242 new commander.Option('-f, --foo <required...>').env('BAR'),243 );244 program.parse(['--foo', 'cli'], { from: 'user' });245 assert.deepEqual(program.opts().foo, ['cli']);246 delete process.env.BAR;247 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 248–249why, one step back248 });249Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 250–267why, one step back250 describe('env only processed when applies', () => {251 test('when env defined on another subcommand then env not applied', () => {252 // Doing selective processing. Not processing env at addOption time.253 const program = new commander.Command();254 process.env.BAR = 'env';255 program.command('one').action(() => {});256 const two = program257 .command('two')258 .addOption(259 new commander.Option('-f, --foo <required...>')260 .env('BAR')261 .default('default'),262 )263 .action(() => {});264 program.parse(['one'], { from: 'user' });265 assert.equal(two.opts().foo, 'default');266 delete process.env.BAR;267 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 268why, one step back268Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 269–290why, one step back269 test('when env and cli defined then only emit option event for cli', (t) => {270 const program = new commander.Command();271 const optionEventMock = t.mock.fn();272 const optionEnvEventMock = t.mock.fn();273 program.on('option:foo', optionEventMock);274 program.on('optionEnv:foo', optionEnvEventMock);275 process.env.BAR = 'env';276 program.addOption(277 new commander.Option('-f, --foo <required...>').env('BAR'),278 );279 program.parse(['--foo', 'cli'], { from: 'user' });280 assert.equal(optionEventMock.mock.calls[0].arguments[0], 'cli');281 assert.equal(optionEventMock.mock.callCount(), 1);282 assert.equal(optionEnvEventMock.mock.callCount(), 0);283 delete process.env.BAR;284 });285286 test('when env and cli defined then only parse value for cli', (t) => {287 const program = new commander.Command();288 const parseMock = t.mock.fn();289 process.env.BAR = 'env';290 program.addOption(Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
lines 291–292why, one step back291 new commander.Option('-f, --foo <required...>')292 .env('BAR')Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 293–300why, one step back293 .argParser(parseMock),294 );295 program.parse(['--foo', 'cli'], { from: 'user' });296 assert.equal(parseMock.mock.calls[0].arguments[0], 'cli');297 assert.equal(parseMock.mock.calls[0].arguments[1], undefined);298 assert.equal(parseMock.mock.callCount(), 1);299 delete process.env.BAR;300 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 301–302why, one step back301 });302Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 303–319why, one step back303 describe('events dispatched for env', () => {304 test('when env defined then emit "optionEnv" and not "option"', (t) => {305 // Decided to do separate events, so test stays that way.306 const program = new commander.Command();307 const optionEventMock = t.mock.fn();308 const optionEnvEventMock = t.mock.fn();309 program.on('option:foo', optionEventMock);310 program.on('optionEnv:foo', optionEnvEventMock);311 process.env.BAR = 'env';312 program.addOption(313 new commander.Option('-f, --foo <required>').env('BAR'),314 );315 program.parse([], { from: 'user' });316 assert.equal(optionEventMock.mock.callCount(), 0);317 assert.equal(optionEnvEventMock.mock.callCount(), 1);318 delete process.env.BAR;319 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 320why, one step back320Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 321–332why, one step back321 test('when env defined for required then emit "optionEnv" with value', (t) => {322 const program = new commander.Command();323 const optionEnvEventMock = t.mock.fn();324 program.on('optionEnv:foo', optionEnvEventMock);325 process.env.BAR = 'env';326 program.addOption(327 new commander.Option('-f, --foo <required>').env('BAR'),328 );329 program.parse([], { from: 'user' });330 assert.equal(optionEnvEventMock.mock.calls[0].arguments[0], 'env');331 delete process.env.BAR;332 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 333why, one step back333Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 334–345why, one step back334 test('when env defined for optional then emit "optionEnv" with value', (t) => {335 const program = new commander.Command();336 const optionEnvEventMock = t.mock.fn();337 program.on('optionEnv:foo', optionEnvEventMock);338 process.env.BAR = 'env';339 program.addOption(340 new commander.Option('-f, --foo [optional]').env('BAR'),341 );342 program.parse([], { from: 'user' });343 assert.equal(optionEnvEventMock.mock.calls[0].arguments[0], 'env');344 delete process.env.BAR;345 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 346why, one step back346Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 347–357why, one step back347 test('when env defined for boolean then emit "optionEnv" with no param', (t) => {348 // check matches historical boolean action event349 const program = new commander.Command();350 const optionEnvEventMock = t.mock.fn();351 program.on('optionEnv:foo', optionEnvEventMock);352 process.env.BAR = 'anything';353 program.addOption(new commander.Option('-f, --foo').env('BAR'));354 program.parse([], { from: 'user' });355 assert.equal(optionEnvEventMock.mock.calls[0].arguments.length, 0);356 delete process.env.BAR;357 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 358why, one step back358Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 359–369why, one step back359 test('when env defined for negated boolean then emit "optionEnv" with no param', (t) => {360 // check matches historical boolean action event361 const program = new commander.Command();362 const optionEnvEventMock = t.mock.fn();363 program.on('optionEnv:no-foo', optionEnvEventMock);364 process.env.BAR = 'anything';365 program.addOption(new commander.Option('-F, --no-foo').env('BAR'));366 program.parse([], { from: 'user' });367 assert.equal(optionEnvEventMock.mock.calls[0].arguments.length, 0);368 delete process.env.BAR;369 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 370–371why, one step back370 });371});