transform: allow updating tests with -update flag

This should make it much easier to update existing tests.
This commit is contained in:
Ayke van Laethem
2020-04-01 20:49:48 +02:00
committed by Ron Evans
parent 5308e8903e
commit 584e94ce2f
5 changed files with 73 additions and 44 deletions
+24 -11
View File
@@ -3,6 +3,7 @@ package transform
// This file defines some helper functions for testing transforms.
import (
"flag"
"io/ioutil"
"os"
"strings"
@@ -11,6 +12,8 @@ import (
"tinygo.org/x/go-llvm"
)
var update = flag.Bool("update", false, "update transform package tests")
// testTransform runs a transformation pass on an input file (pathPrefix+".ll")
// and checks whether it matches the expected output (pathPrefix+".out.ll"). The
// output is compared with a fuzzy match that ignores some irrelevant lines such
@@ -31,18 +34,28 @@ func testTransform(t *testing.T, pathPrefix string, transform func(mod llvm.Modu
// Perform the transform.
transform(mod)
// Read the expected output IR.
out, err := ioutil.ReadFile(pathPrefix + ".out.ll")
if err != nil {
t.Fatalf("could not read output file %s: %v", pathPrefix+".out.ll", err)
}
// See whether the transform output matches with the expected output IR.
expected := string(out)
// Get the output from the test and filter some irrelevant lines.
actual := mod.String()
if !fuzzyEqualIR(expected, actual) {
t.Logf("output does not match expected output:\n%s", actual)
t.Fail()
actual = actual[strings.Index(actual, "\ntarget datalayout = ")+1:]
if *update {
err := ioutil.WriteFile(pathPrefix+".out.ll", []byte(actual), 0666)
if err != nil {
t.Error("failed to write out new output:", err)
}
} else {
// Read the expected output IR.
out, err := ioutil.ReadFile(pathPrefix + ".out.ll")
if err != nil {
t.Fatalf("could not read output file %s: %v", pathPrefix+".out.ll", err)
}
// See whether the transform output matches with the expected output IR.
expected := string(out)
if !fuzzyEqualIR(expected, actual) {
t.Logf("output does not match expected output:\n%s", actual)
t.Fail()
}
}
}