@@ -41,16 +41,25 @@ class ResourceLoader {
41
41
this . path = path ;
42
42
}
43
43
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 ) {
45
53
// We need to patch this to load the WebIDL parser
46
54
url = url . replace (
47
55
'/resources/WebIDLParser.js' ,
48
56
'/resources/webidl2/lib/webidl2.js'
49
57
) ;
58
+ const base = path . dirname ( from ) ;
50
59
const file = url . startsWith ( '/' ) ?
51
60
fixtures . path ( 'wpt' , url ) :
52
- fixtures . path ( 'wpt' , this . path , url ) ;
53
- if ( asPromise ) {
61
+ fixtures . path ( 'wpt' , base , url ) ;
62
+ if ( asFetch ) {
54
63
return fsPromises . readFile ( file )
55
64
. then ( ( data ) => {
56
65
return {
@@ -85,7 +94,7 @@ class StatusRule {
85
94
* @returns {RegExp }
86
95
*/
87
96
transformPattern ( pattern ) {
88
- const result = pattern . replace ( / [ - / \\ ^ $ + ? . ( ) | [ \] { } ] / g, '\\$&' ) ;
97
+ const result = path . normalize ( pattern ) . replace ( / [ - / \\ ^ $ + ? . ( ) | [ \] { } ] / g, '\\$&' ) ;
89
98
return new RegExp ( result . replace ( '*' , '.*' ) ) ;
90
99
}
91
100
}
@@ -155,8 +164,12 @@ class WPTTest {
155
164
}
156
165
}
157
166
167
+ getRelativePath ( ) {
168
+ return path . join ( this . module , this . filename ) ;
169
+ }
170
+
158
171
getAbsolutePath ( ) {
159
- return fixtures . path ( 'wpt' , this . module , this . filename ) ;
172
+ return fixtures . path ( 'wpt' , this . getRelativePath ( ) ) ;
160
173
}
161
174
162
175
getContent ( ) {
@@ -217,20 +230,41 @@ class StatusLoader {
217
230
this . tests = [ ] ;
218
231
}
219
232
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 + \. j s $ / . test ( filepath ) ) ) {
248
+ continue ;
249
+ }
250
+ result . push ( filepath ) ;
251
+ }
252
+ }
253
+ return result ;
254
+ }
255
+
220
256
load ( ) {
221
257
const dir = path . join ( __dirname , '..' , 'wpt' ) ;
222
258
const statusFile = path . join ( dir , 'status' , `${ this . path } .json` ) ;
223
259
const result = JSON . parse ( fs . readFileSync ( statusFile , 'utf8' ) ) ;
224
260
this . rules . addRules ( result ) ;
225
261
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 ) ;
228
264
for ( const file of list ) {
229
- if ( ! ( / \. \w + \. j s $ / . 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 ) ) ;
234
268
}
235
269
this . loaded = true ;
236
270
}
@@ -309,8 +343,9 @@ class WPTRunner {
309
343
const meta = test . title = this . getMeta ( content ) ;
310
344
311
345
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 ) ;
314
349
try {
315
350
vm . runInContext ( code , context , {
316
351
filename : absolutePath
@@ -327,14 +362,14 @@ class WPTRunner {
327
362
this . tryFinish ( ) ;
328
363
}
329
364
330
- mock ( ) {
365
+ mock ( testfile ) {
331
366
const resource = this . resource ;
332
367
const result = {
333
368
// This is a mock, because at the moment fetch is not implemented
334
369
// in Node.js, but some tests and harness depend on this to pull
335
370
// resources.
336
371
fetch ( file ) {
337
- return resource . fetch ( file ) ;
372
+ return resource . read ( testfile , file , true ) ;
338
373
} ,
339
374
GLOBAL : {
340
375
isWindow ( ) { return false ; }
@@ -346,16 +381,17 @@ class WPTRunner {
346
381
}
347
382
348
383
// 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 ) ;
351
386
for ( const [ name , desc ] of this . globals ) {
352
387
Object . defineProperty ( result , name , desc ) ;
353
388
}
354
389
return result ;
355
390
}
356
391
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 ( ) ) ;
359
395
const context = this . context = vm . createContext ( sandbox ) ;
360
396
361
397
const harnessPath = fixtures . path ( 'wpt' , 'resources' , 'testharness.js' ) ;
@@ -509,15 +545,15 @@ class WPTRunner {
509
545
}
510
546
}
511
547
512
- mergeScripts ( meta , content ) {
548
+ mergeScripts ( base , meta , content ) {
513
549
if ( ! meta . script ) {
514
550
return content ;
515
551
}
516
552
517
553
// only one script
518
554
let result = '' ;
519
555
for ( const script of meta . script ) {
520
- result += this . resource . fetch ( script , false ) ;
556
+ result += this . resource . read ( base , script , false ) ;
521
557
}
522
558
523
559
return result + content ;
0 commit comments