@@ -11,6 +11,8 @@ const LOG_DEBUGGING = false;
11
11
const enableAot = process . env . WEVAL_TEST == '1' ;
12
12
const debugBuild = process . env . DEBUG_TEST == '1' ;
13
13
14
+ const noOp = async ( ) => { } ;
15
+
14
16
function maybeLogging ( disableFeatures ) {
15
17
if ( ! LOG_DEBUGGING ) return disableFeatures ;
16
18
if ( disableFeatures && disableFeatures . includes ( 'stdio' ) ) {
@@ -23,13 +25,26 @@ const builtinsCases = await readdir(new URL('./builtins', import.meta.url));
23
25
suite ( 'Builtins' , ( ) => {
24
26
for ( const filename of builtinsCases ) {
25
27
const name = filename . slice ( 0 , - 3 ) ;
28
+ if ( ! name . includes ( "fetch" ) ) { continue ; }
26
29
test ( name , async ( ) => {
30
+ const testModule = await import ( `./builtins/${ filename } ` ) ;
27
31
const {
28
- source ,
32
+ state ,
29
33
test : runTest ,
30
34
disableFeatures,
31
35
enableFeatures,
32
- } = await import ( `./builtins/${ filename } ` ) ;
36
+ } = testModule ;
37
+
38
+ // If an args object was provided, generate the arguments to feed to both
39
+ // source generation (if necessary) and the test run itself
40
+ let stateFn = state ?? noOp ;
41
+ const stateObj = await stateFn ( ) ;
42
+
43
+ // If the source is a function then invoke it to generate the source string, possibly with arguments
44
+ let source = testModule . source ;
45
+ if ( typeof source === 'function' ) {
46
+ source = source ( stateObj ) ;
47
+ }
33
48
34
49
const { component } = await componentize (
35
50
source ,
@@ -46,7 +61,7 @@ suite('Builtins', () => {
46
61
enableFeatures,
47
62
disableFeatures : maybeLogging ( disableFeatures ) ,
48
63
enableAot,
49
- }
64
+ } ,
50
65
) ;
51
66
52
67
const { files } = await transpile ( component , {
@@ -61,13 +76,13 @@ suite('Builtins', () => {
61
76
62
77
await writeFile (
63
78
new URL ( `./output/${ name } .component.wasm` , import . meta. url ) ,
64
- component
79
+ component ,
65
80
) ;
66
81
67
82
for ( const file of Object . keys ( files ) ) {
68
83
await writeFile (
69
84
new URL ( `./output/${ name } /${ file } ` , import . meta. url ) ,
70
- files [ file ]
85
+ files [ file ] ,
71
86
) ;
72
87
}
73
88
@@ -76,11 +91,12 @@ suite('Builtins', () => {
76
91
`
77
92
import { run } from './${ name } .js';
78
93
run();
79
- `
94
+ ` ,
80
95
) ;
81
96
82
97
try {
83
- await runTest ( async function run ( ) {
98
+ // Build a run function to pass to the test
99
+ const runFn = async function run ( ) {
84
100
let stdout = '' ,
85
101
stderr = '' ,
86
102
timeout ;
@@ -90,10 +106,10 @@ suite('Builtins', () => {
90
106
process . argv [ 0 ] ,
91
107
[
92
108
fileURLToPath (
93
- new URL ( `./output/${ name } /run.js` , import . meta. url )
109
+ new URL ( `./output/${ name } /run.js` , import . meta. url ) ,
94
110
) ,
95
111
] ,
96
- { stdio : 'pipe' }
112
+ { stdio : 'pipe' } ,
97
113
) ;
98
114
cp . stdout . on ( 'data' , ( chunk ) => {
99
115
stdout += chunk ;
@@ -103,16 +119,16 @@ suite('Builtins', () => {
103
119
} ) ;
104
120
cp . on ( 'error' , reject ) ;
105
121
cp . on ( 'exit' , ( code ) =>
106
- code === 0 ? resolve ( ) : reject ( new Error ( stderr || stdout ) )
122
+ code === 0 ? resolve ( ) : reject ( new Error ( stderr || stdout ) ) ,
107
123
) ;
108
124
timeout = setTimeout ( ( ) => {
109
125
reject (
110
126
new Error (
111
127
'test timed out with output:\n' +
112
- stdout +
113
- '\n\nstderr:\n' +
114
- stderr
115
- )
128
+ stdout +
129
+ '\n\nstderr:\n' +
130
+ stderr ,
131
+ ) ,
116
132
) ;
117
133
} , 10_000 ) ;
118
134
} ) ;
@@ -123,7 +139,10 @@ suite('Builtins', () => {
123
139
}
124
140
125
141
return { stdout, stderr } ;
126
- } ) ;
142
+ } ;
143
+
144
+ // Run the actual test
145
+ await runTest ( runFn , stateObj ) ;
127
146
} catch ( err ) {
128
147
if ( err . stderr ) console . error ( err . stderr ) ;
129
148
throw err . err || err ;
@@ -138,7 +157,7 @@ suite('Bindings', () => {
138
157
test ( name , async ( ) => {
139
158
const source = await readFile (
140
159
new URL ( `./cases/${ name } /source.js` , import . meta. url ) ,
141
- 'utf8'
160
+ 'utf8' ,
142
161
) ;
143
162
144
163
let witWorld ,
@@ -148,14 +167,14 @@ suite('Bindings', () => {
148
167
try {
149
168
witWorld = await readFile (
150
169
new URL ( `./cases/${ name } /world.wit` , import . meta. url ) ,
151
- 'utf8'
170
+ 'utf8' ,
152
171
) ;
153
172
} catch ( e ) {
154
173
if ( e ?. code == 'ENOENT' ) {
155
174
try {
156
175
isWasiTarget = true ;
157
176
witPath = fileURLToPath (
158
- new URL ( `./cases/${ name } /wit` , import . meta. url )
177
+ new URL ( `./cases/${ name } /wit` , import . meta. url ) ,
159
178
) ;
160
179
await readdir ( witPath ) ;
161
180
} catch ( e ) {
@@ -229,14 +248,14 @@ suite('Bindings', () => {
229
248
230
249
await writeFile (
231
250
new URL ( `./output/${ name } .component.wasm` , import . meta. url ) ,
232
- component
251
+ component ,
233
252
) ;
234
253
235
254
for ( const file of Object . keys ( files ) ) {
236
255
let source = files [ file ] ;
237
256
await writeFile (
238
257
new URL ( `./output/${ name } /${ file } ` , import . meta. url ) ,
239
- source
258
+ source ,
240
259
) ;
241
260
}
242
261
@@ -274,12 +293,12 @@ suite('WASI', () => {
274
293
worldName : 'test1' ,
275
294
enableAot,
276
295
debugBuild,
277
- }
296
+ } ,
278
297
) ;
279
298
280
299
await writeFile (
281
300
new URL ( `./output/wasi.component.wasm` , import . meta. url ) ,
282
- component
301
+ component ,
283
302
) ;
284
303
285
304
const { files } = await transpile ( component , { tracing : DEBUG_TRACING } ) ;
@@ -291,7 +310,7 @@ suite('WASI', () => {
291
310
for ( const file of Object . keys ( files ) ) {
292
311
await writeFile (
293
312
new URL ( `./output/wasi/${ file } ` , import . meta. url ) ,
294
- files [ file ]
313
+ files [ file ] ,
295
314
) ;
296
315
}
297
316
@@ -303,19 +322,17 @@ suite('WASI', () => {
303
322
} ) ;
304
323
305
324
test ( 'basic app (OriginalSourceFile API)' , async ( ) => {
306
- const { component } = await componentize (
307
- {
308
- sourcePath : "./test/api/index.js" ,
309
- witPath : fileURLToPath ( new URL ( './wit' , import . meta. url ) ) ,
310
- worldName : 'test1' ,
311
- enableAot,
312
- debugBuild,
313
- }
314
- ) ;
325
+ const { component } = await componentize ( {
326
+ sourcePath : './test/api/index.js' ,
327
+ witPath : fileURLToPath ( new URL ( './wit' , import . meta. url ) ) ,
328
+ worldName : 'test1' ,
329
+ enableAot,
330
+ debugBuild,
331
+ } ) ;
315
332
316
333
await writeFile (
317
334
new URL ( `./output/wasi.component.wasm` , import . meta. url ) ,
318
- component
335
+ component ,
319
336
) ;
320
337
321
338
const { files } = await transpile ( component , { tracing : DEBUG_TRACING } ) ;
@@ -327,7 +344,7 @@ suite('WASI', () => {
327
344
for ( const file of Object . keys ( files ) ) {
328
345
await writeFile (
329
346
new URL ( `./output/wasi/${ file } ` , import . meta. url ) ,
330
- files [ file ]
347
+ files [ file ] ,
331
348
) ;
332
349
}
333
350
0 commit comments