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.
Этот коммит содержится в:
Ayke van Laethem 2019-11-07 15:50:49 +01:00 коммит произвёл Ron Evans
родитель 16fbf53ae2
коммит fb39e0917b

Просмотреть файл

@ -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))
}
})