From fb39e0917b025607b6689213eb3ac6b29c15793f Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 7 Nov 2019 15:50:49 +0100 Subject: [PATCH] cgo: add -update flag to tests When this flag is set, the testdata/*.out.go files will be updated when they have changed. This is very convenient for updating these files after the expected output changes. Of course, the updated output must still be checked for validity. --- cgo/cgo_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cgo/cgo_test.go b/cgo/cgo_test.go index 3034f012..410e7108 100644 --- a/cgo/cgo_test.go +++ b/cgo/cgo_test.go @@ -2,6 +2,7 @@ package cgo import ( "bytes" + "flag" "fmt" "go/ast" "go/format" @@ -14,6 +15,9 @@ import ( "testing" ) +// Pass -update to go test to update the output of the test files. +var flagUpdate = flag.Bool("update", false, "Update images based on test output.") + func TestCGo(t *testing.T) { var cflags = []string{"--target=armv6m-none-eabi"} @@ -74,6 +78,14 @@ func TestCGo(t *testing.T) { // Check whether the output is as expected. if expected != actual { // It is not. Test failed. + if *flagUpdate { + // Update the file with the expected data. + err := ioutil.WriteFile(outfile, []byte(actual), 0666) + if err != nil { + t.Error("could not write updated output file:", err) + } + return + } t.Errorf("output did not match:\n%s", string(actual)) } })