Files
logging/logtest/mocklogger.go
T
argoyle a705fc33c4
logging / test (push) Has been skipped
logging / vulnerabilities (push) Has been skipped
Release / release (push) Successful in 56s
logging / coverage-baseline (push) Successful in 2m49s
pre-commit / pre-commit (push) Successful in 5m29s
feat: move MockLogger into a logtest sub-package (#5)
2026-06-19 16:43:26 +00:00

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)
}