Skip to content

Commit

Permalink
feat: improve panic stack trace (#96)
Browse files Browse the repository at this point in the history
Signed-off-by: Tronje Krop <[email protected]>
  • Loading branch information
tkrop authored Oct 30, 2024
1 parent d8cf69e commit d85a71f
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jobs:
uses: actions/setup-go@v5
with:
go-version: 1.23.2
cache: false

- name: Checkout code
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export GOPATH ?= $(shell $(GO) env GOPATH)
export GOBIN ?= $(GOPATH)/bin

# Setup go-make version to use desired build and config scripts.
GOMAKE_DEP ?= github.com/tkrop/[email protected].106
GOMAKE_DEP ?= github.com/tkrop/[email protected].108
INSTALL_FLAGS ?= -mod=readonly -buildvcs=auto
# Request targets from go-make targets target.
TARGETS := $(shell command -v $(GOBIN)/go-make >/dev/null || \
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.21
0.0.22
16 changes: 7 additions & 9 deletions test/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package test

import (
"math"
"regexp"
"runtime"
"runtime/debug"
"strings"
gosync "sync"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -450,10 +450,10 @@ func (t *Context) Failed() bool {
return t.failed.Load()
}

// Offset fr original stack in case of panic handling.
//
// TODO: check offset or/and find a better solution to handle panic stack.
const panicOriginStackOffset = 10
// regexPanic is a regular expression to extract the actual important panic
// stack trace removing the distracting parts from the test framework.
var regexPanic = regexp.MustCompile(`(?m)\nruntime\/debug\.Stack\(\)` +
`(\n|.)*runtime\/panic\.go:([0-9]+)[^\n]*\n`)

// Panic handles failure notifications of panics that also abort the test
// execution immediately.
Expand All @@ -469,10 +469,8 @@ func (t *Context) Panic(arg any) {
defer t.unlock()

if t.expect == Success {
stack := strings.SplitN(string(debug.Stack()),
"\n", panicOriginStackOffset)
t.Fatalf("panic: %v\n%s\n%s", arg, stack[0],
stack[panicOriginStackOffset-1])
stack := regexPanic.Split(string(debug.Stack()), -1)
t.Fatalf("panic: %v\n%s\n%s", arg, stack[0], stack[1])
} else if t.reporter != nil {
t.reporter.Panic(arg)
}
Expand Down
1 change: 0 additions & 1 deletion test/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ type TestDeadlineParam struct {
failure test.Expect
}

// TODO: fix unit test for deadline!!
var TestDeadlineParams = map[string]TestDeadlineParam{
"failed": {
time: 0,
Expand Down

0 comments on commit d85a71f

Please sign in to comment.