89 lines
2.5 KiB
Go
89 lines
2.5 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestDefaults(t *testing.T) {
|
|
cfg, err := Load(LoadOptions{})
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
|
|
if cfg.WeatherAPI.Units != "us" {
|
|
t.Fatalf("Units = %q, want us", cfg.WeatherAPI.Units)
|
|
}
|
|
if cfg.WeatherAPI.Timezone != "Chicago" {
|
|
t.Fatalf("Timezone = %q, want Chicago", cfg.WeatherAPI.Timezone)
|
|
}
|
|
if cfg.WeatherAPI.Format != "json" {
|
|
t.Fatalf("Format = %q, want json", cfg.WeatherAPI.Format)
|
|
}
|
|
if cfg.MissingSource.Default != MissingSourceWarn {
|
|
t.Fatalf("MissingSource.Default = %q, want warn", cfg.MissingSource.Default)
|
|
}
|
|
}
|
|
|
|
func TestLoadExampleConfig(t *testing.T) {
|
|
cfg, err := LoadFile(filepath.Join("..", "..", "examples", "config.yml"))
|
|
if err != nil {
|
|
t.Fatalf("LoadFile() error = %v", err)
|
|
}
|
|
|
|
if cfg.WeatherAPI.BaseURL != "https://weather.api.example.com/" {
|
|
t.Fatalf("BaseURL = %q, want example URL", cfg.WeatherAPI.BaseURL)
|
|
}
|
|
if cfg.WeatherAPI.Timeout != 15*time.Second {
|
|
t.Fatalf("Timeout = %s, want 15s", cfg.WeatherAPI.Timeout)
|
|
}
|
|
if cfg.MissingSource.Sources["alerts"] != MissingSourceNone {
|
|
t.Fatalf("alerts policy = %q, want none", cfg.MissingSource.Sources["alerts"])
|
|
}
|
|
}
|
|
|
|
func TestExplicitMissingConfigReturnsError(t *testing.T) {
|
|
_, err := LoadFile(filepath.Join(t.TempDir(), "missing.yml"))
|
|
if err == nil {
|
|
t.Fatal("LoadFile() error = nil, want missing file error")
|
|
}
|
|
if !strings.Contains(err.Error(), "read config") {
|
|
t.Fatalf("error = %q, want read config context", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestInvalidConfigProducesActionableError(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "config.yml")
|
|
if err := os.WriteFile(path, []byte("missing_source:\n default: explode\n"), 0o600); err != nil {
|
|
t.Fatalf("write config fixture: %v", err)
|
|
}
|
|
|
|
_, err := LoadFile(path)
|
|
if err == nil {
|
|
t.Fatal("LoadFile() error = nil, want validation error")
|
|
}
|
|
if !strings.Contains(err.Error(), "missing_source.default") {
|
|
t.Fatalf("error = %q, want field path", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestLoadAppliesOverrides(t *testing.T) {
|
|
cfg, err := Load(LoadOptions{Units: "metric", Timezone: "+09:30", Output: "./out"})
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if cfg.WeatherAPI.Units != "metric" {
|
|
t.Fatalf("Units = %q, want metric", cfg.WeatherAPI.Units)
|
|
}
|
|
if cfg.WeatherAPI.Timezone != "+09:30" {
|
|
t.Fatalf("Timezone = %q, want +09:30", cfg.WeatherAPI.Timezone)
|
|
}
|
|
if cfg.Reports.OutputDir != "./out" {
|
|
t.Fatalf("OutputDir = %q, want ./out", cfg.Reports.OutputDir)
|
|
}
|
|
}
|