Skip to content

Commit

Permalink
WIP: fix slogtest reporter with t.Helper
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hall <[email protected]>
  • Loading branch information
imjasonh committed Feb 12, 2024
1 parent f4145d2 commit adb77fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 3 additions & 1 deletion slogtest/slogtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ type logAdapter struct {
}

func (l *logAdapter) Write(b []byte) (int, error) {
l.l.Helper()
l.l.Log(string(b))
return len(b), nil
}

type Logger interface {
Helper()
Log(args ...any)
Logf(format string, args ...any)
}

// TestLogger gets a logger to use in unit and end to end tests
func TestLogger(t Logger) *clog.Logger {
return clog.New(slog.NewTextHandler(&logAdapter{l: t}, nil))
return clog.New(slog.NewTextHandler(&logAdapter{l: t}, &slog.HandlerOptions{}))
}

// TestContextWithLogger returns a context with a logger to be used in tests
Expand Down
13 changes: 13 additions & 0 deletions slogtest/slogtest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package slogtest

import (
"testing"

"github.com/chainguard-dev/clog"
)

func TestSlogTest(t *testing.T) {
ctx := TestContextWithLogger(t)

clog.FromContext(ctx).With("foo", "bar").Infof("hello world")
}

0 comments on commit adb77fc

Please sign in to comment.