check / check (push) Successful in 10m29s
The four testing.go files in config, database, middleware and session are gone. ClearEnvForTest moves to internal/config/configtest; the webhook database manager helpers move to internal/database/databasetest and the middleware's NewForTest to internal/middleware/middlewaretest, both now built through the production constructors. Tests that wrapped an open main database use database.Open. The session helpers move into the session package's export_test.go; the middleware tests build their session through session.New and age its timestamps instead of using a fake clock. The session, middleware and webhook database manager now take the *slog.Logger they log through, so tests in other packages can give them their own. Model: opus-5-5
43 lines
976 B
Go
43 lines
976 B
Go
// Package middlewaretest builds a Middleware for tests in other
|
|
// packages.
|
|
package middlewaretest
|
|
|
|
import (
|
|
"log/slog"
|
|
"testing"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/fx/fxtest"
|
|
"sneak.berlin/go/webhooker/internal/config"
|
|
"sneak.berlin/go/webhooker/internal/middleware"
|
|
"sneak.berlin/go/webhooker/internal/session"
|
|
)
|
|
|
|
// NewForTest builds a Middleware through middleware.New, on a
|
|
// lifecycle that is never started.
|
|
//
|
|
// Its metrics recorder writes to a fresh registry of its own, so
|
|
// Metrics() works on it and two of them never collide.
|
|
func NewForTest(
|
|
t *testing.T,
|
|
log *slog.Logger,
|
|
cfg *config.Config,
|
|
sess *session.Session,
|
|
) *middleware.Middleware {
|
|
t.Helper()
|
|
|
|
m, err := middleware.New(
|
|
fxtest.NewLifecycle(t),
|
|
middleware.MiddlewareParams{
|
|
Logger: log,
|
|
Config: cfg,
|
|
Session: sess,
|
|
Registry: prometheus.NewRegistry(),
|
|
},
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
return m
|
|
}
|