-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathstacktrace_test.go
More file actions
288 lines (231 loc) · 7.84 KB
/
stacktrace_test.go
File metadata and controls
288 lines (231 loc) · 7.84 KB
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
package oops
import (
"errors"
"runtime"
"runtime/debug"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func a() *oopsStacktrace {
return b()
}
func b() *oopsStacktrace {
return c()
}
func c() *oopsStacktrace {
return d()
}
func d() *oopsStacktrace {
return e()
}
func e() *oopsStacktrace {
return f()
}
func f() *oopsStacktrace {
return newStacktrace("1234", 0)
}
func TestStacktrace(t *testing.T) {
is := assert.New(t)
t.Parallel()
st := a()
is.NotNil(st)
is.Equal("1234", st.span)
bi, ok := debug.ReadBuildInfo()
is.True(ok)
if !strings.Contains(bi.Path, "github.com/samber/oops") {
t.Skip("This test is meant to run on oops main repo")
}
path := strings.Replace(bi.Path, ".test", "", 1) // starting go1.24, go adds ".test" to the path when running tests
if st.frames != nil {
for _, f := range st.frames {
is.Contains(f.file, path, "frame file %s should contain %s", f.file, path)
}
is.Len(st.frames, 7, "expected 7 frames")
if len(st.frames) == 7 {
is.Equal("f", (st.frames)[0].function)
is.Equal("e", (st.frames)[1].function)
is.Equal("d", (st.frames)[2].function)
is.Equal("c", (st.frames)[3].function)
is.Equal("b", (st.frames)[4].function)
is.Equal("a", (st.frames)[5].function)
is.Equal("TestStacktrace", (st.frames)[6].function)
}
}
}
func TestShortFuncNameExtended(t *testing.T) {
is := assert.New(t)
t.Parallel()
// Test with a real function
pc, _, _, ok := runtime.Caller(0)
is.True(ok)
f := runtime.FuncForPC(pc)
is.NotNil(f)
result := shortFuncName(f.Name())
is.NotEmpty(result)
is.Contains(result, "TestShortFuncNameExtended")
// Test with empty function name
result2 := shortFuncName("")
is.Empty(result2)
}
func TestOopsStacktraceError(t *testing.T) {
is := assert.New(t)
t.Parallel()
// Test stacktrace Error method - returns formatted stacktrace, not span
st := &oopsStacktrace{span: "test"}
err := st.Error()
is.Empty(err) // Empty because no frames
// Test with frames
frame := oopsStacktraceFrame{
file: "test.go",
line: 10,
function: "testFunc",
}
st2 := &oopsStacktrace{span: "test", frames: []oopsStacktraceFrame{frame}}
err2 := st2.Error()
is.Contains(err2, "test.go:10 testFunc()")
}
func TestOopsStacktraceString(t *testing.T) {
is := assert.New(t)
t.Parallel()
// Test with empty frames
st := &oopsStacktrace{span: "test", frames: []oopsStacktraceFrame{}}
result := st.String("")
is.Empty(result)
// Test with frames
frame := oopsStacktraceFrame{
file: "test.go",
line: 10,
function: "testFunc",
}
st2 := &oopsStacktrace{span: "test", frames: []oopsStacktraceFrame{frame}}
result2 := st2.String("")
is.Contains(result2, "test.go:10 testFunc()")
// Test with deepest frame
result3 := st2.String("test.go:10 testFunc()")
is.Empty(result3)
}
func TestOopsStacktraceFrameString(t *testing.T) {
is := assert.New(t)
t.Parallel()
// Test with function
frame := &oopsStacktraceFrame{
file: "test.go",
line: 10,
function: "testFunc",
}
result := frame.String()
is.Equal("test.go:10 testFunc()", result)
// Test without function
frame2 := &oopsStacktraceFrame{
file: "test.go",
line: 10,
}
result2 := frame2.String()
is.Equal("test.go:10", result2)
}
// helperWrap wraps oops.Wrap without any caller skip — it should appear in the stack trace.
func helperWrap(err error) error {
return Wrap(err)
}
// helperWrapSkip wraps oops.CallerSkip(1).Wrap — it skips 1 user frame (itself),
// so helperWrapSkip itself should NOT appear in the stack trace.
// CallerSkip(1) means "skip 1 user frame", where the base offset already accounts
// for runtime.Callers → newStacktrace → builder_method (internalFrameDepth=3).
func helperWrapSkip(err error) error {
return CallerSkip(1).Wrap(err)
}
// frameNames returns the list of short function names from an OopsError's stack trace,
// applying output-time FrameSkip filtering (via StackFrames()).
func frameNames(err error) []string {
oopsErr, ok := err.(OopsError)
if !ok {
return nil
}
frames := oopsErr.StackFrames()
names := make([]string, 0, len(frames))
for _, f := range frames {
names = append(names, f.Function)
}
return names
}
// --- CallerSkip tests ---
func TestCallerSkip_NoSkip(t *testing.T) {
is := assert.New(t)
t.Parallel()
base := errors.New("base")
err := helperWrap(base)
names := frameNames(err)
is.NotEmpty(names)
is.Contains(names, "helperWrap", "helperWrap should appear in frames when no skip is applied")
}
func TestCallerSkip_SkipOne(t *testing.T) {
is := assert.New(t)
t.Parallel()
base := errors.New("base")
err := helperWrapSkip(base)
names := frameNames(err)
is.NotEmpty(names)
is.NotContains(names, "helperWrapSkip", "helperWrapSkip should NOT appear in frames when CallerSkip(1) is used")
// The test function itself should be the first user frame instead
is.Contains(names, "TestCallerSkip_SkipOne", "the calling test function should appear in frames")
}
func TestCallerSkip_LastCallWins(t *testing.T) {
is := assert.New(t)
t.Parallel()
base := errors.New("base")
// Chain CallerSkip(100) then CallerSkip(0): the last call should win (skip=0).
// With skip=0, no user frames are skipped, so the test function itself should appear.
// With skip=100, all frames would be skipped and the test function would not appear.
// The test function's presence here confirms that skip=0 (the last value) was used,
// not skip=100.
err := CallerSkip(100).CallerSkip(0).Wrap(base)
names := frameNames(err)
is.NotEmpty(names)
is.Contains(names, "TestCallerSkip_LastCallWins", "with skip=0 the test function should still be in frames — confirms last CallerSkip wins")
}
// --- FrameSkip tests ---
// gWrapper is a helper used by TestFrameSkip_ByFunction to verify function-name filtering.
func gWrapper(err error) error {
return Wrap(err)
}
func TestFrameSkip_ByFunction(t *testing.T) { //nolint:paralleltest
// Modifies global framesSkip — do not run in parallel.
is := assert.New(t)
originalFramesSkip := framesSkip
defer func() { framesSkip = originalFramesSkip }()
// Confirm gWrapper appears before registering the skip.
base := errors.New("base")
errBefore := gWrapper(base)
namesBefore := frameNames(errBefore)
is.Contains(namesBefore, "gWrapper", "gWrapper should appear before FrameSkip is registered")
// Create error BEFORE registering the skip — filtering should still apply at output time.
errCreatedBeforeSkip := gWrapper(base)
FrameSkip("", "gWrapper")
namesBeforeSkip := frameNames(errCreatedBeforeSkip)
is.NotContains(namesBeforeSkip, "gWrapper", "FrameSkip should apply to errors created before registration")
errAfter := gWrapper(base)
namesAfter := frameNames(errAfter)
is.NotContains(namesAfter, "gWrapper", "gWrapper should NOT appear after FrameSkip(\"\", \"gWrapper\") is registered")
}
func TestFrameSkip_ByFile(t *testing.T) { //nolint:paralleltest
// Modifies global framesSkip — do not run in parallel.
is := assert.New(t)
originalFramesSkip := framesSkip
defer func() { framesSkip = originalFramesSkip }()
// Get the raw file path as runtime.CallersFrames would report it.
_, thisFile, _, _ := runtime.Caller(0)
// Confirm helperWrap appears before registering the skip.
base := errors.New("base")
errBefore := helperWrap(base)
is.Contains(frameNames(errBefore), "helperWrap", "helperWrap should appear before FrameSkip is registered")
// Create error BEFORE registering the skip — filtering should still apply at output time.
errCreatedBeforeSkip := helperWrap(base)
// Register a skip for this test file (helperWrap lives in the same file).
FrameSkip(thisFile, "")
namesBeforeSkip := frameNames(errCreatedBeforeSkip)
is.NotContains(namesBeforeSkip, "helperWrap", "FrameSkip should apply to errors created before registration")
errAfter := helperWrap(base)
namesAfter := frameNames(errAfter)
is.NotContains(namesAfter, "helperWrap", "helperWrap should NOT appear after FrameSkip by file path")
}