forked from getsentry/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacktrace_test.go
84 lines (70 loc) · 2.58 KB
/
stacktrace_test.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package sentry
import (
"path/filepath"
"runtime"
"testing"
)
func TestFunctionName(t *testing.T) {
for _, test := range []struct {
skip int
pack string
name string
}{
{0, "sentry-go", "TestFunctionName"},
{1, "testing", "tRunner"},
{2, "runtime", "goexit"},
{100, "", ""},
} {
pc, _, _, _ := runtime.Caller(test.skip)
pack, name := deconstructFunctionName(runtime.FuncForPC(pc).Name())
// Go1.10 reports different paths than Modules aware versions (>=1.11)
assertStringContains(t, pack, test.pack)
assertEqual(t, name, test.name)
}
}
func TestNewStacktrace(t *testing.T) {
stacktrace := Trace()
assertEqual(t, len(stacktrace.Frames), 2)
assertEqual(t, stacktrace.Frames[0].Function, "TestNewStacktrace")
assertEqual(t, stacktrace.Frames[1].Function, "Trace")
}
func TestStacktraceFrame(t *testing.T) {
_, callerFile, _, _ := runtime.Caller(0)
dir, _ := filepath.Split(callerFile)
stacktrace := Trace()
frame := stacktrace.Frames[len(stacktrace.Frames)-1]
assertEqual(t, frame.Filename, "errors_test.go")
assertEqual(t, frame.AbsPath, filepath.Join(dir, "errors_test.go"))
assertEqual(t, frame.Function, "Trace")
assertEqual(t, frame.Lineno, 12)
assertEqual(t, frame.InApp, true)
// Go1.10 reports different Module than Modules aware versions (>=1.11)
assertStringContains(t, frame.Module, "sentry-go")
}
// https://github.com/pkg/errors
func TestExtractStacktracePkgErrors(t *testing.T) {
err := RedPkgErrorsRanger()
stacktrace := ExtractStacktrace(err)
assertEqual(t, len(stacktrace.Frames), 3)
assertEqual(t, stacktrace.Frames[0].Function, "TestExtractStacktracePkgErrors")
assertEqual(t, stacktrace.Frames[1].Function, "RedPkgErrorsRanger")
assertEqual(t, stacktrace.Frames[2].Function, "BluePkgErrorsRanger")
}
// https://github.com/pingcap/errors
func TestExtractStacktracePingcapErrors(t *testing.T) {
err := RedPingcapErrorsRanger()
stacktrace := ExtractStacktrace(err)
assertEqual(t, len(stacktrace.Frames), 3)
assertEqual(t, stacktrace.Frames[0].Function, "TestExtractStacktracePingcapErrors")
assertEqual(t, stacktrace.Frames[1].Function, "RedPingcapErrorsRanger")
assertEqual(t, stacktrace.Frames[2].Function, "BluePingcapErrorsRanger")
}
// https://github.com/go-errors/errors
func TestExtractStacktraceGoErrors(t *testing.T) {
err := RedGoErrorsRanger()
stacktrace := ExtractStacktrace(err)
assertEqual(t, len(stacktrace.Frames), 3)
assertEqual(t, stacktrace.Frames[0].Function, "TestExtractStacktraceGoErrors")
assertEqual(t, stacktrace.Frames[1].Function, "RedGoErrorsRanger")
assertEqual(t, stacktrace.Frames[2].Function, "BlueGoErrorsRanger")
}