| 1 |
/* |
| 2 |
Copyright 2019 The logr Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Package testr provides support for using logr in tests. |
| 18 |
package testr |
| 19 |
|
| 20 |
import ( |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/go-logr/logr" |
| 24 |
"github.com/go-logr/logr/funcr" |
| 25 |
) |
| 26 |
|
| 27 |
// New returns a logr.Logger that prints through a testing.T object. |
| 28 |
// Info logs are only enabled at V(0). |
| 29 |
func New(t *testing.T) logr.Logger { |
| 30 |
return NewWithOptions(t, Options{}) |
| 31 |
} |
| 32 |
|
| 33 |
// Options carries parameters which influence the way logs are generated. |
| 34 |
type Options struct { |
| 35 |
// LogTimestamp tells the logger to add a "ts" key to log |
| 36 |
// lines. This has some overhead, so some users might not want |
| 37 |
// it. |
| 38 |
LogTimestamp bool |
| 39 |
|
| 40 |
// Verbosity tells the logger which V logs to be write. |
| 41 |
// Higher values enable more logs. |
| 42 |
Verbosity int |
| 43 |
} |
| 44 |
|
| 45 |
// NewWithOptions returns a logr.Logger that prints through a testing.T object. |
| 46 |
// In contrast to the simpler New, output formatting can be configured. |
| 47 |
func NewWithOptions(t *testing.T, opts Options) logr.Logger { |
| 48 |
l := &testlogger{ |
| 49 |
testloggerInterface: newLoggerInterfaceWithOptions(t, opts), |
| 50 |
} |
| 51 |
return logr.New(l) |
| 52 |
} |
| 53 |
|
| 54 |
// TestingT is an interface wrapper around testing.T, testing.B and testing.F. |
| 55 |
type TestingT interface { |
| 56 |
Helper() |
| 57 |
Log(args ...any) |
| 58 |
} |
| 59 |
|
| 60 |
// NewWithInterface returns a logr.Logger that prints through a |
| 61 |
// TestingT object. |
| 62 |
// In contrast to the simpler New, output formatting can be configured. |
| 63 |
func NewWithInterface(t TestingT, opts Options) logr.Logger { |
| 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, |
| 71 |
Formatter: funcr.NewFormatter(funcr.Options{ |
| 72 |
LogTimestamp: opts.LogTimestamp, |
| 73 |
Verbosity: opts.Verbosity, |
| 74 |
}), |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Underlier exposes access to the underlying testing.T instance. Since |
| 79 |
// callers only have a logr.Logger, they have to know which |
| 80 |
// implementation is in use, so this interface is less of an |
| 81 |
// abstraction and more of a way to test type conversion. |
| 82 |
type Underlier interface { |
| 83 |
GetUnderlying() *testing.T |
| 84 |
} |
| 85 |
|
| 86 |
// UnderlierInterface exposes access to the underlying TestingT instance. Since |
| 87 |
// callers only have a logr.Logger, they have to know which |
| 88 |
// implementation is in use, so this interface is less of an |
| 89 |
// abstraction and more of a way to test type conversion. |
| 90 |
type UnderlierInterface interface { |
| 91 |
GetUnderlying() TestingT |
| 92 |
} |
| 93 |
|
| 94 |
// Info logging implementation shared between testLogger and testLoggerInterface. |
| 95 |
func logInfo(t TestingT, formatInfo func(int, string, []any) (string, string), level int, msg string, kvList ...any) { |
| 96 |
prefix, args := formatInfo(level, msg, kvList) |
| 97 |
t.Helper() |
| 98 |
if prefix != "" { |
| 99 |
args = prefix + ": " + args |
| 100 |
} |
| 101 |
t.Log(args) |
| 102 |
} |
| 103 |
|
| 104 |
// Error logging implementation shared between testLogger and testLoggerInterface. |
| 105 |
func logError(t TestingT, formatError func(error, string, []any) (string, string), err error, msg string, kvList ...any) { |
| 106 |
prefix, args := formatError(err, msg, kvList) |
| 107 |
t.Helper() |
| 108 |
if prefix != "" { |
| 109 |
args = prefix + ": " + args |
| 110 |
} |
| 111 |
t.Log(args) |
| 112 |
} |
| 113 |
|
| 114 |
// This type exists to wrap and modify the method-set of testloggerInterface. |
| 115 |
// In particular, it changes the GetUnderlying() method. |
| 116 |
type testlogger struct { |
| 117 |
testloggerInterface |
| 118 |
} |
| 119 |
|
| 120 |
func (l testlogger) GetUnderlying() *testing.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 |
| 125 |
} |
| 126 |
|
| 127 |
type testloggerInterface struct { |
| 128 |
funcr.Formatter |
| 129 |
t TestingT |
| 130 |
} |
| 131 |
|
| 132 |
func (l testloggerInterface) WithName(name string) logr.LogSink { |
| 133 |
l.AddName(name) // via Formatter |
| 134 |
return &l |
| 135 |
} |
| 136 |
|
| 137 |
func (l testloggerInterface) WithValues(kvList ...any) logr.LogSink { |
| 138 |
l.AddValues(kvList) // via Formatter |
| 139 |
return &l |
| 140 |
} |
| 141 |
|
| 142 |
func (l testloggerInterface) GetCallStackHelper() func() { |
| 143 |
return l.t.Helper |
| 144 |
} |
| 145 |
|
| 146 |
func (l testloggerInterface) Info(level int, msg string, kvList ...any) { |
| 147 |
l.t.Helper() |
| 148 |
logInfo(l.t, l.FormatInfo, level, msg, kvList...) |
| 149 |
} |
| 150 |
|
| 151 |
func (l testloggerInterface) Error(err error, msg string, kvList ...any) { |
| 152 |
l.t.Helper() |
| 153 |
logError(l.t, l.FormatError, err, msg, kvList...) |
| 154 |
} |
| 155 |
|
| 156 |
func (l testloggerInterface) GetUnderlying() TestingT { |
| 157 |
return l.t |
| 158 |
} |
| 159 |
|
| 160 |
// Assert conformance to the interfaces. |
| 161 |
var _ logr.LogSink = &testlogger{} |
| 162 |
var _ logr.CallStackHelperLogSink = &testlogger{} |
| 163 |
var _ Underlier = &testlogger{} |
| 164 |
|
| 165 |
var _ logr.LogSink = &testloggerInterface{} |
| 166 |
var _ logr.CallStackHelperLogSink = &testloggerInterface{} |
| 167 |
var _ UnderlierInterface = &testloggerInterface{} |