package config import ( "errors" "fmt" "os" "path/filepath" "regexp" "strings" "testing" ) func noSystemConfig(t *testing.T) { t.Helper() old := systemConfigPath systemConfigPath = filepath.Join(t.TempDir(), "absent.conf") t.Cleanup(func() { systemConfigPath = old }) } func setConfKey(t *testing.T, key, value string) string { t.Helper() path := filepath.Join(t.TempDir(), "openstore.conf") buff, err := os.ReadFile("testdata/full.conf") if err != nil { t.Fatalf("read full.conf: %v", err) } expr := fmt.Sprintf("(%s)\\s*=.*", key) repl := fmt.Sprintf("${1} = %s", value) re := regexp.MustCompile(expr) result := re.ReplaceAll(buff, []byte(repl)) if err = os.WriteFile(path, result, 0o644); err != nil { t.Fatalf("write conf: %v", err) } return path } func TestRejectLoadingMissingFile(t *testing.T) { noSystemConfig(t) _, err := Load(filepath.Join(t.TempDir(), "nonexistent.conf")) if err == nil { t.Fatalf("no error when loading missing config: %v", err) } } func TestLoadFullConfig(t *testing.T) { noSystemConfig(t) cfg, err := Load("testdata/full.conf") if err != nil { t.Fatalf("load: %v", err) } tests := []struct { field string got string want string }{ {"addr", cfg.Serve.Addr, "127.0.0.1:8080"}, {"name", cfg.Instance.Name, "Full Store"}, {"contact-email", cfg.Instance.ContactEmail, "full@store.com"}, {"base-url", cfg.Instance.BaseURL, "https://full.store.com"}, {"database-name", cfg.Database.Name, "openstore"}, {"database-user", cfg.Database.User, "openstore"}, {"database-password", cfg.Database.Password, "12345678"}, } for _, tt := range tests { if tt.got != tt.want { t.Errorf("%s: got %q, want %q", tt.field, tt.got, tt.want) } } } func TestLoadUserConfigWinsOverSystem(t *testing.T) { old := systemConfigPath systemConfigPath = "testdata/system.conf" t.Cleanup(func() { systemConfigPath = old }) cfg, err := Load("testdata/partial.conf") if err != nil { t.Fatalf("load: %v", err) } if cfg.Instance.Name != "Only Name Set" { t.Errorf("user config should win, got name %q", cfg.Instance.Name) } if cfg.Instance.ContactEmail != "system@store.com" { t.Errorf("key absent from user config should fall back to system, got %q", cfg.Instance.ContactEmail) } if cfg.Serve.Addr != "127.0.0.1:9999" { t.Errorf("section absent from user config should fall back to system, got %q", cfg.Serve.Addr) } } func TestLoadMalformedFile(t *testing.T) { noSystemConfig(t) if _, err := Load("testdata/malformed.conf"); err == nil { t.Error("malformed config file loaded without error") } } func TestLoadRejectsBadBaseURL(t *testing.T) { noSystemConfig(t) tests := map[string]string{ "not a url": "garbage", "no scheme": "example.com/path", "scheme only": "https://", "path only": "/listings", "bad url syntax": "://x", } for name, badURL := range tests { t.Run(name, func(t *testing.T) { path := setConfKey(t, "base-url", badURL) cfg, err := Load(path) if cfg != nil { t.Error("config with bad base-url created") } if !errors.Is(err, ErrBadConfigUrl) { t.Fatalf("bad base-url %q accepted", badURL) } if !strings.Contains(err.Error(), "base-url") { t.Errorf("error should mention base-url, got: %v", err) } }) } } func TestLoadAcceptsGoodBaseURL(t *testing.T) { noSystemConfig(t) urls := []string{ "https://example.com", "http://example.com", "https://example.com/subpath", "https://sub.example.com:8443", } for _, u := range urls { t.Run(u, func(t *testing.T) { path := setConfKey(t, "base-url", u) cfg, err := Load(path) if err != nil { t.Fatalf("good url %q rejected: %v", u, err) } if cfg.Instance.BaseURL != u { t.Errorf("base-url: got %q, want %q", cfg.Instance.BaseURL, u) } }) } } func TetsRejectsConfigWithoutDatabase(t *testing.T) { noSystemConfig(t) _, err := Load("testdata/no-db.conf") if err == nil { t.Errorf("Load returned no error when database config is missing") } if !errors.Is(err, ErrBadConfig) { t.Errorf("Got error %v, wanted %v", err, ErrBadConfig) } } func TestDefaultPath(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", "/tmp/xdg-test") path, err := DefaultPath() if err != nil { t.Fatalf("DefaultPath: %v", err) } want := filepath.Join("/tmp/xdg-test", "openstore", "openstore.conf") if path != want { t.Errorf("got %q, want %q", path, want) } } func TestDefaultPathWithoutConfigDir(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", "") t.Setenv("HOME", "") if _, err := DefaultPath(); err == nil { t.Error("DefaultPath returned no error with no config dir available") } }