81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func unsetEnvForTest(t *testing.T, key string) {
|
|
t.Helper()
|
|
prev, had := os.LookupEnv(key)
|
|
if err := os.Unsetenv(key); err != nil {
|
|
t.Fatalf("failed to unset %s: %v", key, err)
|
|
}
|
|
t.Cleanup(func() {
|
|
if had {
|
|
_ = os.Setenv(key, prev)
|
|
return
|
|
}
|
|
_ = os.Unsetenv(key)
|
|
})
|
|
}
|
|
|
|
func TestLoadDefaults(t *testing.T) {
|
|
unsetEnvForTest(t, "CAIRN_LISTEN_ADDR")
|
|
unsetEnvForTest(t, "CAIRN_DATABASE_URL")
|
|
unsetEnvForTest(t, "CAIRN_S3_ENDPOINT")
|
|
unsetEnvForTest(t, "CAIRN_S3_BUCKET")
|
|
unsetEnvForTest(t, "CAIRN_S3_ACCESS_KEY")
|
|
unsetEnvForTest(t, "CAIRN_S3_SECRET_KEY")
|
|
unsetEnvForTest(t, "CAIRN_S3_USE_SSL")
|
|
unsetEnvForTest(t, "CAIRN_FORGEJO_URL")
|
|
unsetEnvForTest(t, "CAIRN_FORGEJO_TOKEN")
|
|
unsetEnvForTest(t, "CAIRN_FORGEJO_WEBHOOK_SECRET")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("Load returned error: %v", err)
|
|
}
|
|
if cfg.ListenAddr != ":8080" {
|
|
t.Fatalf("expected default listen addr, got %q", cfg.ListenAddr)
|
|
}
|
|
if cfg.DatabaseURL == "" {
|
|
t.Fatal("expected default database URL to be non-empty")
|
|
}
|
|
if cfg.S3UseSSL {
|
|
t.Fatal("expected default S3UseSSL=false")
|
|
}
|
|
}
|
|
|
|
func TestLoadOverrides(t *testing.T) {
|
|
t.Setenv("CAIRN_LISTEN_ADDR", ":9090")
|
|
t.Setenv("CAIRN_DATABASE_URL", "postgres://test/test")
|
|
t.Setenv("CAIRN_S3_USE_SSL", "true")
|
|
t.Setenv("CAIRN_FORGEJO_URL", "https://forgejo.example")
|
|
t.Setenv("CAIRN_FORGEJO_TOKEN", "token")
|
|
t.Setenv("CAIRN_FORGEJO_WEBHOOK_SECRET", "secret")
|
|
|
|
cfg, err := Load()
|
|
if err != nil {
|
|
t.Fatalf("Load returned error: %v", err)
|
|
}
|
|
if cfg.ListenAddr != ":9090" {
|
|
t.Fatalf("expected :9090, got %q", cfg.ListenAddr)
|
|
}
|
|
if cfg.DatabaseURL != "postgres://test/test" {
|
|
t.Fatalf("unexpected database URL: %q", cfg.DatabaseURL)
|
|
}
|
|
if !cfg.S3UseSSL {
|
|
t.Fatal("expected S3UseSSL=true")
|
|
}
|
|
}
|
|
|
|
func TestLoadInvalidBoolReturnsError(t *testing.T) {
|
|
t.Setenv("CAIRN_S3_USE_SSL", "not-a-bool")
|
|
|
|
_, err := Load()
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid CAIRN_S3_USE_SSL")
|
|
}
|
|
}
|