Move config file to XDG config dir

This commit is contained in:
Jan Bader
2026-07-20 20:41:24 +02:00
parent f603bbb8b8
commit 12aa0d4e21
3 changed files with 86 additions and 8 deletions
+47
View File
@@ -0,0 +1,47 @@
package main
import (
"os"
"path/filepath"
"testing"
)
func TestConfigFilePathUsesXDGConfigDir(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
got, legacy, err := configFilePath()
if err != nil {
t.Fatal(err)
}
want := filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "docspell-cli", "config.json")
if got != want {
t.Fatalf("expected %q, got %q", want, got)
}
if legacy != "" {
t.Fatalf("expected no legacy fallback, got %q", legacy)
}
}
func TestConfigFilePathFallsBackToLegacyHomeFile(t *testing.T) {
home := t.TempDir()
xdg := filepath.Join(t.TempDir(), "config")
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", xdg)
legacyPath := filepath.Join(home, "docspell-cli.json")
if err := os.WriteFile(legacyPath, []byte("{}"), 0644); err != nil {
t.Fatal(err)
}
got, legacy, err := configFilePath()
if err != nil {
t.Fatal(err)
}
if got != legacyPath {
t.Fatalf("expected legacy path %q, got %q", legacyPath, got)
}
wantLegacy := filepath.Join(xdg, "docspell-cli", "config.json")
if legacy != wantLegacy {
t.Fatalf("expected XDG target %q, got %q", wantLegacy, legacy)
}
}