package config import ( "os" "path/filepath" "testing" ) func TestLoad(t *testing.T) { // Create a temporary config file tmpDir := t.TempDir() configFile := filepath.Join(tmpDir, "config.yaml") yamlContent := []byte(` logging: level: "info" server: host: "localhost" port: 8080 `) if err := os.WriteFile(configFile, yamlContent, 0644); err != nil { t.Fatalf("failed to write temp config file: %v", err) } t.Run("Load from YAML", func(t *testing.T) { cfg, err := Load(configFile, "TEST") if err != nil { t.Fatalf("Load() error = %v", err) } if cfg.Logging.Level != "info" { t.Errorf("expected Logging.Level 'info', got '%s'", cfg.Logging.Level) } if cfg.Server.Host != "localhost" { t.Errorf("expected Server.Host 'localhost', got '%s'", cfg.Server.Host) } if cfg.Server.Port != 8080 { t.Errorf("expected Server.Port 8080, got %d", cfg.Server.Port) } }) t.Run("Override from Env", func(t *testing.T) { os.Setenv("TEST_LOGGING_LEVEL", "debug") os.Setenv("TEST_SERVER_PORT", "9090") os.Setenv("TEST_SERVER_HOST", "0.0.0.0") defer func() { os.Unsetenv("TEST_LOGGING_LEVEL") os.Unsetenv("TEST_SERVER_PORT") os.Unsetenv("TEST_SERVER_HOST") }() cfg, err := Load(configFile, "TEST") if err != nil { t.Fatalf("Load() error = %v", err) } if cfg.Logging.Level != "debug" { t.Errorf("expected Logging.Level 'debug', got '%s'", cfg.Logging.Level) } if cfg.Server.Port != 9090 { t.Errorf("expected Server.Port 9090, got %d", cfg.Server.Port) } if cfg.Server.Host != "0.0.0.0" { t.Errorf("expected Server.Host '0.0.0.0', got '%s'", cfg.Server.Host) } }) }