@@ -396,17 +396,13 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
396
396
// of the path.
397
397
path := TESTDATA + "/" + name
398
398
// Get the expected output for this test.
399
- txtpath := path [:len (path )- 3 ] + ".txt"
399
+ expectedOutputPath := path [:len (path )- 3 ] + ".txt"
400
400
pkgName := "./" + path
401
401
if path [len (path )- 1 ] == '/' {
402
- txtpath = path + "out.txt"
402
+ expectedOutputPath = path + "out.txt"
403
403
options .Directory = path
404
404
pkgName = "."
405
405
}
406
- expected , err := os .ReadFile (txtpath )
407
- if err != nil {
408
- t .Fatal ("could not read expected output file:" , err )
409
- }
410
406
411
407
config , err := builder .NewConfig (& options )
412
408
if err != nil {
@@ -428,10 +424,7 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
428
424
return
429
425
}
430
426
431
- // putchar() prints CRLF, convert it to LF.
432
- actual := bytes .Replace (stdout .Bytes (), []byte {'\r' , '\n' }, []byte {'\n' }, - 1 )
433
- expected = bytes .Replace (expected , []byte {'\r' , '\n' }, []byte {'\n' }, - 1 ) // for Windows
434
-
427
+ actual := stdout .Bytes ()
435
428
if config .EmulatorName () == "simavr" {
436
429
// Strip simavr log formatting.
437
430
actual = bytes .Replace (actual , []byte {0x1b , '[' , '3' , '2' , 'm' }, nil , - 1 )
@@ -446,17 +439,12 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
446
439
}
447
440
448
441
// Check whether the command ran successfully.
449
- fail := false
450
442
if err != nil {
451
- t .Log ("failed to run:" , err )
452
- fail = true
453
- } else if ! bytes .Equal (expected , actual ) {
454
- t .Logf ("output did not match (expected %d bytes, got %d bytes):" , len (expected ), len (actual ))
455
- t .Logf (string (Diff ("expected" , expected , "actual" , actual )))
456
- fail = true
443
+ t .Error ("failed to run:" , err )
457
444
}
445
+ checkOutput (t , expectedOutputPath , actual )
458
446
459
- if fail {
447
+ if t . Failed () {
460
448
r := bufio .NewReader (bytes .NewReader (actual ))
461
449
for {
462
450
line , err := r .ReadString ('\n' )
@@ -696,21 +684,27 @@ func TestWasmExport(t *testing.T) {
696
684
// Check that the output matches the expected output.
697
685
// (Skip this for wasm-unknown because it can't produce output).
698
686
if ! tc .noOutput {
699
- expectedOutput , err := os .ReadFile ("testdata/wasmexport.txt" )
700
- if err != nil {
701
- t .Fatal ("could not read output file:" , err )
702
- }
703
- actual := output .Bytes ()
704
- expectedOutput = bytes .ReplaceAll (expectedOutput , []byte ("\r \n " ), []byte ("\n " ))
705
- actual = bytes .ReplaceAll (actual , []byte ("\r \n " ), []byte ("\n " ))
706
- if ! bytes .Equal (actual , expectedOutput ) {
707
- t .Error (string (Diff ("expected" , expectedOutput , "actual" , actual )))
708
- }
687
+ checkOutput (t , "testdata/wasmexport.txt" , output .Bytes ())
709
688
}
710
689
})
711
690
}
712
691
}
713
692
693
+ // Check whether the output of a test equals the expected output.
694
+ func checkOutput (t * testing.T , filename string , actual []byte ) {
695
+ expectedOutput , err := os .ReadFile (filename )
696
+ if err != nil {
697
+ t .Fatal ("could not read output file:" , err )
698
+ }
699
+ expectedOutput = bytes .ReplaceAll (expectedOutput , []byte ("\r \n " ), []byte ("\n " ))
700
+ actual = bytes .ReplaceAll (actual , []byte ("\r \n " ), []byte ("\n " ))
701
+
702
+ if ! bytes .Equal (actual , expectedOutput ) {
703
+ t .Errorf ("output did not match (expected %d bytes, got %d bytes):" , len (expectedOutput ), len (actual ))
704
+ t .Error (string (Diff ("expected" , expectedOutput , "actual" , actual )))
705
+ }
706
+ }
707
+
714
708
func TestTest (t * testing.T ) {
715
709
t .Parallel ()
716
710
0 commit comments