forked from getsentry/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacktrace_test.go
171 lines (162 loc) · 4.21 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package sentry
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
)
func NewStacktraceForTest() *Stacktrace {
return NewStacktrace()
}
type StacktraceTestHelper struct{}
func (StacktraceTestHelper) NewStacktrace() *Stacktrace {
return NewStacktrace()
}
func BenchmarkNewStacktrace(b *testing.B) {
for i := 0; i < b.N; i++ {
Trace()
}
}
// nolint: scopelint // false positive https://github.com/kyoh86/scopelint/issues/4
func TestSplitQualifiedFunctionName(t *testing.T) {
tests := []struct {
in string
pkg string
fun string
}{
{"", "", ""},
{"runtime.Callers", "runtime", "Callers"},
{"main.main.func1", "main", "main.func1"},
{
"github.com/getsentry/sentry-go.Init",
"github.com/getsentry/sentry-go",
"Init",
},
{
"github.com/getsentry/sentry-go.(*Hub).Flush",
"github.com/getsentry/sentry-go",
"(*Hub).Flush",
},
{
"github.com/getsentry/sentry-go.Test.func2.1.1",
"github.com/getsentry/sentry-go",
"Test.func2.1.1",
},
{
"github.com/getsentry/confusing%2epkg%2ewith%2edots.Test.func1",
"github.com/getsentry/confusing%2epkg%2ewith%2edots",
"Test.func1",
},
}
for _, tt := range tests {
t.Run(tt.in, func(t *testing.T) {
pkg, fun := splitQualifiedFunctionName(tt.in)
if diff := cmp.Diff(tt.pkg, pkg); diff != "" {
t.Errorf("Package name mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(tt.fun, fun); diff != "" {
t.Errorf("Function name mismatch (-want +got):\n%s", diff)
}
})
}
}
// nolint: scopelint // false positive https://github.com/kyoh86/scopelint/issues/4
func TestFilterFrames(t *testing.T) {
tests := []struct {
in []Frame
out []Frame
}{
// sanity check
{},
// filter out go internals and SDK internals; "sentry-go_test" is
// considered outside of the SDK and thus included (useful for testing)
{
in: []Frame{
{
Function: "goexit",
Module: "runtime",
AbsPath: "/goroot/src/runtime/asm_amd64.s",
InApp: false,
},
{
Function: "tRunner",
Module: "testing",
AbsPath: "/goroot/src/testing/testing.go",
InApp: false,
},
{
Function: "TestNewStacktrace.func1",
Module: "github.com/getsentry/sentry-go_test",
AbsPath: "/somewhere/sentry/sentry-go/stacktrace_external_test.go",
InApp: true,
},
{
Function: "StacktraceTestHelper.NewStacktrace",
Module: "github.com/getsentry/sentry-go",
AbsPath: "/somewhere/sentry/sentry-go/stacktrace_test.go",
InApp: true,
},
{
Function: "NewStacktrace",
Module: "github.com/getsentry/sentry-go",
AbsPath: "/somewhere/sentry/sentry-go/stacktrace.go",
InApp: true,
},
},
out: []Frame{
{
Function: "TestNewStacktrace.func1",
Module: "github.com/getsentry/sentry-go_test",
AbsPath: "/somewhere/sentry/sentry-go/stacktrace_external_test.go",
InApp: true,
},
},
},
// filter out integrations; SDK subpackages
{
in: []Frame{
{
Function: "Example.Integration",
Module: "github.com/getsentry/sentry-go/http/integration",
AbsPath: "/somewhere/sentry/sentry-go/http/integration/integration.go",
InApp: true,
},
{
Function: "(*Handler).Handle",
Module: "github.com/getsentry/sentry-go/http",
AbsPath: "/somewhere/sentry/sentry-go/http/sentryhttp.go",
InApp: true,
},
{
Function: "main",
Module: "main",
AbsPath: "/somewhere/example.com/pkg/main.go",
InApp: true,
},
},
out: []Frame{
{
Function: "main",
Module: "main",
AbsPath: "/somewhere/example.com/pkg/main.go",
InApp: true,
},
},
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
got := filterFrames(tt.in)
if diff := cmp.Diff(tt.out, got); diff != "" {
t.Errorf("filterFrames() mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestExtractXErrorsPC(t *testing.T) {
// This ensures that extractXErrorsPC does not break code that doesn't use
// golang.org/x/xerrors. For tests that check that it works on the
// appropriate type of errors, see stacktrace_external_test.go.
if got := extractXErrorsPC(errors.New("test")); got != nil {
t.Errorf("got %#v, want nil", got)
}
}