@@ -33,6 +33,17 @@ const parentFilepath = path.join(tmpdir.path, 'parent.js');
33
33
const parentURL = pathToFileURL ( parentFilepath ) ;
34
34
const parentBody = 'require(\'./dep.js\')' ;
35
35
36
+ const workerSpawningFilepath = path . join ( tmpdir . path , 'worker_spawner.js' ) ;
37
+ const workerSpawningURL = pathToFileURL ( workerSpawningFilepath ) ;
38
+ const workerSpawningBody = `
39
+ const { Worker } = require('worker_threads');
40
+ // make sure this is gone to ensure we don't do another fs read of it
41
+ // will error out if we do
42
+ require('fs').unlinkSync(${ JSON . stringify ( policyFilepath ) } );
43
+ const w = new Worker(${ JSON . stringify ( parentFilepath ) } );
44
+ w.on('exit', process.exit);
45
+ ` ;
46
+
36
47
const depFilepath = path . join ( tmpdir . path , 'dep.js' ) ;
37
48
const depURL = pathToFileURL ( depFilepath ) ;
38
49
const depBody = '' ;
@@ -49,8 +60,9 @@ if (!tmpdirURL.pathname.endsWith('/')) {
49
60
}
50
61
function test ( {
51
62
shouldFail = false ,
63
+ preload = [ ] ,
52
64
entry,
53
- onerror,
65
+ onerror = undefined ,
54
66
resources = { }
55
67
} ) {
56
68
const manifest = {
@@ -65,7 +77,9 @@ function test({
65
77
}
66
78
fs . writeFileSync ( policyFilepath , JSON . stringify ( manifest , null , 2 ) ) ;
67
79
const { status } = spawnSync ( process . execPath , [
68
- '--experimental-policy' , policyFilepath , entry
80
+ '--experimental-policy' , policyFilepath ,
81
+ ...preload . map ( ( m ) => [ '-r' , m ] ) . flat ( ) ,
82
+ entry
69
83
] ) ;
70
84
if ( shouldFail ) {
71
85
assert . notStrictEqual ( status , 0 ) ;
@@ -74,13 +88,25 @@ function test({
74
88
}
75
89
}
76
90
77
- const { status } = spawnSync ( process . execPath , [
78
- '--experimental-policy' , policyFilepath ,
79
- '--experimental-policy' , policyFilepath
80
- ] , {
81
- stdio : 'pipe'
82
- } ) ;
83
- assert . notStrictEqual ( status , 0 , 'Should not allow multiple policies' ) ;
91
+ {
92
+ const { status } = spawnSync ( process . execPath , [
93
+ '--experimental-policy' , policyFilepath ,
94
+ '--experimental-policy' , policyFilepath
95
+ ] , {
96
+ stdio : 'pipe'
97
+ } ) ;
98
+ assert . notStrictEqual ( status , 0 , 'Should not allow multiple policies' ) ;
99
+ }
100
+ {
101
+ const enoentFilepath = path . join ( tmpdir . path , 'enoent' ) ;
102
+ try { fs . unlinkSync ( enoentFilepath ) ; } catch { }
103
+ const { status } = spawnSync ( process . execPath , [
104
+ '--experimental-policy' , enoentFilepath , '-e' , ''
105
+ ] , {
106
+ stdio : 'pipe'
107
+ } ) ;
108
+ assert . notStrictEqual ( status , 0 , 'Should not allow missing policies' ) ;
109
+ }
84
110
85
111
test ( {
86
112
shouldFail : true ,
@@ -195,6 +221,21 @@ test({
195
221
}
196
222
}
197
223
} ) ;
224
+ test ( {
225
+ shouldFail : false ,
226
+ preload : [ depFilepath ] ,
227
+ entry : parentFilepath ,
228
+ resources : {
229
+ [ parentURL ] : {
230
+ body : parentBody ,
231
+ match : true ,
232
+ } ,
233
+ [ depURL ] : {
234
+ body : depBody ,
235
+ match : true ,
236
+ }
237
+ }
238
+ } ) ;
198
239
test ( {
199
240
shouldFail : true ,
200
241
entry : parentFilepath ,
@@ -295,3 +336,50 @@ test({
295
336
}
296
337
}
297
338
} ) ;
339
+ test ( {
340
+ shouldFail : true ,
341
+ entry : workerSpawningFilepath ,
342
+ resources : {
343
+ [ workerSpawningURL ] : {
344
+ body : workerSpawningBody ,
345
+ match : true ,
346
+ } ,
347
+ }
348
+ } ) ;
349
+ test ( {
350
+ shouldFail : false ,
351
+ entry : workerSpawningFilepath ,
352
+ resources : {
353
+ [ workerSpawningURL ] : {
354
+ body : workerSpawningBody ,
355
+ match : true ,
356
+ } ,
357
+ [ parentURL ] : {
358
+ body : parentBody ,
359
+ match : true ,
360
+ } ,
361
+ [ depURL ] : {
362
+ body : depBody ,
363
+ match : true ,
364
+ }
365
+ }
366
+ } ) ;
367
+ test ( {
368
+ shouldFail : false ,
369
+ entry : workerSpawningFilepath ,
370
+ preload : [ parentFilepath ] ,
371
+ resources : {
372
+ [ workerSpawningURL ] : {
373
+ body : workerSpawningBody ,
374
+ match : true ,
375
+ } ,
376
+ [ parentURL ] : {
377
+ body : parentBody ,
378
+ match : true ,
379
+ } ,
380
+ [ depURL ] : {
381
+ body : depBody ,
382
+ match : true ,
383
+ }
384
+ }
385
+ } ) ;
0 commit comments