summaryrefslogtreecommitdiff
path: root/internal/config/config_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/config/config_test.go')
-rw-r--r--internal/config/config_test.go105
1 files changed, 51 insertions, 54 deletions
diff --git a/internal/config/config_test.go b/internal/config/config_test.go
index 36b2e97..29a1824 100644
--- a/internal/config/config_test.go
+++ b/internal/config/config_test.go
@@ -1,8 +1,11 @@
package config
import (
+ "errors"
+ "fmt"
"os"
"path/filepath"
+ "regexp"
"strings"
"testing"
)
@@ -14,6 +17,28 @@ func noSystemConfig(t *testing.T) {
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))
+ // t.Errorf("%s", string(result))
+
+ if err = os.WriteFile(path, result, 0o644); err != nil {
+ t.Fatalf("write conf: %v", err)
+ }
+
+ return path
+}
+
func writeConf(t *testing.T, body string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "openstore.conf")
@@ -23,23 +48,16 @@ func writeConf(t *testing.T, body string) string {
return path
}
-func TestLoadMissingFileUsesDefaults(t *testing.T) {
+func TestRejectLoadingMissingFile(t *testing.T) {
noSystemConfig(t)
- cfg, err := Load(filepath.Join(t.TempDir(), "nonexistent.conf"))
- if err != nil {
- t.Fatalf("load with no config file failed: %v", err)
- }
-
- if cfg.Serve.Addr != ":3000" {
- t.Errorf("addr: got %q, want \":3000\"", cfg.Serve.Addr)
- }
- if cfg.Instance.Name != "Openstore" {
- t.Errorf("name: got %q, want \"Openstore\"", cfg.Instance.Name)
+ _, err := Load(filepath.Join(t.TempDir(), "nonexistent.conf"))
+ if err == nil {
+ t.Fatalf("no error when loading missing config: %v", err)
}
}
-func TestLoadFullFileOverridesDefaults(t *testing.T) {
+func TestLoadFullConfig(t *testing.T) {
noSystemConfig(t)
cfg, err := Load("testdata/full.conf")
@@ -56,6 +74,9 @@ func TestLoadFullFileOverridesDefaults(t *testing.T) {
{"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 {
@@ -65,25 +86,6 @@ func TestLoadFullFileOverridesDefaults(t *testing.T) {
}
}
-func TestLoadPartialFileKeepsDefaults(t *testing.T) {
- noSystemConfig(t)
-
- cfg, err := Load("testdata/partial.conf")
- if err != nil {
- t.Fatalf("load: %v", err)
- }
-
- if cfg.Instance.Name != "Only Name Set" {
- t.Errorf("name: got %q, want \"Only Name Set\"", cfg.Instance.Name)
- }
- if cfg.Serve.Addr != ":3000" {
- t.Errorf("unset addr should keep default, got %q", cfg.Serve.Addr)
- }
- if cfg.Instance.BaseURL != "https://www.example.com" {
- t.Errorf("unset base-url should keep default, got %q", cfg.Instance.BaseURL)
- }
-}
-
func TestLoadUserConfigWinsOverSystem(t *testing.T) {
old := systemConfigPath
systemConfigPath = "testdata/system.conf"
@@ -128,12 +130,17 @@ func TestLoadRejectsBadBaseURL(t *testing.T) {
for name, badURL := range tests {
t.Run(name, func(t *testing.T) {
- path := writeConf(t, "[instance]\nbase-url = "+badURL+"\n")
+ path := setConfKey(t, "base-url", badURL)
+
+ cfg, err := Load(path)
+ if cfg != nil {
+ t.Error("config with bad base-url created")
+ }
- _, err := Load(path)
- if err == nil {
+ 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)
}
@@ -141,24 +148,6 @@ func TestLoadRejectsBadBaseURL(t *testing.T) {
}
}
-func TestLoadEmptyValueKeepsDefault(t *testing.T) {
- noSystemConfig(t)
-
- path := writeConf(t, "[instance]\nname = \nbase-url = \n")
-
- cfg, err := Load(path)
- if err != nil {
- t.Fatalf("load: %v", err)
- }
-
- if cfg.Instance.Name != "Openstore" {
- t.Errorf("empty value should leave default, got name %q", cfg.Instance.Name)
- }
- if cfg.Instance.BaseURL != "https://www.example.com" {
- t.Errorf("empty value should leave default, got base-url %q", cfg.Instance.BaseURL)
- }
-}
-
func TestLoadAcceptsGoodBaseURL(t *testing.T) {
noSystemConfig(t)
@@ -171,11 +160,11 @@ func TestLoadAcceptsGoodBaseURL(t *testing.T) {
for _, u := range urls {
t.Run(u, func(t *testing.T) {
- path := writeConf(t, "[instance]\nbase-url = "+u+"\n")
+ path := setConfKey(t, "base-url", u)
cfg, err := Load(path)
if err != nil {
- t.Fatalf("good base-url %q rejected: %v", u, err)
+ 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)
@@ -184,6 +173,14 @@ func TestLoadAcceptsGoodBaseURL(t *testing.T) {
}
}
+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")
+ }
+}
+
func TestDefaultPath(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", "/tmp/xdg-test")