-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmulti_test.go
124 lines (111 loc) · 2.72 KB
/
multi_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
package strc_test
import (
"bytes"
"context"
"encoding/json"
"log/slog"
"strings"
"testing"
"testing/slogtest"
"github.com/google/go-cmp/cmp"
"github.com/osbuild/logging/pkg/strc"
)
func TestMultiSlogtest(t *testing.T) {
var buf bytes.Buffer
hj := slog.NewJSONHandler(&buf, nil)
h := strc.NewMultiHandler(hj)
results := func() []map[string]any {
var ms []map[string]any
for _, line := range bytes.Split(buf.Bytes(), []byte{'\n'}) {
if len(line) == 0 {
continue
}
var m map[string]any
if err := json.Unmarshal(line, &m); err != nil {
t.Fatal(err)
}
ms = append(ms, m)
}
return ms
}
err := slogtest.TestHandler(h, results)
if err != nil {
t.Fatal(err)
}
}
func TestMultiCustomSlogtest(t *testing.T) {
testMultiCallback := func(ctx context.Context, a []slog.Attr) ([]slog.Attr, error) {
return a, nil
}
var buf bytes.Buffer
hj := slog.NewJSONHandler(&buf, nil)
h := strc.NewMultiHandlerCustom(nil, testMultiCallback, hj)
results := func() []map[string]any {
var ms []map[string]any
for _, line := range bytes.Split(buf.Bytes(), []byte{'\n'}) {
if len(line) == 0 {
continue
}
var m map[string]any
if err := json.Unmarshal(line, &m); err != nil {
t.Fatal(err)
}
ms = append(ms, m)
}
return ms
}
err := slogtest.TestHandler(h, results)
if err != nil {
t.Fatal(err)
}
}
func TestMultiCustomValuesSlogtest(t *testing.T) {
type ctxKey string
testMultiCallback := func(ctx context.Context, a []slog.Attr) ([]slog.Attr, error) {
if v := ctx.Value(ctxKey("key")); v != nil {
a = append(a, slog.Any("ctx", v))
}
return a, nil
}
tests := []struct {
block func(logger *slog.Logger)
want string
}{
{
block: func(logger *slog.Logger) {
logger.Debug("test")
},
want: `{"msg":"test","a":"b"}`,
},
{
block: func(logger *slog.Logger) {
logger.InfoContext(context.Background(), "test")
},
want: `{"msg":"test","a":"b"}`,
},
{
block: func(logger *slog.Logger) {
ctx := context.WithValue(context.Background(), ctxKey("key"), "value")
logger.ErrorContext(ctx, "test")
},
want: `{"msg":"test","a":"b","ctx":"value"}`,
},
}
for _, tc := range tests {
var buf bytes.Buffer
hj := slog.NewJSONHandler(&buf, &slog.HandlerOptions{
Level: slog.LevelDebug,
ReplaceAttr: func(group []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey || a.Key == slog.LevelKey || a.Key == strc.BuildIDFieldKey {
return slog.Attr{}
}
return a
},
})
h := strc.NewMultiHandlerCustom([]slog.Attr{slog.String("a", "b")}, testMultiCallback, hj)
tc.block(slog.New(h))
if diff := cmp.Diff(strings.TrimSpace(tc.want), strings.TrimSpace(buf.String())); diff != "" {
t.Errorf("unexpected diff: %s", diff)
}
}
}