36 строки
880 B
Go
36 строки
880 B
Go
package netaddr
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
// TestMain loads .env.test before any test runs so DATABASE_* env vars
|
|
// are populated in this test binary. The netaddr package is intentionally
|
|
// low-level and does not import config/env; this TestMain gives it the
|
|
// same environment the rest of the test suite sees without pulling in
|
|
// the app-wide env init.
|
|
func TestMain(m *testing.M) {
|
|
loadEnvIfPresent(".env.test")
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
func loadEnvIfPresent(name string) {
|
|
candidates := []string{name}
|
|
if cwd := os.Getenv("CWD"); cwd != "" {
|
|
candidates = append(candidates, filepath.Join(cwd, name))
|
|
}
|
|
if dir, err := os.Getwd(); err == nil {
|
|
candidates = append(candidates, filepath.Join(dir, name))
|
|
}
|
|
for _, c := range candidates {
|
|
if _, err := os.Stat(c); err == nil {
|
|
_ = godotenv.Load(c)
|
|
return
|
|
}
|
|
}
|
|
}
|