Skip to content

Instantly share code, notes, and snippets.

@jeffmccune
Created November 1, 2023 22:38
Show Gist options
  • Select an option

  • Save jeffmccune/bde347a3f070f57256ee9c037544f436 to your computer and use it in GitHub Desktop.

Select an option

Save jeffmccune/bde347a3f070f57256ee9c037544f436 to your computer and use it in GitHub Desktop.

Revisions

  1. jeffmccune created this gist Nov 1, 2023.
    8 changes: 8 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    --- 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
    43 changes: 43 additions & 0 deletions testutils.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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)
    }