tests: move the test-only constructors into export_test.go (closes #111)
check / check (push) Successful in 1m9s

state.NewForTest, state.NewForTestWithDataDir and watcher.NewForTest
were in ordinary source files, so they were built into the binary.
NewForTestWithDataDir and watcher.NewForTest now live in their
package's export_test.go. Tests in other packages cannot see those
files, so the watcher and middleware tests build their State with
state.New and a temporary data directory; the watcher tests no longer
try to save to /state.json. state.NewForTest, whose State saved to /,
is deleted: the state tests that used it pass t.TempDir() to
NewForTestWithDataDir, and the test of the helper itself is gone.

Model: opus-5-5
This commit is contained in:
2026-10-01 17:59:27 +00:00
parent 651429137f
commit 07f1cfa9af
8 changed files with 88 additions and 98 deletions
+22 -1
View File
@@ -9,8 +9,12 @@ import (
"testing"
"time"
"go.uber.org/fx/fxtest"
"sneak.berlin/go/dnswatcher/internal/config"
"sneak.berlin/go/dnswatcher/internal/globals"
"sneak.berlin/go/dnswatcher/internal/livednstest"
"sneak.berlin/go/dnswatcher/internal/logger"
"sneak.berlin/go/dnswatcher/internal/portcheck"
"sneak.berlin/go/dnswatcher/internal/resolver"
"sneak.berlin/go/dnswatcher/internal/state"
@@ -148,7 +152,24 @@ func newTestWatcher(
config: cfg,
}
deps.state = state.NewForTest()
g, err := globals.New(nil)
if err != nil {
t.Fatalf("globals.New: %v", err)
}
log, err := logger.New(nil, logger.Params{Globals: g})
if err != nil {
t.Fatalf("logger.New: %v", err)
}
// The watcher saves state after every check, into cfg.DataDir.
deps.state, err = state.New(fxtest.NewLifecycle(t), state.Params{
Logger: log,
Config: cfg,
})
if err != nil {
t.Fatalf("state.New: %v", err)
}
w := watcher.NewForTest(
deps.config,