Files
webhooker/internal/config/testing.go
T
sneak e72d08db13
check / check (push) Waiting to run
Isolate config tests from the shell; any out-of-range PORT is ErrInvalidPort (closes #94)
Config tests, and the first-boot debug log test that builds a Config,
start from an empty environment: config.ClearEnvForTest unsets every
variable the process has and restores each when the test ends, so
nothing exported in the developer's shell changes a result.
TestEnvPositiveInt and TestEnvPort share one table runner, and
TestEnvPort checks that the bad value appears in each error.

Every out-of-range PORT now wraps ErrInvalidPort: zero, negatives,
above 65535, and numbers too large or too small for an int.

The README configuration table and the Settings page say that a
RETENTION_SWEEP_INTERVAL that does not parse, or is zero or negative,
fails startup.

Model: opus-5-5
2026-10-02 14:50:03 +00:00

31 lines
740 B
Go

package config
import (
"os"
"strings"
"testing"
)
// ClearEnvForTest unsets every variable in the process environment
// for the rest of the test and puts each back when the test ends, so
// a test sees only the variables it sets itself, not whatever the
// developer's shell exports.
func ClearEnvForTest(t *testing.T) {
t.Helper()
for _, entry := range os.Environ() {
key, _, _ := strings.Cut(entry, "=")
// 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)
}
}
}