forked from tj/commander.js
/
options.env.test.js
328 lines (295 loc) · 12.6 KB
/
options.env.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const commander = require('../');
// treating optional same as required, treat as option taking value rather than as boolean
describe.each(['-f, --foo <required-arg>', '-f, --foo [optional-arg]'])('option declared as: %s', (fooFlags) => {
test('when env undefined and no cli then option undefined', () => {
const program = new commander.Command();
program.addOption(new commander.Option(fooFlags).env('BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBeUndefined();
});
test('when env defined and no cli then option from env', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program.addOption(new commander.Option(fooFlags).env('BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe('env');
delete process.env.BAR;
});
test('when env defined and cli then option from cli', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program.addOption(new commander.Option(fooFlags).env('BAR'));
program.parse(['--foo', 'cli'], { from: 'user' });
expect(program.opts().foo).toBe('cli');
delete process.env.BAR;
});
test('when env defined and value source is config then option from env', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program.addOption(new commander.Option(fooFlags).env('BAR'));
program.setOptionValueWithSource('foo', 'config', 'config');
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe('env');
delete process.env.BAR;
});
test('when env defined and value source is unspecified then option unchanged', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program.addOption(new commander.Option(fooFlags).env('BAR'));
program.setOptionValue('foo', 'client');
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe('client');
delete process.env.BAR;
});
test('when default and env undefined and no cli then option from default', () => {
const program = new commander.Command();
program.addOption(new commander.Option(fooFlags).env('BAR').default('default'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe('default');
});
test('when default and env defined and no cli then option from env', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program.addOption(new commander.Option(fooFlags).env('BAR').default('default'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe('env');
delete process.env.BAR;
});
test('when default and env defined and cli then option from cli', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program.addOption(new commander.Option(fooFlags).env('BAR').default('default'));
program.parse(['--foo', 'cli'], { from: 'user' });
expect(program.opts().foo).toBe('cli');
delete process.env.BAR;
});
});
describe('boolean flag', () => {
test('when env undefined and no cli then option undefined', () => {
const program = new commander.Command();
program.addOption(new commander.Option('-f, --foo').env('BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBeUndefined();
});
test('when env defined with value and no cli then option true', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program.addOption(new commander.Option('-f, --foo').env('BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe(true);
delete process.env.BAR;
});
test('when env is "" and no cli then option true', () => {
// any string, including ""
const program = new commander.Command();
process.env.BAR = '';
program.addOption(new commander.Option('-f, --foo').env('BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe(true);
delete process.env.BAR;
});
test('when env is "0" and no cli then option true', () => {
// any string, including "0"
const program = new commander.Command();
process.env.BAR = '0';
program.addOption(new commander.Option('-f, --foo').env('BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe(true);
delete process.env.BAR;
});
test('when env is "false" and no cli then option true', () => {
// any string, including "false"
const program = new commander.Command();
process.env.BAR = 'false';
program.addOption(new commander.Option('-f, --foo').env('BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe(true);
delete process.env.BAR;
});
});
describe('boolean no-flag', () => {
test('when env undefined and no cli then option true', () => {
const program = new commander.Command();
program.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe(true);
});
test('when env defined and no cli then option false', () => {
const program = new commander.Command();
process.env.NO_BAR = 'env';
program.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe(false);
delete process.env.NO_BAR;
});
});
describe('boolean flag and negatable', () => {
test('when env undefined and no cli then option undefined', () => {
const program = new commander.Command();
program
.addOption(new commander.Option('-f, --foo').env('BAR'))
.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBeUndefined();
});
test('when env defined and no cli then option true', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program
.addOption(new commander.Option('-f, --foo').env('BAR'))
.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe(true);
delete process.env.BAR;
});
test('when env defined and cli --no-foo then option false', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program
.addOption(new commander.Option('-f, --foo').env('BAR'))
.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));
program.parse(['--no-foo'], { from: 'user' });
expect(program.opts().foo).toBe(false);
delete process.env.BAR;
});
test('when no_env defined and no cli then option false', () => {
const program = new commander.Command();
process.env.NO_BAR = 'env';
program
.addOption(new commander.Option('-f, --foo').env('BAR'))
.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe(false);
delete process.env.NO_BAR;
});
test('when no_env defined and cli --foo then option true', () => {
const program = new commander.Command();
process.env.NO_BAR = 'env';
program
.addOption(new commander.Option('-f, --foo').env('BAR'))
.addOption(new commander.Option('-F, --no-foo').env('NO_BAR'));
program.parse(['--foo'], { from: 'user' });
expect(program.opts().foo).toBe(true);
delete process.env.NO_BAR;
});
});
describe('custom argParser', () => {
test('when env defined and no cli then custom parse from env', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program.addOption(new commander.Option('-f, --foo <required>').env('BAR').argParser(str => str.toUpperCase()));
program.parse([], { from: 'user' });
expect(program.opts().foo).toBe('ENV');
delete process.env.BAR;
});
});
describe('variadic', () => {
test('when env defined and no cli then array from env', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program.addOption(new commander.Option('-f, --foo <required...>').env('BAR'));
program.parse([], { from: 'user' });
expect(program.opts().foo).toEqual(['env']);
delete process.env.BAR;
});
test('when env defined and cli then array from cli', () => {
const program = new commander.Command();
process.env.BAR = 'env';
program.addOption(new commander.Option('-f, --foo <required...>').env('BAR'));
program.parse(['--foo', 'cli'], { from: 'user' });
expect(program.opts().foo).toEqual(['cli']);
delete process.env.BAR;
});
});
describe('env only processed when applies', () => {
test('when env defined on another subcommand then env not applied', () => {
// Doing selective processing. Not processing env at addOption time.
const program = new commander.Command();
process.env.BAR = 'env';
program.command('one')
.action(() => {});
const two = program.command('two')
.addOption(new commander.Option('-f, --foo <required...>').env('BAR').default('default'))
.action(() => {});
program.parse(['one'], { from: 'user' });
expect(two.opts().foo).toBe('default');
delete process.env.BAR;
});
test('when env and cli defined then only emit option event for cli', () => {
const program = new commander.Command();
const optionEventMock = jest.fn();
const optionEnvEventMock = jest.fn();
program.on('option:foo', optionEventMock);
program.on('optionEnv:foo', optionEnvEventMock);
process.env.BAR = 'env';
program.addOption(new commander.Option('-f, --foo <required...>').env('BAR'));
program.parse(['--foo', 'cli'], { from: 'user' });
expect(optionEventMock).toHaveBeenCalledWith('cli');
expect(optionEventMock).toHaveBeenCalledTimes(1);
expect(optionEnvEventMock).toHaveBeenCalledTimes(0);
delete process.env.BAR;
});
test('when env and cli defined then only parse value for cli', () => {
const program = new commander.Command();
const parseMock = jest.fn();
process.env.BAR = 'env';
program.addOption(new commander.Option('-f, --foo <required...>').env('BAR').argParser(parseMock));
program.parse(['--foo', 'cli'], { from: 'user' });
expect(parseMock).toHaveBeenCalledWith('cli', undefined);
expect(parseMock).toHaveBeenCalledTimes(1);
delete process.env.BAR;
});
});
describe('events dispatched for env', () => {
const optionEnvEventMock = jest.fn();
afterEach(() => {
optionEnvEventMock.mockClear();
delete process.env.BAR;
});
test('when env defined then emit "optionEnv" and not "option"', () => {
// Decided to do separate events, so test stays that way.
const program = new commander.Command();
const optionEventMock = jest.fn();
program.on('option:foo', optionEventMock);
program.on('optionEnv:foo', optionEnvEventMock);
process.env.BAR = 'env';
program.addOption(new commander.Option('-f, --foo <required>').env('BAR'));
program.parse([], { from: 'user' });
expect(optionEventMock).toHaveBeenCalledTimes(0);
expect(optionEnvEventMock).toHaveBeenCalledTimes(1);
});
test('when env defined for required then emit "optionEnv" with value', () => {
const program = new commander.Command();
program.on('optionEnv:foo', optionEnvEventMock);
process.env.BAR = 'env';
program.addOption(new commander.Option('-f, --foo <required>').env('BAR'));
program.parse([], { from: 'user' });
expect(optionEnvEventMock).toHaveBeenCalledWith('env');
});
test('when env defined for optional then emit "optionEnv" with value', () => {
const program = new commander.Command();
program.on('optionEnv:foo', optionEnvEventMock);
process.env.BAR = 'env';
program.addOption(new commander.Option('-f, --foo [optional]').env('BAR'));
program.parse([], { from: 'user' });
expect(optionEnvEventMock).toHaveBeenCalledWith('env');
});
test('when env defined for boolean then emit "optionEnv" with no param', () => {
// check matches historical boolean action event
const program = new commander.Command();
program.on('optionEnv:foo', optionEnvEventMock);
process.env.BAR = 'anything';
program.addOption(new commander.Option('-f, --foo').env('BAR'));
program.parse([], { from: 'user' });
expect(optionEnvEventMock).toHaveBeenCalledWith();
});
test('when env defined for negated boolean then emit "optionEnv" with no param', () => {
// check matches historical boolean action event
const program = new commander.Command();
program.on('optionEnv:no-foo', optionEnvEventMock);
process.env.BAR = 'anything';
program.addOption(new commander.Option('-F, --no-foo').env('BAR'));
program.parse([], { from: 'user' });
expect(optionEnvEventMock).toHaveBeenCalledWith();
});
});