-
Notifications
You must be signed in to change notification settings - Fork 213
/
stacktrace_external_test.go
307 lines (284 loc) · 7.63 KB
/
stacktrace_external_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package sentry_test
import (
"path/filepath"
"testing"
goErrors "github.com/go-errors/errors"
pingcapErrors "github.com/pingcap/errors"
pkgErrors "github.com/pkg/errors"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/getsentry/sentry-go"
)
func f1() *sentry.Stacktrace {
return sentry.NewStacktrace()
}
func f2() *sentry.Stacktrace {
return f1()
}
func f3() *sentry.Stacktrace {
return sentry.NewStacktraceForTest()
}
func RedPkgErrorsRanger() error {
return BluePkgErrorsRanger()
}
func BluePkgErrorsRanger() error {
return pkgErrors.New("this is bad from pkgErrors")
}
func RedPingcapErrorsRanger() error {
return BluePingcapErrorsRanger()
}
func BluePingcapErrorsRanger() error {
return pingcapErrors.New("this is bad from pingcapErrors")
}
func RedGoErrorsRanger() error {
return BlueGoErrorsRanger()
}
func BlueGoErrorsRanger() error {
return goErrors.New("this is bad from goErrors")
}
// nolint: scopelint // false positive https://github.com/kyoh86/scopelint/issues/4
func TestNewStacktrace(t *testing.T) {
tests := map[string]struct {
f func() *sentry.Stacktrace
want *sentry.Stacktrace
}{
"f1": {f1, &sentry.Stacktrace{
Frames: []sentry.Frame{
{
Function: "f1",
Module: "github.com/getsentry/sentry-go_test",
Lineno: 18,
InApp: true,
},
},
}},
"f2": {f2, &sentry.Stacktrace{
Frames: []sentry.Frame{
{
Function: "f2",
Module: "github.com/getsentry/sentry-go_test",
Lineno: 22,
InApp: true,
},
{
Function: "f1",
Module: "github.com/getsentry/sentry-go_test",
Lineno: 18,
InApp: true,
},
},
}},
// test that functions in the SDK that call NewStacktrace are not part
// of the resulting Stacktrace.
"NewStacktraceForTest": {sentry.NewStacktraceForTest, &sentry.Stacktrace{
Frames: []sentry.Frame{},
}},
"f3": {f3, &sentry.Stacktrace{
Frames: []sentry.Frame{
{
Function: "f3",
Module: "github.com/getsentry/sentry-go_test",
Lineno: 25,
InApp: true,
},
},
}},
"StacktraceTestHelper": {sentry.StacktraceTestHelper{}.NewStacktrace, &sentry.Stacktrace{
Frames: []sentry.Frame{},
}},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := tt.f()
compareStacktrace(t, got, tt.want)
})
}
}
// nolint: scopelint // false positive https://github.com/kyoh86/scopelint/issues/4
func TestExtractStacktrace(t *testing.T) {
tests := map[string]struct {
f func() error
want *sentry.Stacktrace
}{
// https://github.com/pkg/errors
"pkg/errors": {RedPkgErrorsRanger, &sentry.Stacktrace{
Frames: []sentry.Frame{
{
Function: "RedPkgErrorsRanger",
Module: "github.com/getsentry/sentry-go_test",
Lineno: 29,
InApp: true,
},
{
Function: "BluePkgErrorsRanger",
Module: "github.com/getsentry/sentry-go_test",
Lineno: 33,
InApp: true,
},
},
}},
// https://github.com/pingcap/errors
"pingcap/errors": {RedPingcapErrorsRanger, &sentry.Stacktrace{
Frames: []sentry.Frame{
{
Function: "RedPingcapErrorsRanger",
Module: "github.com/getsentry/sentry-go_test",
Lineno: 37,
InApp: true,
},
{
Function: "BluePingcapErrorsRanger",
Module: "github.com/getsentry/sentry-go_test",
Lineno: 41,
InApp: true,
},
},
}},
// https://github.com/go-errors/errors
"go-errors/errors": {RedGoErrorsRanger, &sentry.Stacktrace{
Frames: []sentry.Frame{
{
Function: "RedGoErrorsRanger",
Module: "github.com/getsentry/sentry-go_test",
Lineno: 45,
InApp: true,
},
{
Function: "BlueGoErrorsRanger",
Module: "github.com/getsentry/sentry-go_test",
Lineno: 49,
InApp: true,
},
},
}},
// FIXME: The tests below are commented out to avoid introducing a
// dependency on golang.org/x/xerrors. We should enable them when we
// move tests with external dependencies to a separate module.
// See https://github.com/getsentry/sentry-go/issues/238.
//
// https://golang.org/x/xerrors
// "xerrors.errorString": {
// func() error {
// err := xerrors.New("xerror")
// errType := reflect.TypeOf(err).String()
// if errType != "*xerrors.errorString" {
// panic("unexpected error type: " + errType)
// }
// return err
// },
// &sentry.Stacktrace{
// Frames: []sentry.Frame{
// {
// Function: "TestExtractStacktrace.func1",
// Module: "github.com/getsentry/sentry-go_test",
// Lineno: 178,
// InApp: true,
// },
// },
// },
// },
// "xerrors.wrapError": {
// func() error {
// err := xerrors.Errorf("new error: %w", xerrors.New("xerror"))
// errType := reflect.TypeOf(err).String()
// if errType != "*xerrors.wrapError" {
// panic("unexpected error type: " + errType)
// }
// return err
// },
// &sentry.Stacktrace{
// Frames: []sentry.Frame{
// {
// Function: "TestExtractStacktrace.func2",
// Module: "github.com/getsentry/sentry-go_test",
// Lineno: 198,
// InApp: true,
// },
// },
// },
// },
// "xerrors.noWrapError": {
// func() error {
// err := xerrors.Errorf("new error: %w", "not an xerror")
// errType := reflect.TypeOf(err).String()
// if errType != "*xerrors.noWrapError" {
// panic("unexpected error type: " + errType)
// }
// return err
// },
// &sentry.Stacktrace{
// Frames: []sentry.Frame{
// {
// Function: "TestExtractStacktrace.func3",
// Module: "github.com/getsentry/sentry-go_test",
// Lineno: 218,
// InApp: true,
// },
// },
// },
// },
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
err := tt.f()
if err == nil {
t.Fatal("got nil error")
}
got := sentry.ExtractStacktrace(err)
compareStacktrace(t, got, tt.want)
// We ignore paths in compareStacktrace because they depend on the
// environment where tests are run. However, Frame.Filename should
// be a relative path and Frame.AbsPath should be an absolute path.
for _, frame := range got.Frames {
if !filepath.IsAbs(frame.AbsPath) {
t.Errorf("got %q, want absolute path", frame.AbsPath)
}
if filepath.IsAbs(frame.Filename) {
t.Errorf("got %q, want relative path", frame.Filename)
}
}
})
}
}
func BenchmarkExtractStacktrace(b *testing.B) {
var fs = []func() error{
RedPkgErrorsRanger,
RedPingcapErrorsRanger,
RedGoErrorsRanger,
}
for i := 0; i < b.N; i++ {
f := fs[i%len(fs)]
err := f()
sentry.ExtractStacktrace(err)
}
}
func compareStacktrace(t *testing.T, got, want *sentry.Stacktrace) {
t.Helper()
if got == nil {
t.Fatal("got nil stack trace")
}
if len(got.Frames) == 0 {
t.Fatal("got no frames")
}
// Skip anonymous function passed to t.Run.
got.Frames = got.Frames[1:]
if diff := stacktraceDiff(want, got); diff != "" {
t.Fatalf("Stacktrace mismatch (-want +got):\n%s", diff)
}
// Because stacktraceDiff ignores Frame.AbsPath, sanity check that the
// values we got are actually absolute paths pointing to this test file.
for _, frame := range got.Frames {
if !filepath.IsAbs(frame.AbsPath) {
t.Errorf("Frame{Function: %q}.AbsPath = %q, want absolute path", frame.Function, frame.AbsPath)
}
if filepath.Base(frame.AbsPath) != "stacktrace_external_test.go" {
t.Errorf(`Frame{Function: %q}.AbsPath = %q, want ".../stacktrace_external_test.go"`, frame.Function, frame.AbsPath)
}
}
}
func stacktraceDiff(x, y *sentry.Stacktrace) string {
return cmp.Diff(
x, y,
cmpopts.IgnoreFields(sentry.Frame{}, "AbsPath", "Filename"),
)
}