check / check (push) Successful in 9m44s
The four testing.go files in config, database, middleware and session are gone. ClearEnvForTest moves to internal/config/configtest as ClearEnv; the webhook database manager helpers move to internal/database/databasetest as NewWebhookDBManager and NewWebhookDBManagerWithLogger, and the middleware's NewForTest to internal/middleware/middlewaretest as New, 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
53 lines
1.3 KiB
Go
53 lines
1.3 KiB
Go
// Package configtest holds test support for code that reads the
|
|
// process environment.
|
|
package configtest
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// ClearEnv unsets every variable in the process environment
|
|
// for the rest of the test, so a test sees only the variables it sets
|
|
// itself, not whatever the developer's shell exports. When the test
|
|
// ends it leaves the environment exactly as it found it: each variable
|
|
// it unset is put back, and any variable added since is removed.
|
|
func ClearEnv(t *testing.T) {
|
|
t.Helper()
|
|
|
|
present := make(map[string]bool)
|
|
|
|
for _, entry := range os.Environ() {
|
|
key, _, _ := strings.Cut(entry, "=")
|
|
present[key] = true
|
|
|
|
// t.Setenv registers the restore; the Unsetenv after it is
|
|
// what makes the key absent, since a key set to the empty
|
|
// string is still present, and godotenv will not overwrite a
|
|
// present key.
|
|
t.Setenv(key, "")
|
|
|
|
err := os.Unsetenv(key)
|
|
if err != nil {
|
|
t.Fatalf("unsetting %s: %v", key, err)
|
|
}
|
|
}
|
|
|
|
// A variable the test adds other than through t.Setenv, as loading
|
|
// a .env file does, has no restore of its own.
|
|
t.Cleanup(func() {
|
|
for _, entry := range os.Environ() {
|
|
key, _, _ := strings.Cut(entry, "=")
|
|
if present[key] {
|
|
continue
|
|
}
|
|
|
|
err := os.Unsetenv(key)
|
|
if err != nil {
|
|
t.Errorf("unsetting %s: %v", key, err)
|
|
}
|
|
}
|
|
})
|
|
}
|