/
cli.js
executable file
·620 lines (529 loc) · 18 KB
/
cli.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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
'use strict';
const Fs = require('fs');
const Path = require('path');
const Bossy = require('@hapi/bossy');
const FindRc = require('find-rc');
const Mo = require('mo-walk');
const Hoek = require('@hapi/hoek');
const Modules = require('./modules');
const Pkg = require('../package.json');
const Runner = require('./runner');
// .labrc configuration will be required if it exists
// index.js required below if setting assertions module
const internals = {};
internals.rcPath = FindRc('lab');
internals.rc = internals.rcPath ? require(internals.rcPath) : {};
exports.run = async function () {
const settings = internals.options();
settings.coveragePath = Path.join(process.cwd(), settings['coverage-path'] || '');
settings.coverageExclude = ['node_modules', 'test', 'test_runner'];
if (settings['coverage-exclude']) {
settings.coverageExclude = settings.coverageExclude.concat(settings['coverage-exclude']);
}
settings.lintingPath = process.cwd();
if (settings.coverage) {
await Modules.coverage.instrument(settings);
}
else if (settings.transform) {
Modules.transform.install(settings);
}
if (settings.environment) {
process.env.NODE_ENV = settings.environment;
}
if (settings.sourcemaps) {
let sourceMapOptions = {};
if (settings.transform) {
sourceMapOptions = {
retrieveFile: Modules.transform.retrieveFile
};
}
require('source-map-support').install(sourceMapOptions);
}
const scripts = await internals.traverse(settings.paths, settings);
const isNotCjs = (script) => script._type !== 'cjs';
if (settings.coverage && scripts.some(isNotCjs)) {
console.error('Cannot use code coverage with ES modules. Consider using c8: instructions can be found in lab\'s docs.');
process.exit(1);
}
if (settings.typescript && scripts.some(isNotCjs)) {
console.error('Cannot use typescript with ES modules.');
process.exit(1);
}
if (settings.transform && scripts.some(isNotCjs)) {
console.error('Cannot use transform with ES modules.');
process.exit(1);
}
return Runner.report(scripts, settings);
};
internals.traverse = async function (paths, options) {
let nextPath = null;
const traverse = function (path, defaultToESM) {
let files = [];
nextPath = path;
const pathStat = Fs.statSync(path);
if (pathStat.isFile()) {
return [[path, defaultToESM]];
}
Fs.readdirSync(path).forEach((filename) => {
nextPath = Path.join(path, filename);
const stat = Fs.statSync(nextPath);
if (stat.isDirectory() &&
!options.flat) {
files = files.concat(traverse(nextPath, defaultToESM));
return;
}
if (stat.isFile() &&
options.pattern.test(filename) &&
Path.basename(nextPath)[0] !== '.') {
files.push([nextPath, defaultToESM]);
}
});
return files;
};
let testFiles = [];
try {
for (const path of paths) {
const defaultToESM = await Mo.getDefaultToESM(path);
testFiles = testFiles.concat(traverse(path, defaultToESM));
}
}
catch (err) {
if (err.code !== 'ENOENT') {
throw err;
}
console.error('Could not find test file or directory \'' + nextPath + '\'.');
process.exit(1);
}
if (options.pattern &&
!testFiles.length) {
console.log('The pattern provided (-P or --pattern) didn\'t match any files.');
process.exit(0);
}
const scripts = [];
for (const [unresolvedFile, defaultToESM] of testFiles) {
global._labScriptRun = false;
const file = Path.resolve(unresolvedFile);
let pkg;
let type;
try {
const resolved = await Mo.tryToResolve(file, { defaultToESM });
Hoek.assert(resolved, 'File missing');
[pkg, , type] = resolved;
}
catch (ex) {
console.error(`Error requiring file: ${file}`);
console.error(`${ex.message}`);
console.error(`${ex.stack}`);
return process.exit(1);
}
if (pkg.lab?._root) {
scripts.push(Object.assign(pkg.lab, {
_type: type,
_executionScheduled: true
}));
if (pkg.lab._cli) {
Object.assign(options, pkg.lab._cli);
}
}
else if (global._labScriptRun) {
console.error(`The file: ${file} includes a lab script that is not exported via exports.lab`);
return process.exit(1);
}
}
return scripts;
};
internals.options = function () {
const definition = {
assert: {
alias: 'a',
type: 'string',
description: 'specify an assertion library module path to require and make available under Lab.assertions',
default: null
},
bail: {
type: 'boolean',
description: 'exit the process with a non zero exit code on the first test failure',
default: null
},
colors: {
alias: 'C',
type: 'boolean',
description: 'enable color output (defaults to terminal capabilities)',
default: null
},
'context-timeout': {
alias: 'M',
type: 'number',
description: 'timeout for before, after, beforeEach, afterEach in milliseconds',
default: null
},
coverage: {
alias: 'c',
type: 'boolean',
description: 'enable code coverage analysis',
default: null
},
'coverage-all': {
type: 'boolean',
description: 'include all files in coveragePath in report',
default: null
},
'coverage-exclude': {
type: 'string',
description: 'set code coverage excludes',
multiple: true,
default: null
},
'coverage-flat': {
type: 'boolean',
description: 'prevent recursive inclusion of all files in coveragePath in report',
default: null
},
'coverage-path': {
type: 'string',
description: 'set code coverage path',
default: null
},
'coverage-pattern': {
type: 'string',
description: 'file pattern to use for locating files for coverage',
default: null
},
'coverage-predicates': {
type: 'json',
parsePrimitives: 'strict',
description: 'set code coverage predicates'
},
'default-plan-threshold': {
alias: 'p',
type: 'number',
description: 'minimum plan threshold to apply to all tests that don\'t define any plan',
default: null
},
dry: {
alias: 'd',
type: 'boolean',
description: 'skip all tests (dry run)',
default: null
},
environment: {
alias: 'e',
type: 'string',
description: 'value to set NODE_ENV before tests',
default: null
},
flat: {
alias: 'f',
type: 'boolean',
description: 'prevent recursive collection of tests within the provided path',
default: null
},
globals: {
alias: ['I', 'ignore'],
type: 'string',
description: 'ignore a list of globals for the leak detection (comma separated)',
default: null
},
grep: {
alias: 'g',
type: 'string',
description: 'only run tests matching the given pattern which is internally compiled to a RegExp',
default: null
},
help: {
alias: 'h',
type: 'boolean',
description: 'display usage options',
default: null
},
id: {
alias: 'i',
type: 'range',
description: 'test identifier',
default: null
},
inspect: {
type: 'boolean',
description: 'starts lab with the node.js native debugger',
default: null
},
leaks: {
alias: 'l',
type: 'boolean',
description: 'disable global variable leaks detection',
default: null
},
lint: {
alias: 'L',
type: 'boolean',
description: 'enable linting',
default: null
},
linter: {
alias: 'n',
type: 'string',
description: 'linter path to use',
default: null
},
'lint-fix': {
type: 'boolean',
description: 'apply any fixes from the linter.',
default: null
},
'lint-options': {
type: 'string',
description: 'specify options to pass to linting program. It must be a string that is JSON.parse(able).',
default: null
},
'lint-errors-threshold': {
type: 'number',
description: 'linter errors threshold in absolute value',
default: null
},
'lint-warnings-threshold': {
type: 'number',
description: 'linter warnings threshold in absolute value',
default: null
},
output: {
alias: 'o',
type: 'string',
description: 'file path to write test results',
multiple: true,
default: null
},
pattern: {
alias: 'P',
type: 'string',
description: 'file pattern to use for locating tests',
default: null
},
reporter: {
alias: 'r',
type: 'string',
description: 'reporter type [console, html, json, tap, lcov, clover, junit]',
multiple: true,
default: null
},
require: {
alias: 'req',
multiple: true,
type: 'string',
description: 'dependency module to require and run before tests run (useful for things like babel, tsconfig-paths, etc)',
default: null
},
retries: {
alias: 'R',
type: 'number',
description: 'number of times to retry tests explicitly marked for retry',
default: null
},
seed: {
type: 'string',
description: 'use this seed to randomize the order with `--shuffle`. This is useful to debug order dependent test failures',
default: null
},
shuffle: {
type: 'boolean',
description: 'shuffle script execution order',
default: null
},
silence: {
alias: 's',
type: 'boolean',
description: 'silence test output',
default: null
},
'silent-skips': {
alias: 'k',
type: 'boolean',
description: 'don’t output skipped tests',
default: null
},
sourcemaps: {
alias: ['S', 'sourcemaps'],
type: 'boolean',
description: 'enable support for sourcemaps',
default: null
},
threshold: {
alias: 't',
type: 'number',
description: 'code coverage threshold percentage',
default: null
},
timeout: {
alias: 'm',
type: 'number',
description: 'timeout for each test in milliseconds',
default: null
},
transform: {
alias: ['T', 'transform'],
type: 'string',
description: 'javascript file that exports an array of objects ie. [ { ext: ".js", transform: function (content, filename) { ... } } ]',
default: null
},
types: {
alias: ['Y', 'types'],
type: 'boolean',
description: 'test types definitions',
default: null
},
'types-test': {
type: 'string',
description: 'location of types definitions test file',
default: null
},
typescript: {
type: 'boolean',
description: 'Enables TypeScript support',
default: null
},
verbose: {
alias: 'v',
type: 'boolean',
description: 'verbose test output',
default: null
},
version: {
alias: 'V',
type: 'boolean',
description: 'version information',
default: null
}
};
const defaults = {
bail: false,
coverage: false,
dry: false,
environment: 'test',
flat: false,
leaks: true,
lint: false,
linter: 'eslint',
'lint-fix': false,
'lint-errors-threshold': 0,
'lint-warnings-threshold': 0,
paths: ['test'],
reporter: 'console',
require: [],
retries: 5,
shuffle: false,
silence: false,
'silent-skips': false,
sourcemaps: false,
'context-timeout': 0,
timeout: 2000,
verbose: false
};
const argv = Bossy.parse(definition);
if (argv instanceof Error) {
console.error(Bossy.usage(definition, 'lab [options] [path]'));
console.error('\n' + argv.message);
process.exit(1);
}
if (argv.help) {
console.log(Bossy.usage(definition, 'lab [options] [path]'));
process.exit(0);
}
if (argv.version) {
console.log(Pkg.version);
process.exit(0);
}
const options = Object.assign({}, defaults, internals.rc);
options.paths = argv._ ? [].concat(argv._) : options.paths;
const keys = ['assert', 'bail', 'colors', 'context-timeout', 'coverage', 'coverage-exclude',
'coverage-path', 'coverage-all', 'coverage-flat', 'coverage-pattern', 'coverage-predicates',
'default-plan-threshold', 'dry', 'environment', 'flat', 'globals', 'grep', 'lint',
'lint-errors-threshold', 'lint-fix', 'lint-options', 'lint-warnings-threshold', 'linter',
'output', 'pattern', 'reporter', 'require', 'retries', 'seed', 'shuffle', 'silence', 'silent-skips',
'sourcemaps', 'threshold', 'timeout', 'transform', 'types', 'types-test', 'typescript', 'verbose'];
for (let i = 0; i < keys.length; ++i) {
if (argv.hasOwnProperty(keys[i]) && argv[keys[i]] !== undefined && argv[keys[i]] !== null) {
options[keys[i]] = argv[keys[i]];
}
}
if (typeof argv.leaks === 'boolean') {
options.leaks = !argv.leaks;
}
if (argv.id) {
options.ids = argv.id;
}
if (Array.isArray(options.reporter) &&
options.output) {
if (!Array.isArray(options.output) ||
options.output.length !== options.reporter.length) {
console.error(Bossy.usage(definition, 'lab [options] [path]'));
process.exit(1);
}
}
if (!options.output) {
options.output = process.stdout;
}
if (options.assert) {
options.assert = require(options.assert);
require('./').assertions = options.assert;
}
if (options.globals) {
options.globals = options.globals.trim().split(',');
}
if (options.silence) {
options.progress = 0;
}
else if (options.verbose) {
options.progress = 2;
}
if (options['types-test']) {
options.types = true;
}
const pattern = options.pattern ? '.*' + options.pattern + '.*?' : '';
if (options.typescript) {
if (options.transform) {
console.error('Cannot use "typescript" with "transform"');
process.exit(1);
}
if (options.types) {
console.error('Cannot use "typescript" with "types"');
process.exit(1);
}
options.transform = Modules.typescript.extensions;
options.sourcemaps = true;
}
if (options.require) {
for (let i = 0; i < options.require.length; ++i) {
const moduleName = options.require[i];
const resolved = require.resolve(
moduleName,
{ paths: [process.cwd()] }
);
if (!resolved) {
console.error(`Could not resolve module used with --require option: ${options.deps[i]}`);
}
require(resolved);
}
}
let exts = '\\.(js|mjs|cjs)$';
if (options.transform) {
const transform = typeof options.transform === 'string' ? require(Path.resolve(options.transform)) : options.transform;
Hoek.assert(Array.isArray(transform) && transform.length > 0, 'transform module must export an array of objects {ext: ".js", transform: null or function (content, filename)}');
options.transform = transform;
const includes = 'js|mjs|cjs|' + transform.map(internals.mapTransform).join('|');
exts = '\\.(' + includes + ')$';
}
options.pattern = new RegExp(pattern + exts);
options.coverage = (options.coverage || options.threshold > 0 || options['coverage-all'] || options.reporter.indexOf('html') !== -1 || options.reporter.indexOf('lcov') !== -1 || options.reporter.indexOf('clover') !== -1);
options.coveragePattern = new RegExp(options['coverage-pattern'] || pattern + exts);
if (options['coverage-pattern'] && !options['coverage-all']) {
console.error('The "coverage-pattern" option can only be used with "coverage-all"');
process.exit(1);
}
if (options['coverage-flat'] && !options['coverage-all']) {
console.error('The "coverage-flat" option can only be used with "coverage-all"');
process.exit(1);
}
return options;
};
internals.mapTransform = function (transform) {
return transform.ext.substr(1).replace(/[\^\$\.\*\+\-\?\=\!\:\|\\\/\(\)\[\]\{\}\,]/g, '\\$&');
};