a45c50ab7b
NewMockLogger/MockLogger move from the main `logging` package to a new `logging/logtest` sub-package, so the production `logging` package's import graph no longer pulls in testify — only consumers that import logtest do. testify stays in the module's go.mod (used by logtest and the library's own tests). Breaking import change for consumers: `logging.NewMockLogger` becomes `logtest.NewMockLogger`. The backend services that reference it are updated to the logtest import alongside their go.mod bump to this release. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
// Package logtest provides test helpers for the logging library. It is kept in
|
|
// its own package so that importing the production logging package does not pull
|
|
// in testify (a test-only dependency) — only consumers that import logtest do.
|
|
package logtest
|
|
|
|
import (
|
|
"bytes"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func NewMockLogger() *MockLogger {
|
|
logged := &bytes.Buffer{}
|
|
|
|
return &MockLogger{
|
|
logged: logged,
|
|
logger: slog.New(slog.NewTextHandler(logged, &slog.HandlerOptions{
|
|
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
|
|
if a.Key == "time" {
|
|
return slog.Attr{}
|
|
}
|
|
return a
|
|
},
|
|
})),
|
|
}
|
|
}
|
|
|
|
type MockLogger struct {
|
|
logger *slog.Logger
|
|
logged *bytes.Buffer
|
|
}
|
|
|
|
func (m *MockLogger) Logger() *slog.Logger {
|
|
return m.logger
|
|
}
|
|
|
|
func (m *MockLogger) Check(t testing.TB, wantLogged []string) {
|
|
var gotLogged []string
|
|
if m.logged.String() != "" {
|
|
gotLogged = strings.Split(m.logged.String(), "\n")
|
|
gotLogged = gotLogged[:len(gotLogged)-1]
|
|
}
|
|
if len(wantLogged) == 0 {
|
|
assert.Empty(t, gotLogged)
|
|
return
|
|
}
|
|
assert.Equal(t, wantLogged, gotLogged)
|
|
}
|