feat(config): 配置加载与 schema 校验(.env/env 注入 + 反射解码 + 校验)
This commit is contained in:
1 parent
0236010136
commit
efed5f05b1
5 files changed
+551
No files matched your search
@@ -0,0 +1,151 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func writeEnvFile(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, ".env")
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatalf("write env file: %v", err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func TestLoad_DefaultsWhenUnset(t *testing.T) {
|
||||
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
||||
|
||||
cfg, err := Load("")
|
||||
if err != nil {
|
||||
t.Fatalf("Load() error: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Env != "development" {
|
||||
t.Errorf("Env = %q, want development", cfg.Env)
|
||||
}
|
||||
if cfg.Server.Host != "0.0.0.0" || cfg.Server.Port != 8080 {
|
||||
t.Errorf("Server = %+v, want 0.0.0.0:8080", cfg.Server)
|
||||
}
|
||||
if cfg.LogLevel != "debug" {
|
||||
t.Errorf("LogLevel = %q, want debug", cfg.LogLevel)
|
||||
}
|
||||
if cfg.DB.SQLitePath != "./data/cyrene.db" {
|
||||
t.Errorf("SQLitePath = %q, want ./data/cyrene.db", cfg.DB.SQLitePath)
|
||||
}
|
||||
want := []string{"http://localhost:5173", "http://localhost:3000"}
|
||||
if !reflect.DeepEqual(cfg.AllowedOrigins, want) {
|
||||
t.Errorf("AllowedOrigins = %v, want %v", cfg.AllowedOrigins, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_EnvOverridesDefault(t *testing.T) {
|
||||
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
||||
t.Setenv("PORT", "9090")
|
||||
t.Setenv("LOG_LEVEL", "warn")
|
||||
t.Setenv("IOT_PROVIDER", "none")
|
||||
|
||||
cfg, err := Load("")
|
||||
if err != nil {
|
||||
t.Fatalf("Load() error: %v", err)
|
||||
}
|
||||
if cfg.Server.Port != 9090 {
|
||||
t.Errorf("Port = %d, want 9090", cfg.Server.Port)
|
||||
}
|
||||
if cfg.LogLevel != "warn" {
|
||||
t.Errorf("LogLevel = %q, want warn", cfg.LogLevel)
|
||||
}
|
||||
if cfg.IoT.Provider != "none" {
|
||||
t.Errorf("IoT.Provider = %q, want none", cfg.IoT.Provider)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_DotEnvThenProcessEnv(t *testing.T) {
|
||||
path := writeEnvFile(t, `PORT=7000
|
||||
LOG_LEVEL=error
|
||||
# comment line
|
||||
IOT_PROVIDER=none
|
||||
`)
|
||||
// Process env overrides .env for PORT but leaves LOG_LEVEL from .env.
|
||||
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
||||
t.Setenv("PORT", "8000")
|
||||
|
||||
cfg, err := Load(path)
|
||||
if err != nil {
|
||||
t.Fatalf("Load(%q) error: %v", path, err)
|
||||
}
|
||||
if cfg.Server.Port != 8000 {
|
||||
t.Errorf("Port = %d, want 8000 (process env wins)", cfg.Server.Port)
|
||||
}
|
||||
if cfg.LogLevel != "error" {
|
||||
t.Errorf("LogLevel = %q, want error from .env", cfg.LogLevel)
|
||||
}
|
||||
if cfg.IoT.Provider != "none" {
|
||||
t.Errorf("IoT.Provider = %q, want none from .env", cfg.IoT.Provider)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_MissingRequiredFails(t *testing.T) {
|
||||
// Do not set JWT_SECRET.
|
||||
if _, err := Load(""); err == nil {
|
||||
t.Fatal("expected error when JWT_SECRET missing, got nil")
|
||||
} else if !strings.Contains(err.Error(), "JWT_SECRET") {
|
||||
t.Errorf("error should mention JWT_SECRET, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_ValidationMinLength(t *testing.T) {
|
||||
t.Setenv("JWT_SECRET", "short")
|
||||
if _, err := Load(""); err == nil {
|
||||
t.Fatal("expected error for JWT_SECRET shorter than 32, got nil")
|
||||
} else if !strings.Contains(err.Error(), "at least 32") {
|
||||
t.Errorf("error should mention min length, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_InvalidPortFails(t *testing.T) {
|
||||
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
||||
t.Setenv("PORT", "not-a-number")
|
||||
if _, err := Load(""); err == nil {
|
||||
t.Fatal("expected error for invalid PORT, got nil")
|
||||
} else if !strings.Contains(err.Error(), "PORT") {
|
||||
t.Errorf("error should mention PORT, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_MissingDotEnvIsTolerated(t *testing.T) {
|
||||
t.Setenv("JWT_SECRET", strings.Repeat("s", 40))
|
||||
// A non-existent .env path must not error.
|
||||
cfg, err := Load(filepath.Join(t.TempDir(), "does-not-exist.env"))
|
||||
if err != nil {
|
||||
t.Fatalf("Load(nonexistent) error: %v", err)
|
||||
}
|
||||
if cfg.Server.Port != 8080 {
|
||||
t.Errorf("Port = %d, want default 8080", cfg.Server.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDotEnv_QuotedAndExport(t *testing.T) {
|
||||
path := writeEnvFile(t, `export FOO=bar
|
||||
BAZ='quoted value'
|
||||
EMPTY=
|
||||
`)
|
||||
m, err := parseDotEnv(path)
|
||||
if err != nil {
|
||||
t.Fatalf("parseDotEnv error: %v", err)
|
||||
}
|
||||
if m["FOO"] != "bar" {
|
||||
t.Errorf("FOO = %q, want bar", m["FOO"])
|
||||
}
|
||||
if m["BAZ"] != "quoted value" {
|
||||
t.Errorf("BAZ = %q, want 'quoted value'", m["BAZ"])
|
||||
}
|
||||
if v, ok := m["EMPTY"]; !ok || v != "" {
|
||||
t.Errorf("EMPTY = %q (present=%v), want empty", v, ok)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user