Skip to content

Commit

Permalink
Merge pull request #160 from thockin/pr-148-takeover
Browse files Browse the repository at this point in the history
testr: merge testLogger and testLoggerInterface
  • Loading branch information
pohly authored Nov 20, 2022
2 parents f2636e2 + 41ad1c2 commit 4497483
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 41 deletions.
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

0 comments on commit 4497483

Please sign in to comment.