From 41ad1c2ab75f42b47a6c1372aef8f5a758b08882 Mon Sep 17 00:00:00 2001 From: Timon Wong Date: Fri, 12 Aug 2022 08:56:17 +0800 Subject: [PATCH] testr: merge testLogger and testLoggerInterface Signed-off-by: Timon Wong --- testr/testr.go | 56 +++++++++++++-------------------------------- testr/testr_test.go | 2 +- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/testr/testr.go b/testr/testr.go index aaf7de5..2772b49 100644 --- a/testr/testr.go +++ b/testr/testr.go @@ -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. @@ -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) } @@ -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 @@ -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 { diff --git a/testr/testr_test.go b/testr/testr_test.go index 4a737b7..252c4be 100644 --- a/testr/testr_test.go +++ b/testr/testr_test.go @@ -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")