-
Notifications
You must be signed in to change notification settings - Fork 766
/
parse-flags.spec.ts
465 lines (404 loc) · 17.3 KB
/
parse-flags.spec.ts
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import { toDashCase } from '@utils';
import { LogLevel } from '../../declarations';
import {
BOOLEAN_CLI_FLAGS,
BOOLEAN_STRING_CLI_FLAGS,
BooleanStringCLIFlag,
ConfigFlags,
NUMBER_CLI_FLAGS,
STRING_ARRAY_CLI_FLAGS,
STRING_CLI_FLAGS,
StringArrayCLIFlag,
} from '../config-flags';
import { Empty, parseEqualsArg, parseFlags } from '../parse-flags';
describe('parseFlags', () => {
it('should get known and unknown args', () => {
const args = ['serve', '--address', '127.0.0.1', '--potatoArgument', '--flimflammery', 'test.spec.ts'];
const flags = parseFlags(args);
expect(flags.task).toBe('serve');
expect(flags.args[0]).toBe('--address');
expect(flags.args[1]).toBe('127.0.0.1');
expect(flags.args[2]).toBe('--potatoArgument');
expect(flags.args[3]).toBe('--flimflammery');
expect(flags.args[4]).toBe('test.spec.ts');
expect(flags.knownArgs).toEqual(['--address', '127.0.0.1']);
expect(flags.unknownArgs[0]).toBe('--potatoArgument');
expect(flags.unknownArgs[1]).toBe('--flimflammery');
expect(flags.unknownArgs[2]).toBe('test.spec.ts');
});
it('should parse cli for dev server', () => {
// user command line args
// $ npm run serve --port 4444
// args.slice(2)
// [ 'serve', '--address', '127.0.0.1', '--port', '4444' ]
const args = ['serve', '--address', '127.0.0.1', '--port', '4444'];
const flags = parseFlags(args);
expect(flags.task).toBe('serve');
expect(flags.address).toBe('127.0.0.1');
expect(flags.port).toBe(4444);
expect(flags.knownArgs).toEqual(['--address', '127.0.0.1', '--port', '4444']);
});
it('should parse task', () => {
const flags = parseFlags(['build']);
expect(flags.task).toBe('build');
});
it('should parse no task', () => {
const flags = parseFlags(['--flag']);
expect(flags.task).toBe(null);
});
/**
* these comprehensive tests of all the supported boolean args serve as
* regression tests against duplicating any of the arguments in the arrays.
* Because of the way that the arg parsing algorithm works having a dupe
* will result in a value like `[true, true]` being set on ConfigFlags, which
* will cause these tests to start failing.
*/
describe.each(BOOLEAN_CLI_FLAGS)('should parse boolean flag %s', (cliArg) => {
it('should parse arg', () => {
const flags = parseFlags([`--${cliArg}`]);
expect(flags.knownArgs).toEqual([`--${cliArg}`]);
expect(flags[cliArg]).toBe(true);
});
it(`should parse --no${cliArg}`, () => {
const negativeFlag = '--no' + cliArg.charAt(0).toUpperCase() + cliArg.slice(1);
const flags = parseFlags([negativeFlag]);
expect(flags.knownArgs).toEqual([negativeFlag]);
expect(flags[cliArg]).toBe(false);
});
it(`should override --${cliArg} with --no${cliArg}`, () => {
const negativeFlag = '--no' + cliArg.charAt(0).toUpperCase() + cliArg.slice(1);
const flags = parseFlags([`--${cliArg}`, negativeFlag]);
expect(flags.knownArgs).toEqual([`--${cliArg}`, negativeFlag]);
expect(flags[cliArg]).toBe(false);
});
it('should not set value if not present', () => {
const flags = parseFlags([]);
expect(flags.knownArgs).toEqual([]);
expect(flags[cliArg]).toBe(undefined);
});
});
describe.each(STRING_CLI_FLAGS)('should parse string flag %s', (cliArg) => {
it(`should parse "--${cliArg} value"`, () => {
const flags = parseFlags([`--${cliArg}`, 'test-value']);
expect(flags.knownArgs).toEqual([`--${cliArg}`, 'test-value']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toBe('test-value');
});
it(`should parse "--${cliArg}=value"`, () => {
const flags = parseFlags([`--${cliArg}=path/to/file.js`]);
expect(flags.knownArgs).toEqual([`--${cliArg}`, 'path/to/file.js']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toBe('path/to/file.js');
});
it(`should parse "--${toDashCase(cliArg)} value"`, () => {
const flags = parseFlags([`--${toDashCase(cliArg)}`, 'path/to/file.js']);
expect(flags.knownArgs).toEqual([`--${toDashCase(cliArg)}`, 'path/to/file.js']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toBe('path/to/file.js');
});
it(`should parse "--${toDashCase(cliArg)}=value"`, () => {
const flags = parseFlags([`--${toDashCase(cliArg)}=path/to/file.js`]);
expect(flags.knownArgs).toEqual([`--${toDashCase(cliArg)}`, 'path/to/file.js']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toBe('path/to/file.js');
});
});
it.each(NUMBER_CLI_FLAGS)('should parse number flag %s', (cliArg) => {
const flags = parseFlags([`--${cliArg}`, '42']);
expect(flags.knownArgs).toEqual([`--${cliArg}`, '42']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toBe(42);
});
it('should override --config with second --config', () => {
const args = ['--config', '/config-1.js', '--config', '/config-2.js'];
const flags = parseFlags(args);
expect(flags.config).toBe('/config-2.js');
});
describe.each(BOOLEAN_STRING_CLI_FLAGS)('boolean-string flag - %s', (cliArg: BooleanStringCLIFlag) => {
it('parses a boolean-string flag as a boolean with no arg', () => {
const args = [`--${cliArg}`];
const flags = parseFlags(args);
expect(flags.headless).toBe(true);
expect(flags.knownArgs).toEqual([`--${cliArg}`]);
});
it(`parses a boolean-string flag as a falsy boolean with "no" arg - --no-${cliArg}`, () => {
const args = [`--no-${cliArg}`];
const flags = parseFlags(args);
expect(flags.headless).toBe(false);
expect(flags.knownArgs).toEqual([`--no-${cliArg}`]);
});
it(`parses a boolean-string flag as a falsy boolean with "no" arg - --no${
cliArg.charAt(0).toUpperCase() + cliArg.slice(1)
}`, () => {
const negativeFlag = '--no' + cliArg.charAt(0).toUpperCase() + cliArg.slice(1);
const flags = parseFlags([negativeFlag]);
expect(flags.headless).toBe(false);
expect(flags.knownArgs).toEqual([negativeFlag]);
});
it('parses a boolean-string flag as a string with a string arg', () => {
const args = [`--${cliArg}`, 'new'];
const flags = parseFlags(args);
expect(flags.headless).toBe('new');
expect(flags.knownArgs).toEqual(['--headless', 'new']);
});
it('parses a boolean-string flag as a string with a string arg using equality', () => {
const args = [`--${cliArg}=new`];
const flags = parseFlags(args);
expect(flags.headless).toBe('new');
expect(flags.knownArgs).toEqual([`--${cliArg}`, 'new']);
});
});
describe.each<LogLevel>(['info', 'warn', 'error', 'debug'])('logLevel %s', (level) => {
it("should parse '--logLevel %s'", () => {
const args = ['--logLevel', level];
const flags = parseFlags(args);
expect(flags.logLevel).toBe(level);
});
it('should parse --logLevel=%s', () => {
const args = [`--logLevel=${level}`];
const flags = parseFlags(args);
expect(flags.logLevel).toBe(level);
});
it("should parse '--log-level %s'", () => {
const flags = parseFlags(['--log-level', level]);
expect(flags.logLevel).toBe(level);
});
it('should parse --log-level=%s', () => {
const flags = parseFlags([`--log-level=${level}`]);
expect(flags.logLevel).toBe(level);
});
});
/**
* maxWorkers is (as of this writing) our only StringNumberCLIArg, meaning it
* may be a string (like "50%") or a number (like 4). For this reason we have
* some tests just for it.
*/
describe('maxWorkers', () => {
it.each([
['--maxWorkers', '4'],
['--maxWorkers=4'],
['--max-workers', '4'],
['--maxWorkers', '4e+0'],
['--maxWorkers', '40e-1'],
])('should parse %p, %p', (...args) => {
const flags = parseFlags(args);
expect(flags.maxWorkers).toBe(4);
});
it('should parse --maxWorkers 4', () => {
const flags = parseFlags(['--maxWorkers', '4']);
expect(flags.maxWorkers).toBe(4);
});
it('should parse --maxWorkers=4', () => {
const flags = parseFlags(['--maxWorkers=4']);
expect(flags.maxWorkers).toBe(4);
});
it('should parse --max-workers 4', () => {
const flags = parseFlags(['--max-workers', '4']);
expect(flags.maxWorkers).toBe(4);
});
it('should parse --maxWorkers=50%', function () {
// see https://jestjs.io/docs/27.x/cli#--maxworkersnumstring
const flags = parseFlags(['--maxWorkers=50%']);
expect(flags.maxWorkers).toBe('50%');
});
it('should parse --max-workers=1', () => {
const flags = parseFlags(['--max-workers=1']);
expect(flags.maxWorkers).toBe(1);
});
it('should not parse --max-workers', () => {
const flags = parseFlags([]);
expect(flags.maxWorkers).toBe(undefined);
});
});
describe('aliases', () => {
describe('-p (alias for port)', () => {
it('should parse -p=4444', () => {
const flags = parseFlags(['-p=4444']);
expect(flags.port).toBe(4444);
});
it('should parse -p 4444', () => {
const flags = parseFlags(['-p', '4444']);
expect(flags.port).toBe(4444);
});
});
it('should parse -h (alias for help)', () => {
const flags = parseFlags(['-h']);
expect(flags.help).toBe(true);
});
it('should parse -v (alias for version)', () => {
const flags = parseFlags(['-v']);
expect(flags.version).toBe(true);
});
describe('-c alias for config', () => {
it('should parse -c /my-config.js', () => {
const flags = parseFlags(['-c', '/my-config.js']);
expect(flags.config).toBe('/my-config.js');
expect(flags.knownArgs).toEqual(['--config', '/my-config.js']);
});
it('should parse -c=/my-config.js', () => {
const flags = parseFlags(['-c=/my-config.js']);
expect(flags.config).toBe('/my-config.js');
expect(flags.knownArgs).toEqual(['--config', '/my-config.js']);
});
});
describe('Jest aliases', () => {
it.each([
['w', 'maxWorkers', '4'],
['t', 'testNamePattern', 'testname'],
])('should support the string Jest alias %p for %p', (alias, fullArgument, value) => {
const flags = parseFlags([`-${alias}`, value]);
expect(flags.knownArgs).toEqual([`--${fullArgument}`, value]);
expect(flags.unknownArgs).toHaveLength(0);
});
it.each([
['w', 'maxWorkers', '4'],
['t', 'testNamePattern', 'testname'],
])('should support the string Jest alias %p for %p in an AliasEqualsArg', (alias, fullArgument, value) => {
const flags = parseFlags([`-${alias}=${value}`]);
expect(flags.knownArgs).toEqual([`--${fullArgument}`, value]);
expect(flags.unknownArgs).toHaveLength(0);
});
it.each<[string, keyof ConfigFlags]>([
['b', 'bail'],
['e', 'expand'],
['o', 'onlyChanged'],
['f', 'onlyFailures'],
['i', 'runInBand'],
['u', 'updateSnapshot'],
])('should support the boolean Jest alias %p for %p', (alias, fullArgument) => {
const flags = parseFlags([`-${alias}`]);
expect(flags.knownArgs).toEqual([`--${fullArgument}`]);
expect(flags[fullArgument]).toBe(true);
expect(flags.unknownArgs).toHaveLength(0);
});
});
});
it('should parse many', () => {
const args = ['-v', '--help', '-c=./myconfig.json'];
const flags = parseFlags(args);
expect(flags.version).toBe(true);
expect(flags.help).toBe(true);
expect(flags.config).toBe('./myconfig.json');
});
describe('parseEqualsArg', () => {
it.each([
['--fooBar=baz', '--fooBar', 'baz'],
['--foo-bar=4', '--foo-bar', '4'],
['--fooBar=twenty=3*4', '--fooBar', 'twenty=3*4'],
['--fooBar', '--fooBar', Empty],
['--foo-bar', '--foo-bar', Empty],
['--foo-bar=""', '--foo-bar', '""'],
])('should parse %s correctly', (testArg, expectedArg, expectedValue) => {
const [arg, value] = parseEqualsArg(testArg);
expect(arg).toBe(expectedArg);
expect(value).toEqual(expectedValue);
});
});
describe.each(STRING_ARRAY_CLI_FLAGS)('should parse string flag %s', (cliArg: StringArrayCLIFlag) => {
it(`should parse single value: "--${cliArg} test-value"`, () => {
const flags = parseFlags([`--${cliArg}`, 'test-value']);
expect(flags.knownArgs).toEqual([`--${cliArg}`, 'test-value']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toEqual(['test-value']);
});
it(`should parse multiple values: "--${cliArg} test-value"`, () => {
const flags = parseFlags([`--${cliArg}`, 'test-value', `--${cliArg}`, 'second-test-value']);
expect(flags.knownArgs).toEqual([`--${cliArg}`, 'test-value', `--${cliArg}`, 'second-test-value']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toEqual(['test-value', 'second-test-value']);
});
it(`should parse "--${cliArg}=value"`, () => {
const flags = parseFlags([`--${cliArg}=path/to/file.js`]);
expect(flags.knownArgs).toEqual([`--${cliArg}`, 'path/to/file.js']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toEqual(['path/to/file.js']);
});
it(`should parse multiple values: "--${cliArg}=test-value"`, () => {
const flags = parseFlags([`--${cliArg}=test-value`, `--${cliArg}=second-test-value`]);
expect(flags.knownArgs).toEqual([`--${cliArg}`, 'test-value', `--${cliArg}`, 'second-test-value']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toEqual(['test-value', 'second-test-value']);
});
it(`should parse "--${toDashCase(cliArg)} value"`, () => {
const flags = parseFlags([`--${toDashCase(cliArg)}`, 'path/to/file.js']);
expect(flags.knownArgs).toEqual([`--${toDashCase(cliArg)}`, 'path/to/file.js']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toEqual(['path/to/file.js']);
});
it(`should parse multiple values: "--${toDashCase(cliArg)} test-value"`, () => {
const flags = parseFlags([
`--${toDashCase(cliArg)}`,
'test-value',
`--${toDashCase(cliArg)}`,
'second-test-value',
]);
expect(flags.knownArgs).toEqual([
`--${toDashCase(cliArg)}`,
'test-value',
`--${toDashCase(cliArg)}`,
'second-test-value',
]);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toEqual(['test-value', 'second-test-value']);
});
it(`should parse "--${toDashCase(cliArg)}=value"`, () => {
const flags = parseFlags([`--${toDashCase(cliArg)}=path/to/file.js`]);
expect(flags.knownArgs).toEqual([`--${toDashCase(cliArg)}`, 'path/to/file.js']);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toEqual(['path/to/file.js']);
});
it(`should parse multiple values: "--${toDashCase(cliArg)}=test-value"`, () => {
const flags = parseFlags([`--${toDashCase(cliArg)}=test-value`, `--${toDashCase(cliArg)}=second-test-value`]);
expect(flags.knownArgs).toEqual([
`--${toDashCase(cliArg)}`,
'test-value',
`--${toDashCase(cliArg)}`,
'second-test-value',
]);
expect(flags.unknownArgs).toEqual([]);
expect(flags[cliArg]).toEqual(['test-value', 'second-test-value']);
});
});
describe('error reporting', () => {
it('should throw if you pass no argument to a string flag', () => {
expect(() => {
parseFlags(['--cacheDirectory', '--someOtherFlag']);
}).toThrow('when parsing CLI flag "--cacheDirectory": expected a string argument but received nothing');
});
it('should throw if you pass no argument to a string array flag', () => {
expect(() => {
parseFlags(['--reporters', '--someOtherFlag']);
}).toThrow('when parsing CLI flag "--reporters": expected a string argument but received nothing');
});
it('should throw if you pass no argument to a number flag', () => {
expect(() => {
parseFlags(['--port', '--someOtherFlag']);
}).toThrow('when parsing CLI flag "--port": expected a number argument but received nothing');
});
it('should throw if you pass a non-number argument to a number flag', () => {
expect(() => {
parseFlags(['--port', 'stringy']);
}).toThrow('when parsing CLI flag "--port": expected a number but received "stringy"');
});
it('should throw if you pass a bad number argument to a number flag', () => {
expect(() => {
parseFlags(['--port=NaN']);
}).toThrow('when parsing CLI flag "--port": expected a number but received "NaN"');
});
it('should throw if you pass no argument to a string/number flag', () => {
expect(() => {
parseFlags(['--maxWorkers']);
}).toThrow('when parsing CLI flag "--maxWorkers": expected a string or a number but received nothing');
});
it('should throw if you pass an invalid log level for --logLevel', () => {
expect(() => {
parseFlags(['--logLevel', 'potato']);
}).toThrow('when parsing CLI flag "--logLevel": expected to receive a valid log level but received "potato"');
});
it('should throw if you pass no argument to --logLevel', () => {
expect(() => {
parseFlags(['--logLevel']);
}).toThrow('when parsing CLI flag "--logLevel": expected to receive a valid log level but received nothing');
});
});
});