-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
run.js
executable file
·783 lines (648 loc) · 19.7 KB
/
run.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#!/usr/bin/env node
'use strict'
const node = process.execPath
const fs = require('fs')
const spawn = require('child_process').spawn
const fg = require('foreground-child')
const opener = require('opener')
const colorSupport = require('color-support')
const nycBin = require.resolve('nyc/bin/nyc.js')
const glob = require('glob')
const isexe = require('isexe')
const osHomedir = require('os-homedir')
const yaml = require('js-yaml')
const path = require('path')
const exists = require('fs-exists-cached').sync
const os = require('os')
const isTTY = process.stdin.isTTY || process.env._TAP_IS_TTY === '1'
const coverageServiceTest = process.env.COVERAGE_SERVICE_TEST === 'true'
// NYC will not wrap a module in node_modules.
// So, we need to tell the child proc when it's been added.
// Of course, this can't reasonably be branch-covered, so ignore it.
/* istanbul ignore next */
if (process.env._TAP_COVERAGE_ === '1')
global.__coverage__ = global.__coverage__ || {}
else if (process.env._TAP_COVERAGE_ === '0') {
global.__coverage__ = null
Object.keys(process.env).filter(k => /NYC/.test(k)).forEach(k =>
process.env[k] = '')
}
/* istanbul ignore next */
if (coverageServiceTest)
console.log('COVERAGE_SERVICE_TEST')
// Add new coverage services here.
// it'll check for the environ named and pipe appropriately.
//
// Currently only Coveralls is supported, but the infrastructure
// is left in place in case some noble soul fixes the codecov
// module in the future. See https://github.com/tapjs/node-tap/issues/270
const coverageServices = [
{
env: 'COVERALLS_REPO_TOKEN',
bin: require.resolve('coveralls/bin/coveralls.js'),
name: 'Coveralls'
}
]
const main = _ => {
const args = process.argv.slice(2)
if (!args.length && isTTY) {
console.error(usage())
process.exit(1)
}
// set default args
const defaults = constructDefaultArgs()
// parse dotfile
const rcFile = process.env.TAP_RCFILE || (osHomedir() + '/.taprc')
const rcOptions = parseRcFile(rcFile)
// supplement defaults with parsed rc options
Object.keys(rcOptions).forEach(k =>
defaults[k] = rcOptions[k])
defaults.rcFile = rcFile
// parse args
const options = parseArgs(args, defaults)
if (!options)
return
process.stdout.on('error', er => {
/* istanbul ignore else */
if (er.code === 'EPIPE')
process.exit()
else
throw er
})
options.files = globFiles(options.files)
// this is only testable by escaping from the covered environment
/* istanbul ignore next */
if ((options.coverageReport || options.checkCoverage) &&
options.files.length === 0)
return runCoverageReport(options)
if (options.files.length === 0) {
console.error('Reading TAP data from stdin (use "-" argument to suppress)')
options.files.push('-')
}
if (options.files.length === 1 && options.files[0] === '-') {
if (options.coverage)
console.error('Coverage disabled because stdin cannot be instrumented')
setupTapEnv(options)
stdinOnly(options)
return
}
// By definition, the next block cannot be covered, because
// they are only relevant when coverage is turned off.
/* istanbul ignore next */
if (options.coverage && !global.__coverage__) {
return respawnWithCoverage(options)
}
setupTapEnv(options)
runTests(options)
}
const constructDefaultArgs = _ => {
/* istanbul ignore next */
const defaultTimeout = global.__coverage__ ? 240 : 30
const defaultArgs = {
nodeArgs: [],
nycArgs: [],
testArgs: [],
timeout: +process.env.TAP_TIMEOUT || defaultTimeout,
color: !!colorSupport.level,
reporter: null,
files: [],
grep: [],
grepInvert: false,
bail: false,
saveFile: null,
pipeToService: false,
coverageReport: null,
browser: true,
coverage: undefined,
checkCoverage: false,
branches: 0,
functions: 0,
lines: 0,
statements: 0,
jobs: 1,
outputFile: null
}
if (process.env.TAP_COLORS !== undefined)
defaultArgs.color = !!(+process.env.TAP_COLORS)
return defaultArgs
}
const parseArgs = (args, options) => {
const singleFlags = {
b: 'bail',
B: 'no-bail',
i: 'invert',
I: 'no-invert',
c: 'color',
C: 'no-color',
T: 'no-timeout',
J: 'jobs-auto',
O: 'only',
h: 'help',
'?': 'help',
v: 'version'
}
const singleOpts = {
j: 'jobs',
g: 'grep',
R: 'reporter',
t: 'timeout',
s: 'save',
o: 'output-file'
}
// If we're running under Travis-CI with a Coveralls.io token,
// then it's a safe bet that we ought to output coverage.
for (let i = 0; i < coverageServices.length && !options.pipeToService; i++) {
if (process.env[coverageServices[i].env])
options.pipeToService = true
}
let defaultCoverage = options.pipeToService
let dumpConfig = false
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg.charAt(0) !== '-' || arg === '-') {
options.files.push(arg)
continue
}
// short-flags
if (arg.charAt(1) !== '-' && arg !== '-gc') {
const expand = []
for (let f = 1; f < arg.length; f++) {
const fc = arg.charAt(f)
const sf = singleFlags[fc]
const so = singleOpts[fc]
if (sf)
expand.push('--' + sf)
else if (so) {
const soslice = arg.slice(f + 1)
const soval = soslice.charAt(0) === '=' ? soslice : '=' + soslice
expand.push('--' + so + soval)
f = arg.length
} else if (arg !== '-' + fc)
expand.push('-' + fc)
}
if (expand.length) {
args.splice.apply(args, [i, 1].concat(expand))
i--
continue
}
}
const argsplit = arg.split('=')
const key = argsplit.shift()
const val = argsplit.length ? argsplit.join('=') : null
switch (key) {
case '--help':
console.log(usage())
return null
case '--dump-config':
dumpConfig = true
continue
case '--nyc-help':
nycHelp()
return null
case '--nyc-version':
nycVersion()
return null
case '--version':
console.log(require('../package.json').version)
return null
case '--jobs':
options.jobs = +(val || args[++i])
continue
case '--jobs-auto':
options.jobs = +os.cpus().length
continue
case '--coverage-report':
options.coverageReport = val || args[++i]
if (options.coverageReport === 'html')
options.coverageReport = 'lcov'
defaultCoverage = true
continue
case '--no-browser':
options.browser = false
continue
case '--no-coverage-report':
options.coverageReport = false
continue
case '--no-cov': case '--no-coverage':
options.coverage = false
continue
case '--cov': case '--coverage':
options.coverage = true
continue
case '--save':
options.saveFile = val || args[++i]
continue
case '--reporter':
options.reporter = val || args[++i]
continue
case '--gc': case '-gc': case '--expose-gc':
options.nodeArgs.push('--expose-gc')
continue
case '--strict':
options.nodeArgs.push('--use_strict')
continue
case '--debug':
options.nodeArgs.push('--debug')
continue
case '--debug-brk':
options.nodeArgs.push('--debug-brk')
continue
case '--harmony':
options.nodeArgs.push('--harmony')
continue
case '--node-arg': {
const v = val || args[++i]
if (v !== undefined)
options.nodeArgs.push(v)
continue
}
case '--check-coverage':
defaultCoverage = true
options.checkCoverage = true
continue
case '--test-arg': {
const v = val || args[++i]
if (v !== undefined)
options.testArgs.push(v)
continue
}
case '--nyc-arg': {
const v = val || args[++i]
if (v !== undefined)
options.nycArgs.push(v)
continue
}
case '--100':
defaultCoverage = true
options.checkCoverage = true
options.branches = 100
options.functions = 100
options.lines = 100
options.statements = 100
continue
case '--branches':
case '--functions':
case '--lines':
case '--statements':
defaultCoverage = true
options.checkCoverage = true
options[key.slice(2)] = +(val || args[++i])
continue
case '--color':
options.color = true
continue
case '--no-color':
options.color = false
continue
case '--output-file': {
const v = val || args[++i]
if (v !== undefined)
options.outputFile = v
continue
}
case '--no-timeout':
options.timeout = 0
continue
case '--timeout':
options.timeout = +(val || args[++i])
continue
case '--invert':
options.grepInvert = true
continue
case '--no-invert':
options.grepInvert = false
continue
case '--grep': {
const v = val || args[++i]
if (v !== undefined)
options.grep.push(strToRegExp(v))
continue
}
case '--bail':
options.bail = true
continue
case '--no-bail':
options.bail = false
continue
case '--only':
options.only = true
continue
case '--':
options.files = options.files.concat(args.slice(i + 1))
i = args.length
continue
default:
throw new Error('Unknown argument: ' + arg)
}
}
if (options.coverage === undefined)
options.coverage = defaultCoverage
if (process.env.TAP === '1')
options.reporter = 'tap'
// default to tap for non-tty envs
if (!options.reporter)
options.reporter = options.color ? 'classic' : 'tap'
if (dumpConfig)
return console.log(JSON.stringify(options, null, 2))
return options
}
// Obviously, this bit isn't going to ever be covered, because
// it only runs when we DON'T have coverage enabled, to enable it.
/* istanbul ignore next */
const respawnWithCoverage = options => {
// Re-spawn with coverage
const args = [nycBin].concat(
'--silent',
'--cache=true',
options.nycArgs,
'--',
process.execArgv,
process.argv.slice(1)
)
process.env._TAP_COVERAGE_ = '1'
const child = fg(node, args)
child.removeAllListeners('close')
child.on('close', (code, signal) =>
runCoverageReport(options, code, signal))
}
/* istanbul ignore next */
const pipeToCoverageServices = (options, child) => {
let piped = false
for (let i = 0; i < coverageServices.length; i++) {
if (process.env[coverageServices[i].env]) {
pipeToCoverageService(coverageServices[i], options, child)
piped = true
}
}
if (!piped)
throw new Error('unknown service, internal error')
}
/* istanbul ignore next */
const pipeToCoverageService = (service, options, child) => {
let bin = service.bin
if (coverageServiceTest) {
// test scaffolding.
// don't actually send stuff to the service
bin = require.resolve('../test/fixtures/cat.js')
console.log('%s:%s', service.name, process.env[service.env])
}
const ca = spawn(node, [bin], {
stdio: [ 'pipe', 1, 2 ]
})
child.stdout.pipe(ca.stdin)
ca.on('close', (code, signal) =>
signal ? process.kill(process.pid, signal)
: code ? console.log('Error piping coverage to ' + service.name)
: console.log('Successfully piped to ' + service.name))
}
/* istanbul ignore next */
const runCoverageReport = (options, code, signal) => {
if (options.checkCoverage)
runCoverageCheck(options, code, signal)
else
runCoverageReportOnly(options, code, signal)
}
/* istanbul ignore next */
const runCoverageReportOnly = (options, code, signal) => {
const close = (s, c) => {
if (signal || s) {
setTimeout(() => {}, 200)
process.kill(process.pid, signal || s)
} else if (code || c)
process.exit(code || c)
}
if (options.coverageReport === false)
return close(code, signal)
if (!options.coverageReport) {
if (options.pipeToService || coverageServiceTest)
options.coverageReport = 'text-lcov'
else
options.coverageReport = 'text'
}
const args = [nycBin, 'report', '--reporter', options.coverageReport]
let child
// automatically hook into coveralls
if (options.coverageReport === 'text-lcov' && options.pipeToService) {
child = spawn(node, args, { stdio: [ 0, 'pipe', 2 ] })
pipeToCoverageServices(options, child)
} else {
// otherwise just run the reporter
child = fg(node, args)
if (options.coverageReport === 'lcov' && options.browser)
child.on('exit', () =>
opener('coverage/lcov-report/index.html'))
}
if (code || signal) {
child.removeAllListeners('close')
child.on('close', close)
}
}
/* istanbul ignore next */
const coverageCheckArgs = options => {
const args = []
if (options.branches)
args.push('--branches', options.branches)
if (options.functions)
args.push('--functions', options.functions)
if (options.lines)
args.push('--lines', options.lines)
if (options.statements)
args.push('--statements', options.statements)
return args
}
/* istanbul ignore next */
const runCoverageCheck = (options, code, signal) => {
const args = [nycBin, 'check-coverage'].concat(coverageCheckArgs(options))
const child = fg(node, args)
child.removeAllListeners('close')
child.on('close', (c, s) =>
runCoverageReportOnly(options, code || c, signal || s))
}
const usage = _ => fs.readFileSync(__dirname + '/usage.txt', 'utf8')
.split('@@REPORTERS@@')
.join(getReporters())
const nycHelp = _ => fg(node, [nycBin, '--help'])
const nycVersion = _ => console.log(require('nyc/package.json').version)
const getReporters = _ => {
const types = require('tap-mocha-reporter').types.reduce((str, t) => {
const ll = str.split('\n').pop().length + t.length
if (ll < 40)
return str + ' ' + t
else
return str + '\n' + t
}, '').trim()
const ind = ' '
return ind + types.split('\n').join('\n' + ind)
}
const setupTapEnv = options => {
process.env.TAP_TIMEOUT = options.timeout
if (options.color)
process.env.TAP_COLORS = '1'
else
process.env.TAP_COLORS = '0'
if (options.bail)
process.env.TAP_BAIL = '1'
if (options.grepInvert)
process.env.TAP_GREP_INVERT = '1'
if (options.grep.length)
process.env.TAP_GREP = options.grep.map(p => p.toString())
.join('\n')
if (options.only)
process.env.TAP_ONLY = '1'
}
const globFiles = files => files.reduce((acc, f) =>
acc.concat(f === '-' ? f : glob.sync(f, { nonull: true })), [])
const makeReporter = options =>
new (require('tap-mocha-reporter'))(options.reporter)
const stdinOnly = options => {
// if we didn't specify any files, then just passthrough
// to the reporter, so we don't get '/dev/stdin' in the suite list.
// We have to pause() before piping to switch streams2 into old-mode
process.stdin.pause()
const reporter = makeReporter(options)
process.stdin.pipe(reporter)
if (options.outputFile !== null)
process.stdin.pipe(fs.createWriteStream(options.outputFile))
process.stdin.resume()
}
const readSaveFile = options => {
if (options.saveFile)
try {
return fs.readFileSync(options.saveFile, 'utf8').trim().split('\n')
} catch (er) {}
return null
}
const saveFails = (options, tap) => {
if (!options.saveFile)
return
let fails = []
const successes = []
tap.on('result', res => {
// we will continue to re-run todo tests, even though they're
// not technically "failures".
if (!res.ok && !res.extra.skip)
fails.push(res.extra.file)
else
successes.push(res.extra.file)
})
const save = () => {
fails = fails.reduce((set, f) => {
if (set.indexOf(f) === -1)
set.push(f)
return set
}, [])
if (!fails.length)
try {
fs.unlinkSync(options.saveFile)
} catch (er) {}
else
try {
fs.writeFileSync(options.saveFile, fails.join('\n') + '\n')
} catch (er) {}
}
tap.on('bailout', reason => {
// add any pending test files to the fails list.
fails.push.apply(fails, options.files.filter(file =>
successes.indexOf(file) === -1))
save()
})
tap.on('end', save)
}
const filterFiles = (files, saved, parallelOk) =>
files.filter(file => {
if (path.basename(file) === 'tap-parallel-ok')
parallelOk[path.resolve(path.dirname(file))] = true
else if (path.basename(file) === 'tap-parallel-not-ok')
parallelOk[path.resolve(path.dirname(file))] = false
else
return saved === null || onSavedList(saved, file)
})
// check if the file is on the list, or if it's a parent dir of
// any items that are on the list.
const onSavedList = (saved, file) =>
!saved || !saved.length ? true
: file === path.dirname(file) ? false
: saved.indexOf(file) !== -1 ? true
: onSavedList(saved.filter(
f => f !== path.dirname(f)).map(
f => path.dirname(f)
), file)
const isParallelOk = (parallelOk, file) => {
const dir = path.resolve(path.dirname(file))
return (dir in parallelOk) ? parallelOk[dir]
: exists(dir + '/tap-parallel-ok')
? parallelOk[dir] = true
: exists(dir + '/tap-parallel-not-ok')
? parallelOk[dir] = false
: dir.length >= process.cwd().length
? isParallelOk(parallelOk, dir)
: true
}
const runAllFiles = (options, saved, tap) => {
let doStdin = false
let parallelOk = Object.create(null)
options.files = filterFiles(options.files, saved, parallelOk)
for (let i = 0; i < options.files.length; i++) {
const opt = {}
const file = options.files[i]
// Pick up stdin after all the other files are handled.
if (file === '-') {
doStdin = true
continue
}
const st = fs.statSync(options.files[i])
if (options.timeout)
opt.timeout = options.timeout * 1000
opt.file = file
if (options.jobs > 1)
opt.buffered = isParallelOk(parallelOk, file) !== false
if (file.match(/\.js$/)) {
const args = options.nodeArgs.concat(file).concat(options.testArgs)
tap.spawn(node, args, opt, file)
} else if (st.isDirectory()) {
const dir = filterFiles(fs.readdirSync(file).map(f =>
file + '/' + f), saved, parallelOk)
options.files.push.apply(options.files, dir)
} else if (isexe.sync(options.files[i]))
tap.spawn(options.files[i], options.testArgs, opt, file)
}
if (doStdin)
tap.stdin()
}
const runTests = options => {
const saved = readSaveFile(options)
// At this point, we know we need to use the tap root,
// because there are 1 or more files to spawn.
const tap = require('../lib/tap.js')
tap.runOnly = false
// greps are passed to children, but not the runner itself
tap.grep = []
tap.jobs = options.jobs
tap.patchProcess()
// if not -Rtap, then output what the user wants.
// otherwise just dump to stdout
tap.pipe(options.reporter === 'tap' ? process.stdout: makeReporter(options))
// need to replay the first version line, because the previous
// line will have flushed it out to stdout or the reporter already.
if (options.outputFile !== null)
tap.pipe(fs.createWriteStream(options.outputFile)).write('TAP version 13\n')
saveFails(options, tap)
runAllFiles(options, saved, tap)
tap.end()
}
const parseRcFile = path => {
try {
const contents = fs.readFileSync(path, 'utf8')
return yaml.safeLoad(contents) || {}
} catch (er) {
// if no dotfile exists, or invalid yaml, fail gracefully
return {}
}
}
const strToRegExp = g => {
const p = g.match(/^\/(.*)\/([a-z]*)$/)
g = p ? p[1] : g
const flags = p ? p[2] : ''
return new RegExp(g, flags)
}
main()