main: improve error reporting while running go test

This commit switches integration tests to use the same error reporting
mechanism as the tinygo compiler normally uses. It replaces errors like
this:

    main_test.go:139: failed to build: interp: branch on a non-constant

With this:

    main.go:693: # math/rand
    main.go:695: interp: branch on a non-constant

In this particular case the error isn't much better (it gives the
relevant package, though) but other errors should also include the
source location where they happen.
Этот коммит содержится в:
Ayke van Laethem 2020-03-03 17:55:57 +01:00 коммит произвёл Ron Evans
родитель ad8996c4ee
коммит 982b2d06ab
2 изменённых файлов: 42 добавлений и 38 удалений

35
main.go
Просмотреть файл

@ -675,38 +675,49 @@ func usage() {
flag.PrintDefaults() flag.PrintDefaults()
} }
func handleCompilerError(err error) { // printCompilerError prints compiler errors using the provided logger function
if err != nil { // (similar to fmt.Println).
//
// There is one exception: interp errors may print to stderr unconditionally due
// to limitations in the LLVM bindings.
func printCompilerError(logln func(...interface{}), err error) {
switch err := err.(type) { switch err := err.(type) {
case *interp.Unsupported: case *interp.Unsupported:
// hit an unknown/unsupported instruction // hit an unknown/unsupported instruction
fmt.Fprintln(os.Stderr, "#", err.ImportPath) logln("#", err.ImportPath)
msg := "unsupported instruction during init evaluation:" msg := "unsupported instruction during init evaluation:"
if err.Pos.String() != "" { if err.Pos.String() != "" {
msg = err.Pos.String() + " " + msg msg = err.Pos.String() + " " + msg
} }
fmt.Fprintln(os.Stderr, msg) logln(msg)
err.Inst.Dump() err.Inst.Dump()
fmt.Fprintln(os.Stderr) logln()
case types.Error, scanner.Error: case types.Error, scanner.Error:
fmt.Fprintln(os.Stderr, err) logln(err)
case interp.Error: case interp.Error:
fmt.Fprintln(os.Stderr, "#", err.ImportPath) logln("#", err.ImportPath)
for _, err := range err.Errs { for _, err := range err.Errs {
fmt.Fprintln(os.Stderr, err) logln(err)
} }
case loader.Errors: case loader.Errors:
fmt.Fprintln(os.Stderr, "#", err.Pkg.ImportPath) logln("#", err.Pkg.ImportPath)
for _, err := range err.Errs { for _, err := range err.Errs {
fmt.Fprintln(os.Stderr, err) logln(err)
} }
case *builder.MultiError: case *builder.MultiError:
for _, err := range err.Errs { for _, err := range err.Errs {
fmt.Fprintln(os.Stderr, err) logln(err)
} }
default: default:
fmt.Fprintln(os.Stderr, "error:", err) logln("error:", err)
} }
}
func handleCompilerError(err error) {
if err != nil {
printCompilerError(func(args ...interface{}) {
fmt.Fprintln(os.Stderr, args...)
}, err)
os.Exit(1) os.Exit(1)
} }
} }

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

@ -17,7 +17,6 @@ import (
"time" "time"
"github.com/tinygo-org/tinygo/compileopts" "github.com/tinygo-org/tinygo/compileopts"
"github.com/tinygo-org/tinygo/loader"
) )
const TESTDATA = "testdata" const TESTDATA = "testdata"
@ -131,13 +130,7 @@ func runTest(path, target string, t *testing.T) {
binary := filepath.Join(tmpdir, "test") binary := filepath.Join(tmpdir, "test")
err = runBuild("./"+path, binary, config) err = runBuild("./"+path, binary, config)
if err != nil { if err != nil {
if errLoader, ok := err.(loader.Errors); ok { printCompilerError(t.Log, err)
for _, err := range errLoader.Errs {
t.Log("failed to build:", err)
}
} else {
t.Log("failed to build:", err)
}
t.Fail() t.Fail()
return return
} }