Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testr: merge testLogger and testLoggerInterface #160

Merged
merged 1 commit into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 16 additions & 40 deletions testr/testr.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ import (
// New returns a logr.Logger that prints through a testing.T object.
// Info logs are only enabled at V(0).
func New(t *testing.T) logr.Logger {
l := &testlogger{
Formatter: funcr.NewFormatter(funcr.Options{}),
t: t,
}
return logr.New(l)
return NewWithOptions(t, Options{})
}

// Options carries parameters which influence the way logs are generated.
Expand All @@ -50,11 +46,7 @@ type Options struct {
// In contrast to the simpler New, output formatting can be configured.
func NewWithOptions(t *testing.T, opts Options) logr.Logger {
l := &testlogger{
Formatter: funcr.NewFormatter(funcr.Options{
LogTimestamp: opts.LogTimestamp,
Verbosity: opts.Verbosity,
}),
t: t,
testloggerInterface: newLoggerInterfaceWithOptions(t, opts),
}
return logr.New(l)
}
Expand All @@ -69,14 +61,18 @@ type TestingT interface {
// TestingT object.
// In contrast to the simpler New, output formatting can be configured.
func NewWithInterface(t TestingT, opts Options) logr.Logger {
l := &testloggerInterface{
l := newLoggerInterfaceWithOptions(t, opts)
return logr.New(&l)
}

func newLoggerInterfaceWithOptions(t TestingT, opts Options) testloggerInterface {
return testloggerInterface{
t: t,
Formatter: funcr.NewFormatter(funcr.Options{
LogTimestamp: opts.LogTimestamp,
Verbosity: opts.Verbosity,
}),
t: t,
}
return logr.New(l)
}

// Underlier exposes access to the underlying testing.T instance. Since
Expand Down Expand Up @@ -115,37 +111,17 @@ func logError(t TestingT, formatError func(error, string, []interface{}) (string
t.Log(args)
}

// This type exists to wrap and modify the method-set of testloggerInterface.
// In particular, it changes the GetUnderlying() method.
type testlogger struct {
funcr.Formatter
t *testing.T
}

func (l testlogger) WithName(name string) logr.LogSink {
l.Formatter.AddName(name)
return &l
}

func (l testlogger) WithValues(kvList ...interface{}) logr.LogSink {
l.Formatter.AddValues(kvList)
return &l
}

func (l testlogger) GetCallStackHelper() func() {
return l.t.Helper
}

func (l testlogger) Info(level int, msg string, kvList ...interface{}) {
l.t.Helper()
logInfo(l.t, l.FormatInfo, level, msg, kvList...)
}

func (l testlogger) Error(err error, msg string, kvList ...interface{}) {
l.t.Helper()
logError(l.t, l.FormatError, err, msg, kvList...)
testloggerInterface
}

func (l testlogger) GetUnderlying() *testing.T {
return l.t
// This method is defined on testlogger, so the only type this could
// possibly be is testing.T, even though that's not guaranteed by the type
// system itself.
return l.t.(*testing.T) //nolint:forcetypeassert
}

type testloggerInterface struct {
Expand Down
2 changes: 1 addition & 1 deletion testr/testr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestLogger(t *testing.T) {

underlier, ok := log.GetSink().(Underlier)
if !ok {
t.Error("couldn't get underlier")
t.Fatal("couldn't get underlier")
}
if t != underlier.GetUnderlying() {
t.Error("invalid underlier")
Expand Down