-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackerr_test.go
More file actions
166 lines (139 loc) · 3.8 KB
/
stackerr_test.go
File metadata and controls
166 lines (139 loc) · 3.8 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
package stackerr_test
import (
"bytes"
"errors"
"os"
"strings"
"testing"
"github.com/LZStock-OS/stackerr"
)
// Helper to reset config after each test
func resetConfig() {
stackerr.Config.StripSequences = []string{"(0", "({"}
stackerr.Config.PathReplacements = stackerr.GetDefaultPathReplacements()
stackerr.Config.MaxStackDepth = 10
stackerr.Config.Output = os.Stderr
}
func TestRecover(t *testing.T) {
defer resetConfig()
var err error
func() {
defer stackerr.Recover(&err)
panic("boom")
}()
if err == nil {
t.Fatal("expected error, got nil")
}
stack := stackerr.GetStack(err)
if stack == "" {
t.Fatal("expected stack trace")
}
if err.Error() != "panic: boom" {
t.Errorf("expected panic: boom, got %v", err)
}
if !strings.Contains(stack, "TestRecover") {
t.Errorf("stack trace should contain TestRecover")
}
}
func TestThrowPanic(t *testing.T) {
defer resetConfig()
var err error
func() {
defer stackerr.Recover(&err)
stackerr.ThrowPanic(errors.New("throw error"))
}()
if err == nil {
t.Fatal("expected error, got nil")
}
stack := stackerr.GetStack(err)
if stack == "" {
t.Fatal("expected stack trace")
}
if err.Error() != "throw error" {
t.Errorf("expected throw error, got %v", err)
}
if !strings.Contains(stack, "TestThrowPanic") {
t.Errorf("stack trace should contain TestThrowPanic")
}
}
func TestMaxStackDepth(t *testing.T) {
defer resetConfig()
stackerr.Config.MaxStackDepth = 2
var err error
func() {
defer stackerr.Recover(&err)
panic("depth test")
}()
stack := stackerr.GetStack(err)
if stack == "" {
t.Fatal("expected stack trace")
}
lines := strings.Split(strings.TrimSpace(stack), "\n")
if len(lines) > 2 {
t.Errorf("expected at most 2 stack frames, got %d\nStack:\n%s", len(lines), stack)
}
}
func TestPathReplacement(t *testing.T) {
defer resetConfig()
// We'll replace the path to the test file with something unique
// Since we don't know the absolute path easily in test without runtime.Caller,
// let's just use a replacement that likely matches part of the path.
// We know the file is stackerr_test.go.
// But the library does simple string replacement on the file path.
// Let's assume the stack trace contains "stackerr_test.go".
// We want to replace "stackerr" with "replaced_lib".
stackerr.Config.PathReplacements = map[string]string{
"stackerr": "replaced_lib",
}
var err error
func() {
defer stackerr.Recover(&err)
panic("path test")
}()
stack := stackerr.GetStack(err)
if !strings.Contains(stack, "replaced_lib") {
// It's possible the file path in stack trace is absolute and doesn't contain "stackerr" if run in a weird way,
// but usually it should be there.
// However, in the sandbox environment, path is /Users/universetennis/Code/go/src/stackerr/stackerr_test.go
// So "stackerr" is definitely in there.
t.Errorf("Stack trace expected to contain 'replaced_lib', got:\n%s", stack)
}
}
func TestCustomOutput(t *testing.T) {
defer resetConfig()
var buf bytes.Buffer
stackerr.Config.Output = &buf
var err error
func() {
defer stackerr.Recover(&err)
panic("log test")
}()
output := buf.String()
if !strings.Contains(output, "Recovered from panic: panic: log test") {
t.Errorf("expected log output to contain panic message, got: %s", output)
}
}
func TestRecoverStandardError(t *testing.T) {
defer resetConfig()
var err error = errors.New("standard error")
func() {
defer stackerr.Recover(&err)
}()
stack := stackerr.GetStack(err)
if stack == "" {
t.Fatal("expected stack trace")
}
if err.Error() != "standard error" {
t.Errorf("expected error message 'standard error', got '%s'", err.Error())
}
}
func TestRecoverNilError(t *testing.T) {
defer resetConfig()
var err error
func() {
defer stackerr.Recover(&err)
}()
if err != nil {
t.Errorf("expected nil error, got %v", err)
}
}