Skip to content

Instantly share code, notes, and snippets.

@jeffmccune
Created November 1, 2023 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffmccune/bde347a3f070f57256ee9c037544f436 to your computer and use it in GitHub Desktop.
Save jeffmccune/bde347a3f070f57256ee9c037544f436 to your computer and use it in GitHub Desktop.
log source in tests
--- FAIL: TestServer (0.00s)
--- FAIL: TestServer/NewServer (0.00s)
testutils.go:16: DBG identity.go:80: could not get oidc provider err="Get \"/.well-known/openid-configuration\": unsupported protocol scheme \"\""
server_suite_test.go:28:
Error Trace: /home/jeff/workspace/holos-run/holos-server-go/internal/server/server_suite_test.go:28
Error: Received unexpected error:
Get "/.well-known/openid-configuration": unsupported protocol scheme ""
Test: TestServer/NewServer
package testutils
import (
"fmt"
"github.com/lmittmann/tint"
"log/slog"
"path/filepath"
"testing"
)
type testWriter struct {
t testing.TB
}
func (w *testWriter) Write(b []byte) (int, error) {
w.t.Logf("%s", b)
return len(b), nil
}
// TestLogger is an adapter that sends output to t.Log so that log messages are
// associated with failing tests.
func TestLogger(t testing.TB) *slog.Logger {
t.Helper()
testHandler := tint.NewHandler(&testWriter{t}, &tint.Options{
Level: slog.LevelDebug,
AddSource: true,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if len(groups) == 0 {
switch key := a.Key; key {
case slog.TimeKey:
return slog.Attr{} // Remove the timestamp
case slog.SourceKey:
if src, ok := a.Value.Any().(*slog.Source); ok {
name := fmt.Sprintf("%s:%d:", filepath.Base(src.File), src.Line)
return slog.String("source", name)
}
}
}
return a
},
})
return slog.New(testHandler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment