Skip to content

Commit 5243e37

Browse files
committed
Clean up sloghandler testing and restore coverage
This commit retools the way we do testing of sloghandler. It moves the test implementations of LogSink and SlogSink into new files and then runs the standard slog tests against them, which excercises both the SlogSlink and plain LogSink paths. It also retools sloghandler to handle slog Attrs a bit better and fixes some of the previous exceptions to slog's standard test. It still doesn't hande groups "properly", but this is simpler and users who really need slog support should use an impl that supports SlogSink (e.g. funcr).
1 parent 402e5b6 commit 5243e37

File tree

7 files changed

+356
-334
lines changed

7 files changed

+356
-334
lines changed

logr_noslog_test.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

logr_slog_test.go

Lines changed: 0 additions & 218 deletions
This file was deleted.

logr_test.go

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -24,85 +24,6 @@ import (
2424
"testing"
2525
)
2626

27-
// testLogSink is a Logger just for testing that calls optional hooks on each method.
28-
type testLogSink struct {
29-
fnInit func(ri RuntimeInfo)
30-
fnEnabled func(lvl int) bool
31-
fnInfo func(lvl int, msg string, kv ...any)
32-
fnError func(err error, msg string, kv ...any)
33-
fnWithValues func(kv ...any)
34-
fnWithName func(name string)
35-
36-
withValues []any
37-
38-
// testSlogSink contains some additional fields if (and only if) slog is supported by Go.
39-
// See logr_slog_test.go.
40-
//nolint:unused // Only unused with Go < 1.21.
41-
testSlogSink
42-
}
43-
44-
var _ LogSink = &testLogSink{}
45-
46-
func (l *testLogSink) Init(ri RuntimeInfo) {
47-
if l.fnInit != nil {
48-
l.fnInit(ri)
49-
}
50-
}
51-
52-
func (l *testLogSink) Enabled(lvl int) bool {
53-
if l.fnEnabled != nil {
54-
return l.fnEnabled(lvl)
55-
}
56-
return false
57-
}
58-
59-
func (l *testLogSink) Info(lvl int, msg string, kv ...any) {
60-
if l.fnInfo != nil {
61-
l.fnInfo(lvl, msg, kv...)
62-
}
63-
}
64-
65-
func (l *testLogSink) Error(err error, msg string, kv ...any) {
66-
if l.fnError != nil {
67-
l.fnError(err, msg, kv...)
68-
}
69-
}
70-
71-
func (l *testLogSink) WithValues(kv ...any) LogSink {
72-
if l.fnWithValues != nil {
73-
l.fnWithValues(kv...)
74-
}
75-
out := *l
76-
n := len(out.withValues)
77-
out.withValues = append(out.withValues[:n:n], kv...)
78-
return &out
79-
}
80-
81-
func (l *testLogSink) WithName(name string) LogSink {
82-
if l.fnWithName != nil {
83-
l.fnWithName(name)
84-
}
85-
out := *l
86-
return &out
87-
}
88-
89-
type testCallDepthLogSink struct {
90-
testLogSink
91-
callDepth int
92-
fnWithCallDepth func(depth int)
93-
}
94-
95-
var _ CallDepthLogSink = &testCallDepthLogSink{}
96-
97-
func (l *testCallDepthLogSink) WithCallDepth(depth int) LogSink {
98-
if l.fnWithCallDepth != nil {
99-
l.fnWithCallDepth(depth)
100-
}
101-
out := *l
102-
out.callDepth += depth
103-
return &out
104-
}
105-
10627
func TestNew(t *testing.T) {
10728
calledInit := 0
10829

0 commit comments

Comments
 (0)