Skip to content

Commit 982b2d0

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent ad8996c commit 982b2d0

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

main.go

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -675,38 +675,49 @@ func usage() {
675675
flag.PrintDefaults()
676676
}
677677

678+
// printCompilerError prints compiler errors using the provided logger function
679+
// (similar to fmt.Println).
680+
//
681+
// There is one exception: interp errors may print to stderr unconditionally due
682+
// to limitations in the LLVM bindings.
683+
func printCompilerError(logln func(...interface{}), err error) {
684+
switch err := err.(type) {
685+
case *interp.Unsupported:
686+
// hit an unknown/unsupported instruction
687+
logln("#", err.ImportPath)
688+
msg := "unsupported instruction during init evaluation:"
689+
if err.Pos.String() != "" {
690+
msg = err.Pos.String() + " " + msg
691+
}
692+
logln(msg)
693+
err.Inst.Dump()
694+
logln()
695+
case types.Error, scanner.Error:
696+
logln(err)
697+
case interp.Error:
698+
logln("#", err.ImportPath)
699+
for _, err := range err.Errs {
700+
logln(err)
701+
}
702+
case loader.Errors:
703+
logln("#", err.Pkg.ImportPath)
704+
for _, err := range err.Errs {
705+
logln(err)
706+
}
707+
case *builder.MultiError:
708+
for _, err := range err.Errs {
709+
logln(err)
710+
}
711+
default:
712+
logln("error:", err)
713+
}
714+
}
715+
678716
func handleCompilerError(err error) {
679717
if err != nil {
680-
switch err := err.(type) {
681-
case *interp.Unsupported:
682-
// hit an unknown/unsupported instruction
683-
fmt.Fprintln(os.Stderr, "#", err.ImportPath)
684-
msg := "unsupported instruction during init evaluation:"
685-
if err.Pos.String() != "" {
686-
msg = err.Pos.String() + " " + msg
687-
}
688-
fmt.Fprintln(os.Stderr, msg)
689-
err.Inst.Dump()
690-
fmt.Fprintln(os.Stderr)
691-
case types.Error, scanner.Error:
692-
fmt.Fprintln(os.Stderr, err)
693-
case interp.Error:
694-
fmt.Fprintln(os.Stderr, "#", err.ImportPath)
695-
for _, err := range err.Errs {
696-
fmt.Fprintln(os.Stderr, err)
697-
}
698-
case loader.Errors:
699-
fmt.Fprintln(os.Stderr, "#", err.Pkg.ImportPath)
700-
for _, err := range err.Errs {
701-
fmt.Fprintln(os.Stderr, err)
702-
}
703-
case *builder.MultiError:
704-
for _, err := range err.Errs {
705-
fmt.Fprintln(os.Stderr, err)
706-
}
707-
default:
708-
fmt.Fprintln(os.Stderr, "error:", err)
709-
}
718+
printCompilerError(func(args ...interface{}) {
719+
fmt.Fprintln(os.Stderr, args...)
720+
}, err)
710721
os.Exit(1)
711722
}
712723
}

main_test.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"time"
1818

1919
"github.com/tinygo-org/tinygo/compileopts"
20-
"github.com/tinygo-org/tinygo/loader"
2120
)
2221

2322
const TESTDATA = "testdata"
@@ -131,13 +130,7 @@ func runTest(path, target string, t *testing.T) {
131130
binary := filepath.Join(tmpdir, "test")
132131
err = runBuild("./"+path, binary, config)
133132
if err != nil {
134-
if errLoader, ok := err.(loader.Errors); ok {
135-
for _, err := range errLoader.Errs {
136-
t.Log("failed to build:", err)
137-
}
138-
} else {
139-
t.Log("failed to build:", err)
140-
}
133+
printCompilerError(t.Log, err)
141134
t.Fail()
142135
return
143136
}

0 commit comments

Comments
 (0)