@@ -80,10 +80,11 @@ if (args.help) {
80
80
const features = process . env . ASC_FEATURES ? process . env . ASC_FEATURES . split ( "," ) : [ ] ;
81
81
const featuresConfig = require ( "./features.json" ) ;
82
82
const basedir = path . join ( dirname , "compiler" ) ;
83
+ process . chdir ( basedir ) ;
83
84
84
85
// Gets a list of all relevant tests
85
86
function getTests ( ) {
86
- let tests = glob . sync ( "**/!(_*).ts" , { cwd : basedir } )
87
+ let tests = glob . sync ( "**/!(_*).ts" )
87
88
. map ( name => name . replace ( / \. t s $ / , "" ) )
88
89
. filter ( name => ! name . endsWith ( ".d" ) && ! name . includes ( "node_modules" ) ) ;
89
90
if ( argv . length ) { // run matching tests only
@@ -134,6 +135,8 @@ async function runTest(basename) {
134
135
const stdout = asc . createMemoryStream ( ) ;
135
136
const stderr = asc . createMemoryStream ( chunk => process . stderr . write ( chunk . toString ( ) . replace ( / ^ (? ! $ ) / mg, " " ) ) ) ;
136
137
stderr . isTTY = true ;
138
+ const dummy = new Map ( ) ;
139
+ const writeFile = Map . prototype . set . bind ( dummy ) ;
137
140
let asc_flags = [ ] ;
138
141
let asc_rtrace = ! ! config . asc_rtrace ;
139
142
let v8_flags = "" ;
@@ -143,10 +146,107 @@ async function runTest(basename) {
143
146
// Makes sure to reset the environment after
144
147
function prepareResult ( code , message = null ) {
145
148
if ( v8_no_flags ) v8 . setFlagsFromString ( v8_no_flags ) ;
146
- if ( ! args . createBinary ) fs . unlink ( path . join ( basedir , basename + ".debug.wasm" ) , err => { /* nop */ } ) ;
149
+ // Delete the .wasm files in case the subsequent run doesn't specify the
150
+ // --createBinary flag, thereby preventing confusion. Also, the .debug.wasm
151
+ // file is used by the bindings/esm test.
152
+ if ( ! args . createBinary ) {
153
+ fs . unlink ( basename + ".debug.wasm" , err => { /* nop */ } ) ;
154
+ fs . unlink ( basename + ".release.wasm" , err => { /* nop */ } ) ;
155
+ }
147
156
return { code, message } ;
148
157
}
149
158
159
+ function afterCompile ( mode ) {
160
+ // The ESM bindings test requires the .wasm file to be present. The file is
161
+ // promptly deleted after the test has completed, unless --createBinary is
162
+ // specified.
163
+ {
164
+ const filename = `${ basename } .${ mode } .wasm` ;
165
+ fs . writeFileSync ( filename , dummy . get ( filename ) ) ;
166
+ }
167
+
168
+ const compareFixture = section ( "compare fixture" ) ;
169
+ const fixtureExtensions = [ "wat" , "js" , "d.ts" ] ;
170
+
171
+ if ( args . create ) {
172
+ for ( const extension of fixtureExtensions ) {
173
+ const filename = `${ basename } .${ mode } .${ extension } ` ;
174
+ if ( ! dummy . has ( filename ) ) {
175
+ fs . unlink ( filename , err => { /* nop */ } ) ;
176
+ continue ;
177
+ }
178
+ fs . writeFileSync ( filename , dummy . get ( filename ) ) ;
179
+ console . log ( " " + stdoutColors . yellow ( `Created fixture ${ filename } ` ) ) ;
180
+ }
181
+ compareFixture . end ( SKIPPED ) ;
182
+ return ;
183
+ }
184
+
185
+ // Displaying the diffs in console for release fixtures isn't usually
186
+ // meaningful, so release fixtures are compared as if --noDiff was passed.
187
+ if ( args . noDiff || mode === "release" ) {
188
+ for ( const extension of fixtureExtensions ) {
189
+ const filename = `${ basename } .${ mode } .${ extension } ` ;
190
+ const actual = (
191
+ dummy . has ( filename ) &&
192
+ dummy . get ( filename ) . replace ( / \r \n / g, "\n" )
193
+ ) ;
194
+ const expected = (
195
+ fs . existsSync ( filename ) &&
196
+ fs . readFileSync ( filename , { encoding : "utf8" } ) . replace ( / \r \n / g, "\n" )
197
+ ) ;
198
+
199
+ // If a fixture/generated file is missing, false will be compared to a
200
+ // string. If both are missing, nothing happens below (as it should).
201
+ if ( actual !== expected ) {
202
+ compareFixture . end ( FAILURE ) ;
203
+ return prepareResult ( FAILURE , "fixture mismatch" ) ;
204
+ }
205
+ }
206
+ compareFixture . end ( SUCCESS ) ;
207
+ return ;
208
+ }
209
+
210
+ let failed = false ;
211
+
212
+ for ( const extension of fixtureExtensions ) {
213
+ const filename = `${ basename } .${ mode } .${ extension } ` ;
214
+ const actualExists = dummy . has ( filename ) ;
215
+ const expectedExists = fs . existsSync ( filename ) ;
216
+
217
+ if ( ! actualExists && ! expectedExists ) {
218
+ // Neither exists, which is perfectly fine. Carry on.
219
+ continue ;
220
+ } else if ( actualExists != expectedExists ) {
221
+ const message = actualExists
222
+ ? `Fixture ${ filename } is missing!`
223
+ : `File ${ filename } was not generated!` ;
224
+
225
+ console . log ( " " + stdoutColors . yellow ( message ) ) ;
226
+ failed = true ;
227
+ continue ;
228
+ }
229
+
230
+ const actual = dummy . has ( filename ) && dummy . get ( filename ) . replace ( / \r \n / g, "\n" ) ;
231
+ const expected = (
232
+ fs . existsSync ( filename ) &&
233
+ fs . readFileSync ( filename , { encoding : "utf8" } ) . replace ( / \r \n / g, "\n" )
234
+ ) ;
235
+
236
+ const diffResult = diff ( filename , expected , actual ) ;
237
+ if ( diffResult !== null ) {
238
+ console . log ( diffResult ) ;
239
+ failed = true ;
240
+ }
241
+ }
242
+
243
+ if ( failed ) {
244
+ compareFixture . end ( FAILURE ) ;
245
+ return prepareResult ( FAILURE , "fixture mismatch" ) ;
246
+ }
247
+ compareFixture . end ( SUCCESS ) ;
248
+ }
249
+
150
250
if ( config . features ) {
151
251
config . features . forEach ( feature => {
152
252
if ( ! features . includes ( feature ) && ! features . includes ( "*" ) ) {
@@ -176,15 +276,14 @@ async function runTest(basename) {
176
276
{
177
277
const cmd = [
178
278
basename + ".ts" ,
179
- "--baseDir" , basedir ,
180
279
"--debug" ,
181
- "--textFile" // -> stdout
280
+ "--outFile" , basename + ".debug.wasm" ,
281
+ "--textFile" , basename + ".debug.wat"
182
282
] ;
183
283
if ( asc_flags ) cmd . push ( ...asc_flags ) ;
184
- cmd . push ( "--outFile" , basename + ".debug.wasm" ) ;
185
284
if ( args . noColors ) cmd . push ( "--noColors" ) ;
186
285
const compileDebug = section ( "compile debug" ) ;
187
- const { error } = await asc . main ( cmd , { stdout, stderr } ) ;
286
+ const { error } = await asc . main ( cmd , { stdout, stderr, writeFile } ) ;
188
287
189
288
let expectStderr = config . stderr ;
190
289
if ( error ) {
@@ -224,50 +323,28 @@ async function runTest(basename) {
224
323
return prepareResult ( SUCCESS ) ;
225
324
}
226
325
227
- const compareFixture = section ( "compare fixture" ) ;
228
- const actual = stdout . toString ( ) . replace ( / \r \n / g, "\n" ) ;
229
- if ( args . create ) {
230
- fs . writeFileSync ( path . join ( basedir , basename + ".debug.wat" ) , actual , { encoding : "utf8" } ) ;
231
- console . log ( " " + stdoutColors . yellow ( "Created fixture" ) ) ;
232
- compareFixture . end ( SKIPPED ) ;
233
- } else {
234
- const expected = fs . readFileSync ( path . join ( basedir , basename + ".debug.wat" ) , { encoding : "utf8" } ) . replace ( / \r \n / g, "\n" ) ;
235
- if ( args . noDiff ) {
236
- if ( expected != actual ) {
237
- compareFixture . end ( FAILURE ) ;
238
- return prepareResult ( FAILURE , "fixture mismatch" ) ;
239
- }
240
- } else {
241
- let diffs = diff ( basename + ".debug.wat" , expected , actual ) ;
242
- if ( diffs !== null ) {
243
- console . log ( diffs ) ;
244
- compareFixture . end ( FAILURE ) ;
245
- return prepareResult ( FAILURE , "fixture mismatch" ) ;
246
- }
247
- }
248
- compareFixture . end ( SUCCESS ) ;
249
- }
326
+ const afterCompileResult = afterCompile ( "debug" ) ;
327
+ if ( afterCompileResult ) return afterCompileResult ;
250
328
}
251
329
252
330
stdout . length = 0 ;
253
331
stderr . length = 0 ;
254
332
255
- const gluePath = path . join ( basedir , basename + ".js" ) ;
333
+ const gluePath = basename + ".js" ;
256
334
const glue = fs . existsSync ( gluePath ) ? await import ( pathToFileURL ( gluePath ) ) : { } ;
257
335
258
336
// Build release
259
337
{
260
338
const cmd = [
261
339
basename + ".ts" ,
262
- "--baseDir " , basedir ,
263
- "--outFile " , // -> stdout
340
+ "--outFile " , basename + ".release.wasm" ,
341
+ "--textFile " , basename + ".release.wat" ,
264
342
"-O"
265
343
] ;
266
344
if ( asc_flags ) cmd . push ( ...asc_flags ) ;
267
- if ( args . create ) cmd . push ( "--textFile" , basename + ".release.wat" ) ;
268
345
if ( args . noColors ) cmd . push ( "--noColors" ) ;
269
346
const compileRelease = section ( "compile release" ) ;
270
- const { error } = await asc . main ( cmd , { stdout : stdout , stderr : stderr } ) ;
347
+ const { error } = await asc . main ( cmd , { stdout, stderr, writeFile } ) ;
271
348
272
349
if ( error ) {
273
350
stderr . write ( "---\n" ) ;
@@ -278,15 +355,18 @@ async function runTest(basename) {
278
355
}
279
356
compileRelease . end ( SUCCESS ) ;
280
357
358
+ const afterCompileResult = afterCompile ( "release" ) ;
359
+ if ( afterCompileResult ) return afterCompileResult ;
360
+
281
361
if ( missing_features . length ) {
282
362
console . log ( "- " + stdoutColors . yellow ( "instantiate SKIPPED" ) + ": " + missing_features . join ( ", " ) + " not enabled\n" ) ;
283
363
return prepareResult ( SKIPPED , "feature not enabled: " + missing_features . join ( ", " ) ) ;
284
364
} else if ( v8_flags ) {
285
365
v8 . setFlagsFromString ( v8_flags ) ;
286
366
}
287
367
288
- const debugBuffer = fs . readFileSync ( path . join ( basedir , basename + ".debug.wasm" ) ) ;
289
- const releaseBuffer = stdout . toBuffer ( ) ;
368
+ const debugBuffer = dummy . get ( basename + ".debug.wasm" ) ;
369
+ const releaseBuffer = dummy . get ( basename + ".release.wasm" ) ;
290
370
const instantiateDebug = section ( "instantiate debug" ) ;
291
371
if ( config . skipInstantiate ) {
292
372
instantiateDebug . end ( SKIPPED ) ;
@@ -313,7 +393,6 @@ async function runTest(basename) {
313
393
if ( asc_rtrace ) {
314
394
const cmd = [
315
395
basename + ".ts" ,
316
- "--baseDir" , basedir ,
317
396
"--outFile" , // -> stdout
318
397
"--debug" ,
319
398
"--use" , "ASC_RTRACE=1" ,
0 commit comments