-
Notifications
You must be signed in to change notification settings - Fork 161
/
run-web-platform-tests.js
85 lines (73 loc) · 2.48 KB
/
run-web-platform-tests.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
// This runs the web platform tests against the reference implementation, in Node.js using jsdom, for easier rapid
// development of the reference implementation and the web platform tests.
/* eslint-disable no-console */
'use strict';
const path = require('path');
const fs = require('fs');
const { promisify } = require('util');
const browserify = require('browserify');
const wptRunner = require('wpt-runner');
const minimatch = require('minimatch');
const readFileAsync = promisify(fs.readFile);
// wpt-runner does not yet support unhandled rejection tracking a la
// https://github.com/w3c/testharness.js/commit/7716e2581a86dfd9405a9c00547a7504f0c7fe94
// So we emulate it with Node.js events
const rejections = new Map();
process.on('unhandledRejection', (reason, promise) => {
rejections.set(promise, reason);
});
process.on('rejectionHandled', promise => {
rejections.delete(promise);
});
main().catch(e => {
console.error(e.stack);
process.exitCode = 1;
});
async function main() {
const entryPath = path.resolve(__dirname, 'lib/index.js');
const wptPath = path.resolve(__dirname, 'web-platform-tests');
const testsPath = path.resolve(wptPath, 'streams');
const filterGlobs = process.argv.length >= 3 ? process.argv.slice(2) : ['**/*.html'];
const anyTestPattern = /\.any\.html$/;
const bundledJS = await bundle(entryPath);
const failures = await wptRunner(testsPath, {
rootURL: 'streams/',
setup(window) {
window.queueMicrotask = queueMicrotask;
window.fetch = async function (url) {
const filePath = path.join(wptPath, url);
if (!filePath.startsWith(wptPath)) {
throw new TypeError('Invalid URL');
}
return {
ok: true,
async text() {
return await readFileAsync(filePath, { encoding: 'utf8' });
}
};
};
window.eval(bundledJS);
},
filter(testPath) {
// Ignore the window-specific and worker-specific tests
if (!anyTestPattern.test(testPath)) {
return false;
}
return filterGlobs.some(glob => minimatch(testPath, glob));
}
});
process.exitCode = failures;
if (rejections.size > 0) {
if (failures === 0) {
process.exitCode = 1;
}
for (const reason of rejections.values()) {
console.error('Unhandled promise rejection: ', reason.stack);
}
}
}
async function bundle(entryPath) {
const b = browserify([entryPath]);
const buffer = await promisify(b.bundle.bind(b))();
return buffer.toString();
}