Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cmd/gno): log panic information in gno test #1397

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"log"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"text/template"
Expand Down Expand Up @@ -439,8 +440,13 @@
printRuntimeMetrics bool,
runFlag string,
io commands.IO,
) error {
var errs error
) (errs error) {
defer func() {
if r := recover(); r != nil {
trace := smallStacktrace()
errs = multierr.Append(fmt.Errorf("panic: %v\ngno machine: %v\nstracktrace:\n%v", r, m.String(), trace), errs)
}

Check warning on line 448 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L446-L448

Added lines #L446 - L448 were not covered by tests
}()

testFuncs := &testFuncs{
PackageName: pkgName,
Expand Down Expand Up @@ -535,6 +541,34 @@
return errs
}

/*
smallStacktrace sample result:

prog.go:11 main.main
proc.go:250 runtime.main
asm_amd64.s:1598 runtime.goexit
*/
func smallStacktrace() string {
var buf bytes.Buffer
pc := make([]uintptr, 100)
pc = pc[:runtime.Callers(2, pc)]
frames := runtime.CallersFrames(pc)
for {
f, more := frames.Next()

if idx := strings.LastIndexByte(f.Function, '/'); idx >= 0 {
f.Function = f.Function[idx+1:]
}

Check warning on line 561 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L551-L561

Added lines #L551 - L561 were not covered by tests

fmt.Fprintf(&buf, "%-25s %s\n", fmt.Sprintf("%s:%d", filepath.Base(f.File), f.Line), f.Function)

if !more {
return buf.String()
}

Check warning on line 567 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L563-L567

Added lines #L563 - L567 were not covered by tests
}
return buf.String()

Check warning on line 569 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L569

Added line #L569 was not covered by tests
}

// mirror of stdlibs/testing.Report
type report struct {
Name string
Expand Down
Loading