Skip to content

Commit

Permalink
testing: fix source code location printed by testing.T
Browse files Browse the repository at this point in the history
The funcr log sink must implement the new HelperLogSink
interface (otherwise the wrapper function wouldn't be handled
correctly) and also mark its own wrapper as a helper.
  • Loading branch information
pohly committed Aug 4, 2021
1 parent bcd3916 commit e94d002
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
21 changes: 20 additions & 1 deletion funcr/funcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ func New(fn func(prefix, args string), opts Options) logr.Logger {
}

func newSink(fn func(prefix, args string), opts Options) logr.LogSink {
return &fnlogger{
l := &fnlogger{
prefix: "",
values: nil,
depth: 0,
write: fn,
logCaller: opts.LogCaller,
verbosity: opts.Verbosity,
helper: opts.Helper,
}
if l.helper == nil {
// We have to have a valid function for GetHelper, so
// we might as well just cover the nil case once here.
l.helper = func() {}
}
return l
}

// Options carries parameters which influence the way logs are generated.
Expand All @@ -55,6 +62,10 @@ type Options struct {
// Verbosity tells funcr which V logs to be write. Higher values enable
// more logs.
Verbosity int

// Helper is an optional function that funcr must call to mark its own
// stack frames as helper functions.
Helper func()
}

// MessageClass indicates which category or categories of messages to consider.
Expand All @@ -72,13 +83,15 @@ type fnlogger struct {
values []interface{}
depth int
write func(prefix, args string)
helper func()
logCaller MessageClass
verbosity int
}

// Assert conformance to the interfaces.
var _ logr.LogSink = &fnlogger{}
var _ logr.CallDepthLogSink = &fnlogger{}
var _ logr.HelperLogSink = &fnlogger{}

func flatten(kvList ...interface{}) string {
if len(kvList)%2 != 0 {
Expand Down Expand Up @@ -271,6 +284,7 @@ func (l fnlogger) Info(level int, msg string, kvList ...interface{}) {
args = append(args, l.values...)
args = append(args, kvList...)
argsStr := flatten(args...)
l.helper()
l.write(l.prefix, argsStr)
}

Expand All @@ -288,6 +302,7 @@ func (l fnlogger) Error(err error, msg string, kvList ...interface{}) {
args = append(args, l.values...)
args = append(args, kvList...)
argsStr := flatten(args...)
l.helper()
l.write(l.prefix, argsStr)
}

Expand All @@ -313,3 +328,7 @@ func (l fnlogger) WithCallDepth(depth int) logr.LogSink {
l.depth += depth
return &l
}

func (l fnlogger) GetHelper() func() {
return l.helper
}
3 changes: 2 additions & 1 deletion testing/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import (
// Info logs are only enabled at V(0).
func NewTestLogger(t *testing.T) logr.Logger {
fn := func(prefix, args string) {
t.Helper()
t.Logf("%s: %s", prefix, args)
}
return funcr.New(fn, funcr.Options{})
return funcr.New(fn, funcr.Options{Helper: t.Helper})
}
10 changes: 10 additions & 0 deletions testing/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package testing
import (
"fmt"
"testing"

"github.com/go-logr/logr"
)

func TestTestLogger(t *testing.T) {
Expand All @@ -27,4 +29,12 @@ func TestTestLogger(t *testing.T) {
logger.V(0).Info("V(0).info")
logger.V(1).Info("v(1).info")
logger.Error(fmt.Errorf("error"), "error")

myWrapper(logger, "hello world")
}

func myWrapper(logger logr.Logger, msg string) {
helper, logger := logger.Helper()
helper()
logger.Info(msg)
}

0 comments on commit e94d002

Please sign in to comment.