Skip to content

Commit 0f86c2b

Browse files
joyeecheungtargos
authored andcommitted
test: run WPT in subdirectories
For a directory like this: - wpt - encoding - streams - backpressure.any.js - api-basics.any.js Previously we only run `api-basics.any.js`, now we also run `backpressure.any.js` (and any tests in more deeply nested directories). This enables us to run more of WPT since not every module put their tests at the top level directory. PR-URL: #27860 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 51ccdae commit 0f86c2b

File tree

1 file changed

+58
-22
lines changed

1 file changed

+58
-22
lines changed

test/common/wpt.js

+58-22
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,25 @@ class ResourceLoader {
4141
this.path = path;
4242
}
4343

44-
fetch(url, asPromise = true) {
44+
/**
45+
* Load a resource in test/fixtures/wpt specified with a URL
46+
* @param {string} from the path of the file loading this resource,
47+
* relative to thw WPT folder.
48+
* @param {string} url the url of the resource being loaded.
49+
* @param {boolean} asPromise if true, return the resource in a
50+
* pseudo-Response object.
51+
*/
52+
read(from, url, asFetch = true) {
4553
// We need to patch this to load the WebIDL parser
4654
url = url.replace(
4755
'/resources/WebIDLParser.js',
4856
'/resources/webidl2/lib/webidl2.js'
4957
);
58+
const base = path.dirname(from);
5059
const file = url.startsWith('/') ?
5160
fixtures.path('wpt', url) :
52-
fixtures.path('wpt', this.path, url);
53-
if (asPromise) {
61+
fixtures.path('wpt', base, url);
62+
if (asFetch) {
5463
return fsPromises.readFile(file)
5564
.then((data) => {
5665
return {
@@ -85,7 +94,7 @@ class StatusRule {
8594
* @returns {RegExp}
8695
*/
8796
transformPattern(pattern) {
88-
const result = pattern.replace(/[-/\\^$+?.()|[\]{}]/g, '\\$&');
97+
const result = path.normalize(pattern).replace(/[-/\\^$+?.()|[\]{}]/g, '\\$&');
8998
return new RegExp(result.replace('*', '.*'));
9099
}
91100
}
@@ -155,8 +164,12 @@ class WPTTest {
155164
}
156165
}
157166

167+
getRelativePath() {
168+
return path.join(this.module, this.filename);
169+
}
170+
158171
getAbsolutePath() {
159-
return fixtures.path('wpt', this.module, this.filename);
172+
return fixtures.path('wpt', this.getRelativePath());
160173
}
161174

162175
getContent() {
@@ -217,20 +230,41 @@ class StatusLoader {
217230
this.tests = [];
218231
}
219232

233+
/**
234+
* Grep for all .*.js file recursively in a directory.
235+
* @param {string} dir
236+
*/
237+
grep(dir) {
238+
let result = [];
239+
const list = fs.readdirSync(dir);
240+
for (const file of list) {
241+
const filepath = path.join(dir, file);
242+
const stat = fs.statSync(filepath);
243+
if (stat.isDirectory()) {
244+
const list = this.grep(filepath);
245+
result = result.concat(list);
246+
} else {
247+
if (!(/\.\w+\.js$/.test(filepath))) {
248+
continue;
249+
}
250+
result.push(filepath);
251+
}
252+
}
253+
return result;
254+
}
255+
220256
load() {
221257
const dir = path.join(__dirname, '..', 'wpt');
222258
const statusFile = path.join(dir, 'status', `${this.path}.json`);
223259
const result = JSON.parse(fs.readFileSync(statusFile, 'utf8'));
224260
this.rules.addRules(result);
225261

226-
const list = fs.readdirSync(fixtures.path('wpt', this.path));
227-
262+
const subDir = fixtures.path('wpt', this.path);
263+
const list = this.grep(subDir);
228264
for (const file of list) {
229-
if (!(/\.\w+\.js$/.test(file))) {
230-
continue;
231-
}
232-
const match = this.rules.match(file);
233-
this.tests.push(new WPTTest(this.path, file, match));
265+
const relativePath = path.relative(subDir, file);
266+
const match = this.rules.match(relativePath);
267+
this.tests.push(new WPTTest(this.path, relativePath, match));
234268
}
235269
this.loaded = true;
236270
}
@@ -309,8 +343,9 @@ class WPTRunner {
309343
const meta = test.title = this.getMeta(content);
310344

311345
const absolutePath = test.getAbsolutePath();
312-
const context = this.generateContext(test.filename);
313-
const code = this.mergeScripts(meta, content);
346+
const context = this.generateContext(test);
347+
const relativePath = test.getRelativePath();
348+
const code = this.mergeScripts(relativePath, meta, content);
314349
try {
315350
vm.runInContext(code, context, {
316351
filename: absolutePath
@@ -327,14 +362,14 @@ class WPTRunner {
327362
this.tryFinish();
328363
}
329364

330-
mock() {
365+
mock(testfile) {
331366
const resource = this.resource;
332367
const result = {
333368
// This is a mock, because at the moment fetch is not implemented
334369
// in Node.js, but some tests and harness depend on this to pull
335370
// resources.
336371
fetch(file) {
337-
return resource.fetch(file);
372+
return resource.read(testfile, file, true);
338373
},
339374
GLOBAL: {
340375
isWindow() { return false; }
@@ -346,16 +381,17 @@ class WPTRunner {
346381
}
347382

348383
// Note: this is how our global space for the WPT test should look like
349-
getSandbox() {
350-
const result = this.mock();
384+
getSandbox(filename) {
385+
const result = this.mock(filename);
351386
for (const [name, desc] of this.globals) {
352387
Object.defineProperty(result, name, desc);
353388
}
354389
return result;
355390
}
356391

357-
generateContext(filename) {
358-
const sandbox = this.sandbox = this.getSandbox();
392+
generateContext(test) {
393+
const filename = test.filename;
394+
const sandbox = this.sandbox = this.getSandbox(test.getRelativePath());
359395
const context = this.context = vm.createContext(sandbox);
360396

361397
const harnessPath = fixtures.path('wpt', 'resources', 'testharness.js');
@@ -509,15 +545,15 @@ class WPTRunner {
509545
}
510546
}
511547

512-
mergeScripts(meta, content) {
548+
mergeScripts(base, meta, content) {
513549
if (!meta.script) {
514550
return content;
515551
}
516552

517553
// only one script
518554
let result = '';
519555
for (const script of meta.script) {
520-
result += this.resource.fetch(script, false);
556+
result += this.resource.read(base, script, false);
521557
}
522558

523559
return result + content;

0 commit comments

Comments
 (0)