Files
Crussell/backend/db/db_test_init.go
T
popertots 248756a1fe
Backend Tests / test (push) Successful in 59s
fix(db): prevent POSTGRES_HOST env var leakage between tests
resetEnv() conditionally set POSTGRES_HOST only when empty. When
TestConnect_InvalidCredentials explicitly set it to 'localhost' and
then called resetEnv(), the value was preserved because it wasn't
empty. This leaked into TestConcurrentQueries, which then tried to
connect to localhost:5432 instead of the workflow-configured postgres
hostname.

Fix: capture the POSTGRES_HOST value at init() time in a package-level
variable (savedPOSTGRESHost) and always restore it in resetEnv(), so
the correct value is always used regardless of which tests ran before.
2026-06-25 01:21:15 +01:00

35 lines
860 B
Go

//go:build test
// +build test
package db
import (
"os"
)
// DB is a compatibility alias for Conn, for tests not yet migrated from db.Conn to db.Conn.
// savedPOSTGRESHost captures the env value at init time so resetEnv() can
// always restore it, even after a test temporarily overrides it.
var savedPOSTGRESHost string
func init() {
// Set defaults for test environment if not already set
if os.Getenv("POSTGRES_HOST") == "" {
os.Setenv("POSTGRES_HOST", "localhost")
}
savedPOSTGRESHost = os.Getenv("POSTGRES_HOST")
if os.Getenv("POSTGRES_USER") == "" {
os.Setenv("POSTGRES_USER", "myuser")
}
if os.Getenv("POSTGRES_PASSWORD") == "" {
os.Setenv("POSTGRES_PASSWORD", "mypassword")
}
if os.Getenv("POSTGRES_DB") == "" {
os.Setenv("POSTGRES_DB", "crussell_test")
}
if os.Getenv("GO_TESTING") == "" {
os.Setenv("GO_TESTING", "1")
}
}