-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
executable file
·148 lines (126 loc) · 3.7 KB
/
index.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
#! /usr/bin/env node
import { spawn } from 'child_process'
import { globArgs } from './lib/glob'
import { Result, runTest } from './lib/run-test'
const argv: {
o: boolean
p: number
j: boolean
t: number
'node-arg': string | string[]
controller: string
_: string[]
} = require('minimist')(process.argv.slice(2), {
boolean: ['o', 'j'],
default: {
p: 1,
t: 0,
},
})
const results = new Map<string, Result>()
const nodeArgs = new Array<string>()
if (argv['node-arg']) {
if (Array.isArray(argv['node-arg'])) {
nodeArgs.push(...argv['node-arg'])
} else {
nodeArgs.push(argv['node-arg'])
}
}
const files = globArgs(argv._).sort()
const inProgress = new Set<string>()
let aborted = new Set<string>()
function printInProgress() {
inProgress.forEach(file => {
aborted.add(file)
})
}
async function thread() {
let file: string | undefined
// tslint:disable-next-line:no-conditional-assignment
while ((file = files.shift())) {
inProgress.add(file)
const result = await runTest(
file,
nodeArgs,
argv.p === 1,
argv.o,
argv.j,
argv.t
)
inProgress.delete(file)
results.set(file, result)
}
}
async function run() {
let controller: ReturnType<typeof spawn> | undefined
let controllerRunning = false
if (argv.controller) {
await new Promise<void>((resolve, reject) => {
controller = spawn(argv.controller, [], {
shell: true,
stdio: ['ignore', 'pipe', 'pipe'],
})
controller.stdout?.on('data', data => {
console.log(`controller: ${data}`)
controllerRunning = true
resolve()
})
controller.stderr?.on('data', data => {
console.error(`controller: ${data}`)
})
controller.on('error', code => {
reject()
console.log(`controller error ${code}`)
})
controller.on('close', () => {
controllerRunning = false
})
})
}
await Promise.all(new Array(argv.p).fill(0).map(() => thread()))
if (controller && controllerRunning) {
console.log('controller: stopping')
controller.once('close', () => printSummary())
controller.kill()
} else {
printSummary()
}
}
function printSummary() {
let success = true
console.log('')
for (let [file, res] of [...results.entries()].sort(
(a, b) => a[1].executionTime - b[1].executionTime
)) {
const { exitCode, result: r, executionTime } = res
const timeStr = `${(executionTime / 1000).toFixed(1)}s`
if (exitCode === 0 && r.ok) {
console.log(`OK ${file} (${timeStr}) ${r.pass}/${r.count}`)
} else if (!r.ok) {
success = false
console.log(
`FAIL ${file} (${timeStr}) ${r.pass || 0}/${r.count || 0}`
)
} else {
success = false
console.log(`FAIL ${file} exited with error ${exitCode}`)
}
}
if (aborted.size > 0) {
console.log('\nmulti-tape aborted. Tests in progress: ')
aborted.forEach(file => console.log(` ${file}`))
success = false
}
if (!success) {
process.exit(1)
}
}
process.on('SIGTERM', printInProgress)
process.on('SIGINT', printInProgress)
if (process.env.MT_DEBUG_INTERVAL) {
setInterval(() => {
console.log(`## Queued: ${files.length}`)
console.log(`## Running: ${[...inProgress.keys()]}`)
}, parseInt(process.env.MT_DEBUG_INTERVAL)).unref()
}
void run()