Skip to content

Commit 107be65

Browse files
committed
main_test: refactor output comparison into separate function
This shouldn't affect anything, just make the code a bit better (especially for the next commit).
1 parent 4720f35 commit 107be65

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

main_test.go

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -396,17 +396,13 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
396396
// of the path.
397397
path := TESTDATA + "/" + name
398398
// Get the expected output for this test.
399-
txtpath := path[:len(path)-3] + ".txt"
399+
expectedOutputPath := path[:len(path)-3] + ".txt"
400400
pkgName := "./" + path
401401
if path[len(path)-1] == '/' {
402-
txtpath = path + "out.txt"
402+
expectedOutputPath = path + "out.txt"
403403
options.Directory = path
404404
pkgName = "."
405405
}
406-
expected, err := os.ReadFile(txtpath)
407-
if err != nil {
408-
t.Fatal("could not read expected output file:", err)
409-
}
410406

411407
config, err := builder.NewConfig(&options)
412408
if err != nil {
@@ -428,10 +424,7 @@ func runTestWithConfig(name string, t *testing.T, options compileopts.Options, c
428424
return
429425
}
430426

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()
435428
if config.EmulatorName() == "simavr" {
436429
// Strip simavr log formatting.
437430
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
446439
}
447440

448441
// Check whether the command ran successfully.
449-
fail := false
450442
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)
457444
}
445+
checkOutput(t, expectedOutputPath, actual)
458446

459-
if fail {
447+
if t.Failed() {
460448
r := bufio.NewReader(bytes.NewReader(actual))
461449
for {
462450
line, err := r.ReadString('\n')
@@ -695,21 +683,27 @@ func TestWasmExport(t *testing.T) {
695683
// Check that the output matches the expected output.
696684
// (Skip this for wasm-unknown because it can't produce output).
697685
if !tc.noOutput {
698-
expectedOutput, err := os.ReadFile("testdata/wasmexport.txt")
699-
if err != nil {
700-
t.Fatal("could not read output file:", err)
701-
}
702-
actual := output.Bytes()
703-
expectedOutput = bytes.ReplaceAll(expectedOutput, []byte("\r\n"), []byte("\n"))
704-
actual = bytes.ReplaceAll(actual, []byte("\r\n"), []byte("\n"))
705-
if !bytes.Equal(actual, expectedOutput) {
706-
t.Error(string(Diff("expected", expectedOutput, "actual", actual)))
707-
}
686+
checkOutput(t, "testdata/wasmexport.txt", output.Bytes())
708687
}
709688
})
710689
}
711690
}
712691

692+
// Check whether the output of a test equals the expected output.
693+
func checkOutput(t *testing.T, filename string, actual []byte) {
694+
expectedOutput, err := os.ReadFile(filename)
695+
if err != nil {
696+
t.Fatal("could not read output file:", err)
697+
}
698+
expectedOutput = bytes.ReplaceAll(expectedOutput, []byte("\r\n"), []byte("\n"))
699+
actual = bytes.ReplaceAll(actual, []byte("\r\n"), []byte("\n"))
700+
701+
if !bytes.Equal(actual, expectedOutput) {
702+
t.Errorf("output did not match (expected %d bytes, got %d bytes):", len(expectedOutput), len(actual))
703+
t.Error(string(Diff("expected", expectedOutput, "actual", actual)))
704+
}
705+
}
706+
713707
func TestTest(t *testing.T) {
714708
t.Parallel()
715709

0 commit comments

Comments
 (0)