Skip to content

Commit

Permalink
Clean up sloghandler testing and restore coverage
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
thockin committed Dec 7, 2023
1 parent 402e5b6 commit 5243e37
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 334 deletions.
23 changes: 0 additions & 23 deletions logr_noslog_test.go

This file was deleted.

218 changes: 0 additions & 218 deletions logr_slog_test.go

This file was deleted.

79 changes: 0 additions & 79 deletions logr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,85 +24,6 @@ import (
"testing"
)

// testLogSink is a Logger just for testing that calls optional hooks on each method.
type testLogSink struct {
fnInit func(ri RuntimeInfo)
fnEnabled func(lvl int) bool
fnInfo func(lvl int, msg string, kv ...any)
fnError func(err error, msg string, kv ...any)
fnWithValues func(kv ...any)
fnWithName func(name string)

withValues []any

// testSlogSink contains some additional fields if (and only if) slog is supported by Go.
// See logr_slog_test.go.
//nolint:unused // Only unused with Go < 1.21.
testSlogSink
}

var _ LogSink = &testLogSink{}

func (l *testLogSink) Init(ri RuntimeInfo) {
if l.fnInit != nil {
l.fnInit(ri)
}
}

func (l *testLogSink) Enabled(lvl int) bool {
if l.fnEnabled != nil {
return l.fnEnabled(lvl)
}
return false
}

func (l *testLogSink) Info(lvl int, msg string, kv ...any) {
if l.fnInfo != nil {
l.fnInfo(lvl, msg, kv...)
}
}

func (l *testLogSink) Error(err error, msg string, kv ...any) {
if l.fnError != nil {
l.fnError(err, msg, kv...)
}
}

func (l *testLogSink) WithValues(kv ...any) LogSink {
if l.fnWithValues != nil {
l.fnWithValues(kv...)
}
out := *l
n := len(out.withValues)
out.withValues = append(out.withValues[:n:n], kv...)
return &out
}

func (l *testLogSink) WithName(name string) LogSink {
if l.fnWithName != nil {
l.fnWithName(name)
}
out := *l
return &out
}

type testCallDepthLogSink struct {
testLogSink
callDepth int
fnWithCallDepth func(depth int)
}

var _ CallDepthLogSink = &testCallDepthLogSink{}

func (l *testCallDepthLogSink) WithCallDepth(depth int) LogSink {
if l.fnWithCallDepth != nil {
l.fnWithCallDepth(depth)
}
out := *l
out.callDepth += depth
return &out
}

func TestNew(t *testing.T) {
calledInit := 0

Expand Down
Loading

0 comments on commit 5243e37

Please sign in to comment.