From a45c50ab7b11edd2993bec0c5024cc28e76bd84a Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Fri, 19 Jun 2026 18:34:11 +0200 Subject: [PATCH] feat: move MockLogger into a logtest sub-package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- CLAUDE.md | 15 ++++++++------- log_test.go | 8 -------- mocklogger.go => logtest/mocklogger.go | 5 ++++- logtest/mocklogger_test.go | 11 +++++++++++ 4 files changed, 23 insertions(+), 16 deletions(-) rename mocklogger.go => logtest/mocklogger.go (77%) create mode 100644 logtest/mocklogger_test.go diff --git a/CLAUDE.md b/CLAUDE.md index 3029df6..e15b09e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -38,7 +38,9 @@ handler = logmw.RequestLogger(logger)(handler) In tests: ```go -m := logging.NewMockLogger() +import logtest "gitea.unbound.se/shiny/logging/logtest" + +m := logtest.NewMockLogger() // ... exercise code with m.Logger() ... m.Check(t, []string{"level=INFO msg=\"...\""}) ``` @@ -47,16 +49,15 @@ m.Check(t, []string{"level=INFO msg=\"...\""}) - `SetupLogger(logLevel, logFormat, serviceName, buildVersion string) *slog.Logger` - `ContextWithLogger(ctx, *slog.Logger) context.Context` / `LoggerFromContext(ctx) *slog.Logger` -- `Logger` interface; `NewMockLogger() *MockLogger` (+ `MockLogger.Logger()`, `MockLogger.Check(t, want)`). +- `logging/logtest.NewMockLogger() *MockLogger` (+ `MockLogger.Logger()`, `MockLogger.Check(t, want)`) — the test helper, in its own sub-package so production code doesn't pull in testify. - `logging/middleware.RequestLogger(logger) func(http.Handler) http.Handler`. ### Notes -- `MockLogger` currently lives in the main package, so `testify` is a non-test - dependency of the module. Moving it to a `logging/logtest` sub-package is a - tracked low-priority follow-up — it's a breaking import change for the ~13 - services that reference `logging.NewMockLogger`, so it is deferred until a - coordinated bump (Ambix 019ecabc). +- `MockLogger`/`NewMockLogger` live in the `logging/logtest` sub-package, so the + production `logging` package's import graph is free of `testify` — consumers + only compile it if they import `logtest`. (`testify` stays in the module's + `go.mod` because `logtest` and the library's own tests use it.) ### Conventions diff --git a/log_test.go b/log_test.go index 18b9d4e..3540a7e 100644 --- a/log_test.go +++ b/log_test.go @@ -23,11 +23,3 @@ func TestContextLogger(t *testing.T) { ctx := ContextWithLogger(context.Background(), custom) assert.Same(t, custom, LoggerFromContext(ctx)) } - -func TestMockLogger(t *testing.T) { - m := NewMockLogger() - m.Logger().Info("hello", "k", "v") - m.Check(t, []string{`level=INFO msg=hello k=v`}) - empty := NewMockLogger() - empty.Check(t, nil) -} diff --git a/mocklogger.go b/logtest/mocklogger.go similarity index 77% rename from mocklogger.go rename to logtest/mocklogger.go index 37bf6cc..ae2417a 100644 --- a/mocklogger.go +++ b/logtest/mocklogger.go @@ -1,4 +1,7 @@ -package logging +// 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" diff --git a/logtest/mocklogger_test.go b/logtest/mocklogger_test.go new file mode 100644 index 0000000..6c80a9a --- /dev/null +++ b/logtest/mocklogger_test.go @@ -0,0 +1,11 @@ +package logtest + +import "testing" + +func TestMockLogger(t *testing.T) { + m := NewMockLogger() + m.Logger().Info("hello", "k", "v") + m.Check(t, []string{`level=INFO msg=hello k=v`}) + empty := NewMockLogger() + empty.Check(t, nil) +} -- 2.52.0