Skip to content

Commit 4497483

Browse files
authored
Merge pull request #160 from thockin/pr-148-takeover
testr: merge testLogger and testLoggerInterface
2 parents f2636e2 + 41ad1c2 commit 4497483

File tree

2 files changed

+17
-41
lines changed

2 files changed

+17
-41
lines changed

testr/testr.go

+16-40
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ import (
2727
// New returns a logr.Logger that prints through a testing.T object.
2828
// Info logs are only enabled at V(0).
2929
func New(t *testing.T) logr.Logger {
30-
l := &testlogger{
31-
Formatter: funcr.NewFormatter(funcr.Options{}),
32-
t: t,
33-
}
34-
return logr.New(l)
30+
return NewWithOptions(t, Options{})
3531
}
3632

3733
// Options carries parameters which influence the way logs are generated.
@@ -50,11 +46,7 @@ type Options struct {
5046
// In contrast to the simpler New, output formatting can be configured.
5147
func NewWithOptions(t *testing.T, opts Options) logr.Logger {
5248
l := &testlogger{
53-
Formatter: funcr.NewFormatter(funcr.Options{
54-
LogTimestamp: opts.LogTimestamp,
55-
Verbosity: opts.Verbosity,
56-
}),
57-
t: t,
49+
testloggerInterface: newLoggerInterfaceWithOptions(t, opts),
5850
}
5951
return logr.New(l)
6052
}
@@ -69,14 +61,18 @@ type TestingT interface {
6961
// TestingT object.
7062
// In contrast to the simpler New, output formatting can be configured.
7163
func NewWithInterface(t TestingT, opts Options) logr.Logger {
72-
l := &testloggerInterface{
64+
l := newLoggerInterfaceWithOptions(t, opts)
65+
return logr.New(&l)
66+
}
67+
68+
func newLoggerInterfaceWithOptions(t TestingT, opts Options) testloggerInterface {
69+
return testloggerInterface{
70+
t: t,
7371
Formatter: funcr.NewFormatter(funcr.Options{
7472
LogTimestamp: opts.LogTimestamp,
7573
Verbosity: opts.Verbosity,
7674
}),
77-
t: t,
7875
}
79-
return logr.New(l)
8076
}
8177

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

114+
// This type exists to wrap and modify the method-set of testloggerInterface.
115+
// In particular, it changes the GetUnderlying() method.
118116
type testlogger struct {
119-
funcr.Formatter
120-
t *testing.T
121-
}
122-
123-
func (l testlogger) WithName(name string) logr.LogSink {
124-
l.Formatter.AddName(name)
125-
return &l
126-
}
127-
128-
func (l testlogger) WithValues(kvList ...interface{}) logr.LogSink {
129-
l.Formatter.AddValues(kvList)
130-
return &l
131-
}
132-
133-
func (l testlogger) GetCallStackHelper() func() {
134-
return l.t.Helper
135-
}
136-
137-
func (l testlogger) Info(level int, msg string, kvList ...interface{}) {
138-
l.t.Helper()
139-
logInfo(l.t, l.FormatInfo, level, msg, kvList...)
140-
}
141-
142-
func (l testlogger) Error(err error, msg string, kvList ...interface{}) {
143-
l.t.Helper()
144-
logError(l.t, l.FormatError, err, msg, kvList...)
117+
testloggerInterface
145118
}
146119

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

151127
type testloggerInterface struct {

testr/testr_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestLogger(t *testing.T) {
4141

4242
underlier, ok := log.GetSink().(Underlier)
4343
if !ok {
44-
t.Error("couldn't get underlier")
44+
t.Fatal("couldn't get underlier")
4545
}
4646
if t != underlier.GetUnderlying() {
4747
t.Error("invalid underlier")

0 commit comments

Comments
 (0)