Skip to content
Myr

options.env.test.js

This file

Path

tests/options.env.test.js

328 lines · 12.6 KiB · JavaScript

Last changed by

Add setOptionValueWithSource and getOptionValueSource (#1613)John Geebefore adoption6e00f44

History of this file

Take it

Add .env method to Option for consulting environment variable for option value (#1587)John Geebefore adoption8571a75

lines 1–29why, one step back
1const commander = require('../');
23// treating optional same as required, treat as option taking value rather than as boolean4describe.each(['-f, --foo <required-arg>', '-f, --foo [optional-arg]'])('option declared as: %s', (fooFlags) => {5 test('when env undefined and no cli then option undefined', () => {6 const program = new commander.Command();7 program.addOption(new commander.Option(fooFlags).env('BAR'));8 program.parse([], { from: 'user' });9 expect(program.opts().foo).toBeUndefined();10 });1112 test('when env defined and no cli then option from env', () => {13 const program = new commander.Command();14 process.env.BAR = 'env';15 program.addOption(new commander.Option(fooFlags).env('BAR'));16 program.parse([], { from: 'user' });17 expect(program.opts().foo).toBe('env');18 delete process.env.BAR;19 });2021 test('when env defined and cli then option from cli', () => {22 const program = new commander.Command();23 process.env.BAR = 'env';24 program.addOption(new commander.Option(fooFlags).env('BAR'));25 program.parse(['--foo', 'cli'], { from: 'user' });26 expect(program.opts().foo).toBe('cli');27 delete process.env.BAR;28 });29

Add setOptionValueWithSource and getOptionValueSource (#1613)John Geebefore adoption6e00f44

lines 30–49why, one step back
30 test('when env defined and value source is config then option from env', () => {
31 const program = new commander.Command();32 process.env.BAR = 'env';33 program.addOption(new commander.Option(fooFlags).env('BAR'));34 program.setOptionValueWithSource('foo', 'config', 'config');35 program.parse([], { from: 'user' });36 expect(program.opts().foo).toBe('env');37 delete process.env.BAR;38 });3940 test('when env defined and value source is unspecified then option unchanged', () => {41 const program = new commander.Command();42 process.env.BAR = 'env';43 program.addOption(new commander.Option(fooFlags).env('BAR'));44 program.setOptionValue('foo', 'client');45 program.parse([], { from: 'user' });46 expect(program.opts().foo).toBe('client');47 delete process.env.BAR;48 });49

Add .env method to Option for consulting environment variable for option value (#1587)John Geebefore adoption8571a75

lines 50–328why, one step back
50 test('when default and env undefined and no cli then option from default', () => {
51 const program = new commander.Command();52 program.addOption(new commander.Option(fooFlags).env('BAR').default('default'));53 program.parse([], { from: 'user' });54 expect(program.opts().foo).toBe('default');55 });5657 test('when default and env defined and no cli then option from env', () => {58 const program = new commander.Command();59 process.env.BAR = 'env';60 program.addOption(new commander.Option(fooFlags).env('BAR').default('default'));61 program.parse([], { from: 'user' });62 expect(program.opts().foo).toBe('env');63 delete process.env.BAR;64 });6566 test('when default and env defined and cli then option from cli', () => {67 const program = new commander.Command();68 process.env.BAR = 'env';69 program.addOption(new commander.Option(fooFlags).env('BAR').default('default'));70 program.parse(['--foo', 'cli'], { from: 'user' });71 expect(program.opts().foo).toBe('cli');72 delete process.env.BAR;73 });74});7576describe('boolean flag', () => {77 test('when env undefined and no cli then option undefined', () => {78 const program = new commander.Command();79 program.addOption(new commander.Option('-f, --foo').env('BAR'));80 program.parse([], { from: 'user' });81 expect(program.opts().foo).toBeUndefined();82 });8384 test('when env defined with value and no cli then option true', () => {85 const program = new commander.Command();86 process.env.BAR = 'env';87 program.addOption(new commander.Option('-f, --foo').env('BAR'));88 program.parse([], { from: 'user' });89 expect(program.opts().foo).toBe(true);90 delete process.env.BAR;91 });9293 test('when env is "" and no cli then option true', () => {94 // any string, including ""95 const program = new commander.Command();96 process.env.BAR = '';97 program.addOption(new commander.Option('-f, --foo').env('BAR'));98 program.parse([], { from: 'user' });99 expect(program.opts().foo).toBe(true);100 delete process.env.BAR;101 });102103 test('when env is "0" and no cli then option true', () => {104 // any string, including "0"105 const program = new commander.Command();106 process.env.BAR = '0';107 program.addOption(new commander.Option('-f, --foo').env('BAR'));108 program.parse([], { from: 'user' });109 expect(program.opts().foo).toBe(true);110 delete process.env.BAR;111 });112113 test('when env is "false" and no cli then option true', () => {114 // any string, including "false"115 const program = new commander.Command();116 process.env.BAR = 'false';117 program.addOption(new commander.Option('-f, --foo').env('BAR'));118 program.parse([], { from: 'user' });119 expect(program.opts().foo).toBe(true);120 delete process.env.BAR;121 });122});123124describe('boolean no-flag', () => {125 test('when env undefined and no cli then option true', () => {126 const program = new commander.Command();127 program.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));128 program.parse([], { from: 'user' });129 expect(program.opts().foo).toBe(true);130 });131132 test('when env defined and no cli then option false', () => {133 const program = new commander.Command();134 process.env.NO_BAR = 'env';135 program.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));136 program.parse([], { from: 'user' });137 expect(program.opts().foo).toBe(false);138 delete process.env.NO_BAR;139 });140});141142describe('boolean flag and negatable', () => {143 test('when env undefined and no cli then option undefined', () => {144 const program = new commander.Command();145 program146 .addOption(new commander.Option('-f, --foo').env('BAR'))147 .addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));148 program.parse([], { from: 'user' });149 expect(program.opts().foo).toBeUndefined();150 });151152 test('when env defined and no cli then option true', () => {153 const program = new commander.Command();154 process.env.BAR = 'env';155 program156 .addOption(new commander.Option('-f, --foo').env('BAR'))157 .addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));158 program.parse([], { from: 'user' });159 expect(program.opts().foo).toBe(true);160 delete process.env.BAR;161 });162163 test('when env defined and cli --no-foo then option false', () => {164 const program = new commander.Command();165 process.env.BAR = 'env';166 program167 .addOption(new commander.Option('-f, --foo').env('BAR'))168 .addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));169 program.parse(['--no-foo'], { from: 'user' });170 expect(program.opts().foo).toBe(false);171 delete process.env.BAR;172 });173174 test('when no_env defined and no cli then option false', () => {175 const program = new commander.Command();176 process.env.NO_BAR = 'env';177 program178 .addOption(new commander.Option('-f, --foo').env('BAR'))179 .addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));180 program.parse([], { from: 'user' });181 expect(program.opts().foo).toBe(false);182 delete process.env.NO_BAR;183 });184185 test('when no_env defined and cli --foo then option true', () => {186 const program = new commander.Command();187 process.env.NO_BAR = 'env';188 program189 .addOption(new commander.Option('-f, --foo').env('BAR'))190 .addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));191 program.parse(['--foo'], { from: 'user' });192 expect(program.opts().foo).toBe(true);193 delete process.env.NO_BAR;194 });195});196197describe('custom argParser', () => {198 test('when env defined and no cli then custom parse from env', () => {199 const program = new commander.Command();200 process.env.BAR = 'env';201 program.addOption(new commander.Option('-f, --foo <required>').env('BAR').argParser(str => str.toUpperCase()));202 program.parse([], { from: 'user' });203 expect(program.opts().foo).toBe('ENV');204 delete process.env.BAR;205 });206});207208describe('variadic', () => {209 test('when env defined and no cli then array from env', () => {210 const program = new commander.Command();211 process.env.BAR = 'env';212 program.addOption(new commander.Option('-f, --foo <required...>').env('BAR'));213 program.parse([], { from: 'user' });214 expect(program.opts().foo).toEqual(['env']);215 delete process.env.BAR;216 });217218 test('when env defined and cli then array from cli', () => {219 const program = new commander.Command();220 process.env.BAR = 'env';221 program.addOption(new commander.Option('-f, --foo <required...>').env('BAR'));222 program.parse(['--foo', 'cli'], { from: 'user' });223 expect(program.opts().foo).toEqual(['cli']);224 delete process.env.BAR;225 });226});227228describe('env only processed when applies', () => {229 test('when env defined on another subcommand then env not applied', () => {230 // Doing selective processing. Not processing env at addOption time.231 const program = new commander.Command();232 process.env.BAR = 'env';233 program.command('one')234 .action(() => {});235 const two = program.command('two')236 .addOption(new commander.Option('-f, --foo <required...>').env('BAR').default('default'))237 .action(() => {});238 program.parse(['one'], { from: 'user' });239 expect(two.opts().foo).toBe('default');240 delete process.env.BAR;241 });242243 test('when env and cli defined then only emit option event for cli', () => {244 const program = new commander.Command();245 const optionEventMock = jest.fn();246 const optionEnvEventMock = jest.fn();247 program.on('option:foo', optionEventMock);248 program.on('optionEnv:foo', optionEnvEventMock);249 process.env.BAR = 'env';250 program.addOption(new commander.Option('-f, --foo <required...>').env('BAR'));251 program.parse(['--foo', 'cli'], { from: 'user' });252 expect(optionEventMock).toHaveBeenCalledWith('cli');253 expect(optionEventMock).toHaveBeenCalledTimes(1);254 expect(optionEnvEventMock).toHaveBeenCalledTimes(0);255 delete process.env.BAR;256 });257258 test('when env and cli defined then only parse value for cli', () => {259 const program = new commander.Command();260 const parseMock = jest.fn();261 process.env.BAR = 'env';262 program.addOption(new commander.Option('-f, --foo <required...>').env('BAR').argParser(parseMock));263 program.parse(['--foo', 'cli'], { from: 'user' });264 expect(parseMock).toHaveBeenCalledWith('cli', undefined);265 expect(parseMock).toHaveBeenCalledTimes(1);266 delete process.env.BAR;267 });268});269270describe('events dispatched for env', () => {271 const optionEnvEventMock = jest.fn();272273 afterEach(() => {274 optionEnvEventMock.mockClear();275 delete process.env.BAR;276 });277278 test('when env defined then emit "optionEnv" and not "option"', () => {279 // Decided to do separate events, so test stays that way.280 const program = new commander.Command();281 const optionEventMock = jest.fn();282 program.on('option:foo', optionEventMock);283 program.on('optionEnv:foo', optionEnvEventMock);284 process.env.BAR = 'env';285 program.addOption(new commander.Option('-f, --foo <required>').env('BAR'));286 program.parse([], { from: 'user' });287 expect(optionEventMock).toHaveBeenCalledTimes(0);288 expect(optionEnvEventMock).toHaveBeenCalledTimes(1);289 });290291 test('when env defined for required then emit "optionEnv" with value', () => {292 const program = new commander.Command();293 program.on('optionEnv:foo', optionEnvEventMock);294 process.env.BAR = 'env';295 program.addOption(new commander.Option('-f, --foo <required>').env('BAR'));296 program.parse([], { from: 'user' });297 expect(optionEnvEventMock).toHaveBeenCalledWith('env');298 });299300 test('when env defined for optional then emit "optionEnv" with value', () => {301 const program = new commander.Command();302 program.on('optionEnv:foo', optionEnvEventMock);303 process.env.BAR = 'env';304 program.addOption(new commander.Option('-f, --foo [optional]').env('BAR'));305 program.parse([], { from: 'user' });306 expect(optionEnvEventMock).toHaveBeenCalledWith('env');307 });308309 test('when env defined for boolean then emit "optionEnv" with no param', () => {310 // check matches historical boolean action event311 const program = new commander.Command();312 program.on('optionEnv:foo', optionEnvEventMock);313 process.env.BAR = 'anything';314 program.addOption(new commander.Option('-f, --foo').env('BAR'));315 program.parse([], { from: 'user' });316 expect(optionEnvEventMock).toHaveBeenCalledWith();317 });318319 test('when env defined for negated boolean then emit "optionEnv" with no param', () => {320 // check matches historical boolean action event321 const program = new commander.Command();322 program.on('optionEnv:no-foo', optionEnvEventMock);323 process.env.BAR = 'anything';324 program.addOption(new commander.Option('-F, --no-foo').env('BAR'));325 program.parse([], { from: 'user' });326 expect(optionEnvEventMock).toHaveBeenCalledWith();327 });328});