From d5f49fc99aa512518391086b461ee69fab57594b Mon Sep 17 00:00:00 2001 From: Eric Phillips Date: Tue, 21 Apr 2026 09:15:56 -0600 Subject: [PATCH] added tests for config --- config_test.go | 138 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 config_test.go diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..ee9c077 --- /dev/null +++ b/config_test.go @@ -0,0 +1,138 @@ +package main + +import ( + "errors" + "os" + "path/filepath" + "testing" +) + +func writeConfigFile(t *testing.T, content string) string { + t.Helper() + dir := t.TempDir() + path := filepath.Join(dir, "config.toml") + if err := os.WriteFile(path, []byte(content), 0644); err != nil { + t.Fatalf("failed write test config: %v", err) + } + return path +} + +func TestReadConfig(t *testing.T) { + path := writeConfigFile(t, ` +cache_root = "srv/cache" +mirror_urls = ["https://mirror.example.com"] +mirrored_repos = ["core", "extra"] +port = "8090" + +[auth] +token = "testtoken" +`) + + cfg, err := ReadConfig(path) + if err != nil { + t.Fatalf("expected no err on read got: %v", err) + } + if cfg.Port != "8090" { + t.Errorf("expected port 8090 got %s", cfg.Port) + } +} + +func TestMissingCacheRoot(t *testing.T) { + path := writeConfigFile(t, ` +mirror_urls = ["https://mirror.example.com"] +mirrored_repos = ["core", "extra"] +port = "8090" + +[auth] +token = "testtoken" +`) + + _, err := ReadConfig(path) + if err == nil { + t.Fatal("expected err got nil") + } +} + +func TestMissingMirrorUrls(t *testing.T) { + path := writeConfigFile(t, ` +cache_root = "srv/cache" +mirrored_repos = ["core", "extra"] +port = "8090" + +[auth] +token = "testtoken" +`) + + _, err := ReadConfig(path) + if err == nil { + t.Fatal("expected err got nil") + } +} + +func TestMissingMirroredRepos(t *testing.T) { + path := writeConfigFile(t, ` +cache_root = "srv/cache" +mirror_urls = ["https://mirror.example.com"] +port = "8090" + +[auth] +token = "testtoken" +`) + + _, err := ReadConfig(path) + if err == nil { + t.Fatal("expected err got nil") + } +} + +func TestMissingPort(t *testing.T) { + path := writeConfigFile(t, ` +cache_root = "srv/cache" +mirror_urls = ["https://mirror.example.com"] +mirrored_repos = ["core", "extra"] + +[auth] +token = "testtoken" +`) + + _, err := ReadConfig(path) + if err == nil { + t.Fatal("expected err got nil") + } +} + +func TestMissingAuthToken(t *testing.T) { + path := writeConfigFile(t, ` +cache_root = "srv/cache" +mirror_urls = ["https://mirror.example.com"] +mirrored_repos = ["core", "extra"] +port = "8090" + +[auth] +`) + + _, err := ReadConfig(path) + if err == nil { + t.Fatal("expected err got nil") + } +} + +func TestMissingFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "nonexistant.toml") + + _, err := ReadConfig(path) + if !errors.Is(err, os.ErrNotExist) { + t.Fatal("expected err got nil") + } +} + +func TestInvalidToml(t *testing.T) { + path := writeConfigFile(t, ` +cache_root = [srv/cache] +`) + + _, err := ReadConfig(path) + if err == nil { + t.Fatal("expected err got nil") + } +}