/
borp.js
executable file
·172 lines (151 loc) · 4.58 KB
/
borp.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
#! /usr/bin/env node
import { parseArgs } from 'node:util'
import Reporters from 'node:test/reporters'
import { mkdtemp, rm, readFile } from 'node:fs/promises'
import { createWriteStream } from 'node:fs'
import { finished } from 'node:stream/promises'
import { join, relative } from 'node:path'
import posix from 'node:path/posix'
import runWithTypeScript from './lib/run.js'
import githubReporter from '@reporters/github'
import { Report } from 'c8'
import { checkCoverages } from 'c8/lib/commands/check-coverage.js'
import os from 'node:os'
import { execa } from 'execa'
/* c8 ignore next 4 */
process.on('unhandledRejection', (err) => {
console.error(err)
process.exit(1)
})
const args = parseArgs({
args: process.argv.slice(2),
options: {
only: { type: 'boolean', short: 'o' },
watch: { type: 'boolean', short: 'w' },
pattern: { type: 'string', short: 'p' },
concurrency: { type: 'string', short: 'c', default: os.availableParallelism() - 1 + '' },
coverage: { type: 'boolean', short: 'C' },
timeout: { type: 'string', short: 't', default: '30000' },
'no-timeout': { type: 'boolean' },
'coverage-exclude': { type: 'string', short: 'X', multiple: true },
ignore: { type: 'string', short: 'i', multiple: true },
'expose-gc': { type: 'boolean' },
help: { type: 'boolean', short: 'h' },
'no-typescript': { type: 'boolean', short: 'T' },
'post-compile': { type: 'string', short: 'P' },
reporter: {
type: 'string',
short: 'r',
default: ['spec'],
multiple: true
},
'check-coverage': { type: 'boolean' },
lines: { type: 'string', default: '100' },
branches: { type: 'string', default: '100' },
functions: { type: 'string', default: '100' },
statements: { type: 'string', default: '100' }
},
allowPositionals: true
})
/* c8 ignore next 5 */
if (args.values.help) {
console.log(await readFile(new URL('./README.md', import.meta.url), 'utf8'))
process.exit(0)
}
if (args.values['expose-gc'] && typeof global.gc !== 'function') {
try {
await execa('node', ['--expose-gc', ...process.argv.slice(1)], {
stdio: 'inherit',
env: {
...process.env
}
})
process.exit(0)
} catch (error) {
process.exit(1)
}
}
if (args.values.concurrency) {
args.values.concurrency = parseInt(args.values.concurrency)
}
if (args.values['no-timeout']) {
delete args.values.timeout
}
if (args.values.timeout) {
args.values.timeout = parseInt(args.values.timeout)
}
let covDir
if (args.values.coverage) {
covDir = await mkdtemp(join(os.tmpdir(), 'coverage-'))
process.env.NODE_V8_COVERAGE = covDir
}
const config = {
...args.values,
typescript: !args.values['no-typescript'],
files: args.positionals,
pattern: args.values.pattern,
cwd: process.cwd()
}
try {
const pipes = []
const reporters = {
...Reporters,
gh: githubReporter
}
// If we're running in a GitHub action, adds the gh reporter
// by default so that we can report failures to GitHub
if (process.env.GITHUB_ACTION) {
args.values.reporter.push('gh')
}
for (const input of args.values.reporter) {
const [name, dest] = input.split(':')
const Ctor = reporters[name] || await import(name).then((m) => m.default || m)
const reporter = Ctor.prototype && Object.getOwnPropertyDescriptor(Ctor.prototype, 'constructor') ? new Ctor() : Ctor
let output = process.stdout
if (dest) {
output = createWriteStream(dest)
}
pipes.push([reporter, output])
}
const stream = await runWithTypeScript(config)
stream.on('test:fail', () => {
process.exitCode = 1
})
for (const [reporter, output] of pipes) {
stream.compose(reporter).pipe(output)
}
await finished(stream)
if (covDir) {
let exclude = args.values['coverage-exclude']
if (exclude && config.prefix) {
const localPrefix = relative(process.cwd(), config.prefix)
exclude = exclude.map((file) => posix.join(localPrefix, file))
}
const report = Report({
reporter: ['text'],
tempDirectory: covDir,
exclude
})
if (args.values['check-coverage']) {
await checkCoverages({
lines: parseInt(args.values.lines),
functions: parseInt(args.values.functions),
branches: parseInt(args.values.branches),
statements: parseInt(args.values.statements),
...args
}, report)
}
await report.run()
}
/* c8 ignore next 3 */
} catch (err) {
console.error(err)
process.exitCode = 1
} finally {
if (covDir) {
try {
await rm(covDir, { recursive: true, maxRetries: 10, retryDelay: 100 })
/* c8 ignore next 2 */
} catch {}
}
}