-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
126 lines (106 loc) · 3.12 KB
/
handler_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
package errlog_test
import (
"context"
"errors"
"log/slog"
"strings"
"testing"
"github.com/ichizero/errlog"
)
func TestHandler_Handle_OverrideSource(t *testing.T) {
t.Parallel()
tests := []struct {
name string
overrideSource bool
wantFileSuffix string
}{
{
name: "should override source with stack trace",
overrideSource: true,
wantFileSuffix: "errlog/testhelper_test.go",
},
{
name: "should not override source with stack trace",
overrideSource: false,
wantFileSuffix: "errlog/handler_test.go",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
logger, buf := newLogger(t,
&slog.HandlerOptions{AddSource: true},
&errlog.HandlerOptions{OverrideSource: tt.overrideSource})
errWithStack := newWrappedError(t)
logger.ErrorContext(context.Background(), "test", slog.Any(errlog.ErrorKey, errWithStack))
got := unmarshalLog(t, buf)
source, ok := got[slog.SourceKey]
assertTrue(t, ok)
sourceMap, ok := source.(map[string]any)
assertTrue(t, ok)
file, ok := sourceMap["file"]
assertTrue(t, ok)
fileStr, ok := file.(string)
assertTrue(t, ok)
if !strings.HasSuffix(fileStr, tt.wantFileSuffix) {
t.Errorf("file is incorrect: wantFileSuffix=%s, got=%s", tt.wantFileSuffix, file)
}
})
}
}
func TestHandler_Handle_StackTrace(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err error
suppressStackTrace bool
wantStackTrace bool
}{
{
name: "should not output stack trace when err has no stack trace",
err: errors.New("no stack trace"),
suppressStackTrace: false,
wantStackTrace: false,
},
{
name: "should not output stack trace when err has no stack trace and suppress option is true",
err: errors.New("no stack trace"),
suppressStackTrace: true,
wantStackTrace: false,
},
{
name: "should output stack trace when err has stack trace",
err: newWrappedError(t),
suppressStackTrace: false,
wantStackTrace: true,
},
{
name: "should not output stack trace when err has stack trace but suppress option is true",
err: newWrappedError(t),
suppressStackTrace: true,
wantStackTrace: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
logger, buf := newLogger(t,
&slog.HandlerOptions{AddSource: true},
&errlog.HandlerOptions{SuppressStackTrace: tt.suppressStackTrace})
logger.ErrorContext(context.Background(), "test", slog.Any(errlog.ErrorKey, tt.err))
got := unmarshalLog(t, buf)
stackTrace, ok := got[errlog.StackTraceKey]
if tt.wantStackTrace != ok {
t.Errorf("stack trace is incorrect: wantStackTrace=%t, got=%s", tt.wantStackTrace, stackTrace)
}
if !tt.wantStackTrace {
return
}
stackTraceStr, ok := stackTrace.(string)
assertTrue(t, ok)
if !strings.Contains(stackTraceStr, "TestHandler_Handle_StackTrace") {
t.Errorf("stack trace is incorrect: got=%s", stackTrace)
}
})
}
}