-
Notifications
You must be signed in to change notification settings - Fork 541
/
runner.mjs
359 lines (296 loc) · 9.94 KB
/
runner.mjs
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
import { EventEmitter, once } from 'node:events'
import { isAbsolute, join, resolve } from 'node:path'
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { Worker } from 'node:worker_threads'
import { colors, handlePipes, normalizeName, parseMeta, resolveStatusPath } from './util.mjs'
const basePath = fileURLToPath(join(import.meta.url, '../..'))
const testPath = join(basePath, 'tests')
const statusPath = join(basePath, 'status')
// https://github.com/web-platform-tests/wpt/blob/b24eedd/resources/testharness.js#L3705
function sanitizeUnpairedSurrogates (str) {
return str.replace(
/([\ud800-\udbff]+)(?![\udc00-\udfff])|(^|[^\ud800-\udbff])([\udc00-\udfff]+)/g,
function (_, low, prefix, high) {
let output = prefix || '' // Prefix may be undefined
const string = low || high // Only one of these alternates can match
for (let i = 0; i < string.length; i++) {
output += codeUnitStr(string[i])
}
return output
})
}
function codeUnitStr (char) {
return 'U+' + char.charCodeAt(0).toString(16)
}
export class WPTRunner extends EventEmitter {
/** @type {string} */
#folderName
/** @type {string} */
#folderPath
/** @type {string[]} */
#files = []
/** @type {string[]} */
#initScripts = []
/** @type {string} */
#url
/** @type {import('../../status/fetch.status.json')} */
#status
/** Tests that have expectedly failed mapped by file name */
#statusOutput = {}
#uncaughtExceptions = []
/** @type {boolean} */
#appendReport
/** @type {string} */
#reportPath
#stats = {
completed: 0,
failed: 0,
success: 0,
expectedFailures: 0,
skipped: 0
}
constructor (folder, url, { appendReport = false, reportPath } = {}) {
super()
this.#folderName = folder
this.#folderPath = join(testPath, folder)
this.#files.push(
...WPTRunner.walk(
this.#folderPath,
(file) => file.endsWith('.any.js')
)
)
if (appendReport) {
if (!reportPath) {
throw new TypeError('reportPath must be provided when appendReport is true')
}
if (!existsSync(reportPath)) {
throw new TypeError('reportPath is invalid')
}
}
this.#appendReport = appendReport
this.#reportPath = reportPath
this.#status = JSON.parse(readFileSync(join(statusPath, `${folder}.status.json`)))
this.#url = url
if (this.#files.length === 0) {
queueMicrotask(() => {
this.emit('completion')
})
}
this.once('completion', () => {
for (const { error, test } of this.#uncaughtExceptions) {
console.log(colors(`Uncaught exception in "${test}":`, 'red'))
console.log(colors(`${error.stack}`, 'red'))
console.log('='.repeat(96))
}
})
}
static walk (dir, fn) {
const ini = new Set(readdirSync(dir))
const files = new Set()
while (ini.size !== 0) {
for (const d of ini) {
const path = resolve(dir, d)
ini.delete(d) // remove from set
const stats = statSync(path)
if (stats.isDirectory()) {
for (const f of readdirSync(path)) {
ini.add(resolve(path, f))
}
} else if (stats.isFile() && fn(d)) {
files.add(path)
}
}
}
return [...files].sort()
}
async run () {
const workerPath = fileURLToPath(join(import.meta.url, '../worker.mjs'))
/** @type {Set<Worker>} */
const activeWorkers = new Set()
let finishedFiles = 1
let total = this.#files.length
const files = this.#files.map((test) => {
const code = test.includes('.sub.')
? handlePipes(readFileSync(test, 'utf-8'), this.#url)
: readFileSync(test, 'utf-8')
const meta = this.resolveMeta(code, test)
if (meta.variant.length) {
total += meta.variant.length - 1
}
return [test, code, meta]
})
console.log('='.repeat(96))
for (const [test, code, meta] of files) {
console.log(`Started ${test}`)
const status = resolveStatusPath(test, this.#status)
if (status.file.skip || status.topLevel.skip) {
this.#stats.skipped += 1
console.log(colors(`[${finishedFiles}/${total}] SKIPPED - ${test}`, 'yellow'))
console.log('='.repeat(96))
finishedFiles++
continue
}
const start = performance.now()
for (const variant of meta.variant.length ? meta.variant : ['']) {
const url = new URL(this.#url)
if (variant) {
url.search = variant
}
const worker = new Worker(workerPath, {
workerData: {
// Code to load before the test harness and tests.
initScripts: this.#initScripts,
// The test file.
test: code,
// Parsed META tag information
meta,
url: url.href,
path: test
}
})
let result, report
if (this.#appendReport) {
report = JSON.parse(readFileSync(this.#reportPath))
const fileUrl = new URL(`/${this.#folderName}${test.slice(this.#folderPath.length)}`, 'http://wpt')
fileUrl.pathname = fileUrl.pathname.replace(/\.js$/, '.html')
fileUrl.search = variant
result = {
test: fileUrl.href.slice(fileUrl.origin.length),
subtests: [],
status: 'OK'
}
report.results.push(result)
}
activeWorkers.add(worker)
// These values come directly from the web-platform-tests
const timeout = meta.timeout === 'long' ? 60_000 : 10_000
worker.on('message', (message) => {
if (message.type === 'result') {
this.handleIndividualTestCompletion(message, status, test, meta, result)
} else if (message.type === 'completion') {
this.handleTestCompletion(worker)
} else if (message.type === 'error') {
this.#uncaughtExceptions.push({ error: message.error, test })
this.#stats.failed += 1
this.#stats.success -= 1
}
})
try {
await once(worker, 'exit', {
signal: AbortSignal.timeout(timeout)
})
console.log(colors(`[${finishedFiles}/${total}] PASSED - ${test}`, 'green'))
if (variant) console.log('Variant:', variant)
console.log(`Test took ${(performance.now() - start).toFixed(2)}ms`)
console.log('='.repeat(96))
} catch (e) {
console.log(`${test} timed out after ${timeout}ms`)
} finally {
if (result?.subtests.length > 0) {
writeFileSync(this.#reportPath, JSON.stringify(report))
}
finishedFiles++
activeWorkers.delete(worker)
}
}
}
this.handleRunnerCompletion()
}
/**
* Called after a test has succeeded or failed.
*/
handleIndividualTestCompletion (message, status, path, meta, wptResult) {
const { file, topLevel } = status
if (message.type === 'result') {
this.#stats.completed += 1
if (message.result.status === 1) {
this.#stats.failed += 1
wptResult?.subtests.push({
status: 'FAIL',
name: sanitizeUnpairedSurrogates(message.result.name),
message: sanitizeUnpairedSurrogates(message.result.message)
})
const name = normalizeName(message.result.name)
if (file.flaky?.includes(name)) {
this.#stats.expectedFailures += 1
} else if (file.allowUnexpectedFailures || topLevel.allowUnexpectedFailures || file.fail?.includes(name)) {
if (!file.allowUnexpectedFailures && !topLevel.allowUnexpectedFailures) {
if (Array.isArray(file.fail)) {
this.#statusOutput[path] ??= []
this.#statusOutput[path].push(name)
}
}
this.#stats.expectedFailures += 1
} else {
process.exitCode = 1
console.error(message.result)
}
} else {
wptResult?.subtests.push({
status: 'PASS',
name: sanitizeUnpairedSurrogates(message.result.name)
})
this.#stats.success += 1
}
}
}
/**
* Called after all the tests in a worker are completed.
* @param {Worker} worker
*/
handleTestCompletion (worker) {
worker.terminate()
}
/**
* Called after every test has completed.
*/
handleRunnerCompletion () {
// tests that failed
if (Object.keys(this.#statusOutput).length !== 0) {
console.log(this.#statusOutput)
}
this.emit('completion')
const { completed, failed, success, expectedFailures, skipped } = this.#stats
console.log(
`[${this.#folderName}]: ` +
`completed: ${completed}, failed: ${failed}, success: ${success}, ` +
`expected failures: ${expectedFailures}, ` +
`unexpected failures: ${failed - expectedFailures}, ` +
`skipped: ${skipped}`
)
process.exit(failed - expectedFailures ? 1 : process.exitCode)
}
addInitScript (code) {
this.#initScripts.push(code)
}
/**
* Parses META tags and resolves any script file paths.
* @param {string} code
* @param {string} path The absolute path of the test
*/
resolveMeta (code, path) {
const meta = parseMeta(code)
const scripts = meta.scripts.map((filePath) => {
let content = ''
if (filePath === '/resources/WebIDLParser.js') {
// See https://github.com/web-platform-tests/wpt/pull/731
return readFileSync(join(testPath, '/resources/webidl2/lib/webidl2.js'), 'utf-8')
} else if (isAbsolute(filePath)) {
content = readFileSync(join(testPath, filePath), 'utf-8')
} else {
content = readFileSync(resolve(path, '..', filePath), 'utf-8')
}
// If the file has any built-in pipes.
if (filePath.includes('.sub.')) {
content = handlePipes(content, this.#url)
}
return content
})
return {
...meta,
resourcePaths: meta.scripts,
scripts
}
}
}