config_test.go
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
// writeConfig drops config text into a temp dir and returns the file path.
func writeConfig(t *testing.T, text string) string {
t.Helper()
p := filepath.Join(t.TempDir(), "vitrine.cfg")
if err := os.WriteFile(p, []byte(text), 0o644); err != nil {
t.Fatal(err)
}
return p
}
func TestConfigValid(t *testing.T) {
p := writeConfig(t, `
exhibits:
demo:
path: /srv/demo
meta/research/thing:
path: /srv/thing
expose-git: true
`)
cfg, err := loadConfig(p)
if err != nil {
t.Fatal(err)
}
if got := cfg.Exhibits["demo"]; got.Path != "/srv/demo" || got.ExposeGit {
t.Errorf("demo = %+v", got)
}
if got := cfg.Exhibits["meta/research/thing"]; got.Path != "/srv/thing" || !got.ExposeGit {
t.Errorf("meta/research/thing = %+v", got)
}
}
func TestConfigRelativePathResolvesAgainstConfigDir(t *testing.T) {
p := writeConfig(t, `
exhibits:
demo:
path: sub/tree
updemo:
path: ../elsewhere
`)
cfg, err := loadConfig(p)
if err != nil {
t.Fatal(err)
}
dir := filepath.Dir(p)
if want := filepath.Join(dir, "sub/tree"); cfg.Exhibits["demo"].Path != want {
t.Errorf("demo path = %q, want %q", cfg.Exhibits["demo"].Path, want)
}
if want := filepath.Join(filepath.Dir(dir), "elsewhere"); cfg.Exhibits["updemo"].Path != want {
t.Errorf("updemo path = %q, want %q", cfg.Exhibits["updemo"].Path, want)
}
}
func TestConfigUnknownFieldRejected(t *testing.T) {
p := writeConfig(t, `
exhibits:
demo:
path: /srv/demo
expose-gti: true
`)
if _, err := loadConfig(p); err == nil {
t.Fatal("want error for unknown field, got nil")
}
}
func TestConfigMissingPathRejected(t *testing.T) {
p := writeConfig(t, `
exhibits:
demo:
expose-git: true
`)
_, err := loadConfig(p)
if err == nil || !strings.Contains(err.Error(), "path") {
t.Fatalf("want missing-path error, got %v", err)
}
}
func TestConfigEmptyExhibitsRejected(t *testing.T) {
for name, text := range map[string]string{
"empty-map": "exhibits: {}\n",
"absent-key": "# nothing here\n",
} {
p := writeConfig(t, text)
if _, err := loadConfig(p); err == nil {
t.Errorf("%s: want error for empty exhibits, got nil", name)
}
}
}
func TestConfigMissingFileRejected(t *testing.T) {
if _, err := loadConfig(filepath.Join(t.TempDir(), "nope.cfg")); err == nil {
t.Fatal("want error for missing file, got nil")
}
}
config_test.go · 2.4 KiB · 1a5958b537