-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacktrace_test.go
More file actions
91 lines (81 loc) · 2.56 KB
/
stacktrace_test.go
File metadata and controls
91 lines (81 loc) · 2.56 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
package logtide_test
import (
"errors"
"fmt"
"strings"
"testing"
logtide "github.com/logtide-dev/logtide-sdk-go"
)
func TestNewStacktraceHasFrames(t *testing.T) {
st := logtide.NewStacktrace(0)
if st == nil {
t.Fatal("NewStacktrace returned nil")
}
if len(st.Frames) == 0 {
t.Error("expected at least one frame")
}
}
func TestNewStacktraceStripsSDKFrames(t *testing.T) {
st := logtide.NewStacktrace(0)
for _, f := range st.Frames {
if strings.HasPrefix(f.Module, "github.com/logtide-dev/logtide-sdk-go") &&
!strings.HasPrefix(f.Module, "github.com/logtide-dev/logtide-sdk-go_test") {
t.Errorf("SDK-internal frame leaked: %s %s", f.Module, f.Function)
}
}
}
func TestNewStacktraceFrameFields(t *testing.T) {
st := logtide.NewStacktrace(0)
if len(st.Frames) == 0 {
t.Skip("no frames captured")
}
// Innermost frame (last in slice) should be in this test.
inner := st.Frames[len(st.Frames)-1]
if inner.Function == "" {
t.Error("frame Function should not be empty")
}
if inner.Lineno <= 0 {
t.Errorf("frame Lineno = %d, want > 0", inner.Lineno)
}
}
func TestExtractStacktraceFallback(t *testing.T) {
// A plain error has no StackTrace() method — ExtractStacktrace should fall
// back to capturing the current call stack.
err := errors.New("plain error")
st := logtide.ExtractStacktrace(err)
if st == nil {
t.Fatal("ExtractStacktrace returned nil")
}
if len(st.Frames) == 0 {
t.Error("expected frames from fallback capture")
}
}
type pkgErrorsLike struct {
msg string
pcs []uintptr
}
func (e *pkgErrorsLike) Error() string { return e.msg }
func (e *pkgErrorsLike) StackTrace() []uintptr { return e.pcs }
func TestExtractStacktraceFromErrorWithStack(t *testing.T) {
// An error that implements StackTrace() []uintptr (pkg/errors-compatible).
// We pass an empty slice so the stacktrace will have no frames,
// but it should not fall back to the current stack.
wrapped := &pkgErrorsLike{msg: "wrapped", pcs: []uintptr{}}
st := logtide.ExtractStacktrace(wrapped)
if st == nil {
t.Fatal("ExtractStacktrace returned nil")
}
// Since pcs is empty, Frames should be empty (not fallback frames).
if len(st.Frames) != 0 {
t.Errorf("expected 0 frames for empty StackTrace(), got %d", len(st.Frames))
}
}
func TestExtractStacktraceUnwrapsChain(t *testing.T) {
inner := &pkgErrorsLike{msg: "inner", pcs: []uintptr{}}
outer := fmt.Errorf("outer: %w", inner) // stdlib wrapped error
// ExtractStacktrace should unwrap and find inner's StackTrace.
st := logtide.ExtractStacktrace(outer)
if st == nil {
t.Fatal("ExtractStacktrace returned nil")
}
}