/
testing_t_support.go
59 lines (48 loc) · 1.52 KB
/
testing_t_support.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package matchers
import (
"regexp"
"runtime/debug"
"strings"
"github.com/onsi/gomega"
"github.com/onsi/gomega/types"
)
// RegisterExtendedTestingT is like gomega.RegisterTestingT but it also
// omits any gomega frames from the printed stacktrace.
func RegisterExtendedTestingT(t types.GomegaTestingT) {
gomega.RegisterFailHandler(buildTestingTGomegaFailHandler(t))
}
func buildTestingTGomegaFailHandler(t types.GomegaTestingT) types.GomegaFailHandler {
return func(message string, callerSkip ...int) {
skip := 2 // initial runtime/debug.Stack frame + gomega-matchers.buildTestingTGomegaFailHandler frame
if len(callerSkip) > 0 {
skip += callerSkip[0]
}
stackTrace := pruneStack(string(debug.Stack()), skip)
stackTrace = strings.TrimSpace(stackTrace)
t.Fatalf("\n%s\n%s", stackTrace, message)
}
}
func pruneStack(fullStackTrace string, skip int) string {
stack := strings.Split(fullStackTrace, "\n")
if len(stack) > 1+2*skip {
stack = stack[1+2*skip:]
}
srcBlacklist := []string{
"testing.tRunner",
"created by testing.RunTests",
}
srcBlacklistRE := regexp.MustCompile(strings.Join(srcBlacklist, "|"))
suffix := regexp.MustCompile(` \+0x[0-9a-f]+$`)
trim := func(s string) string {
return suffix.ReplaceAllString(s, "")
}
prunedStack := []string{}
for i := 0; i < len(stack)/2; i++ {
if srcBlacklistRE.Match([]byte(stack[i*2])) {
continue
}
prunedStack = append(prunedStack, trim(stack[i*2]))
prunedStack = append(prunedStack, trim(stack[i*2+1]))
}
return strings.Join(prunedStack, "\n")
}