blob: ac8912525a89546bb101e5e41b22b7650e6b9672 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001package testingtsupport
2
3import (
4 "regexp"
5 "runtime/debug"
6 "strings"
7
8 "github.com/onsi/gomega/types"
9)
10
11type gomegaTestingT interface {
12 Fatalf(format string, args ...interface{})
13}
14
15func BuildTestingTGomegaFailHandler(t gomegaTestingT) types.GomegaFailHandler {
16 return func(message string, callerSkip ...int) {
17 skip := 1
18 if len(callerSkip) > 0 {
19 skip = callerSkip[0]
20 }
21 stackTrace := pruneStack(string(debug.Stack()), skip)
22 t.Fatalf("\n%s\n%s", stackTrace, message)
23 }
24}
25
26func pruneStack(fullStackTrace string, skip int) string {
27 stack := strings.Split(fullStackTrace, "\n")
28 if len(stack) > 2*(skip+1) {
29 stack = stack[2*(skip+1):]
30 }
31 prunedStack := []string{}
32 re := regexp.MustCompile(`\/ginkgo\/|\/pkg\/testing\/|\/pkg\/runtime\/`)
33 for i := 0; i < len(stack)/2; i++ {
34 if !re.Match([]byte(stack[i*2])) {
35 prunedStack = append(prunedStack, stack[i*2])
36 prunedStack = append(prunedStack, stack[i*2+1])
37 }
38 }
39 return strings.Join(prunedStack, "\n")
40}