Files
core/paths/search_test.go
terminar 13801bec11 Add platform-independent path/config resolution library
Implements core/paths (Config/Data/State/Cache/Runtime/Log/Bin
directory resolution with canonical-vs-search-dir separation, XDG on
Unix, native APPDATA/LOCALAPPDATA/PROGRAMDATA layering on Windows,
~/Library on macOS Desktop, injectable environment/platform for
deterministic cross-platform tests) and core/config (file search,
first-match resolution, layered YAML merging, safe ${VAR} expansion,
Explain/doctor introspection) on top of the shared Application/
Kind/Scope model in the core package.
2026-09-19 16:26:01 +02:00

86 lines
2.7 KiB
Go

package paths
import (
"os"
"path/filepath"
"testing"
)
func TestFindConfigFirstMatch(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithRoot(root))
// Nothing exists yet.
if _, found, err := r.FindConfig("yet.yaml"); err != nil || found {
t.Fatalf("FindConfig() = found=%v err=%v, want found=false", found, err)
}
if err := os.MkdirAll(filepath.Join(root, "etc", "yet"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "etc", "yet", "yet.yaml"), []byte("system: true\n"), 0o644); err != nil {
t.Fatal(err)
}
result, found, err := r.FindConfig("yet.yaml")
if err != nil || !found {
t.Fatalf("FindConfig() = found=%v err=%v, want found=true", found, err)
}
if result.Source != SystemDirectory {
t.Errorf("FindConfig() source = %v, want SystemDirectory", result.Source)
}
// Now also create the higher-priority user config; it should win.
if err := os.MkdirAll(filepath.Join(root, "home", "test", ".config", "yet"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "home", "test", ".config", "yet", "yet.yaml"), []byte("user: true\n"), 0o644); err != nil {
t.Fatal(err)
}
result, found, err = r.FindConfig("yet.yaml")
if err != nil || !found {
t.Fatalf("FindConfig() = found=%v err=%v, want found=true", found, err)
}
if result.Source == SystemDirectory {
t.Errorf("FindConfig() should prefer the user layer once it exists, got source=%v path=%v", result.Source, result.Path)
}
}
func TestEnsureConfigDirCreatesDirectory(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithRoot(root))
dir, err := r.EnsureConfigDir()
if err != nil {
t.Fatalf("EnsureConfigDir: %v", err)
}
info, err := os.Stat(dir)
if err != nil {
t.Fatalf("stat %s: %v", dir, err)
}
if !info.IsDir() {
t.Errorf("%s is not a directory", dir)
}
}
func TestEnsureRuntimeDirPropagatesUnavailable(t *testing.T) {
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
if _, err := r.EnsureRuntimeDir(); err != ErrRuntimeDirUnavailable {
t.Fatalf("EnsureRuntimeDir() error = %v, want ErrRuntimeDirUnavailable", err)
}
}
func TestFindFileNotFoundIsNotAnError(t *testing.T) {
dirs := []SearchResult{{Path: "/does/not/exist", Source: SystemDirectory}}
_, found, err := FindFile(dirs, "missing.yaml")
if err != nil {
t.Fatalf("FindFile() error = %v, want nil", err)
}
if found {
t.Fatalf("FindFile() found = true, want false")
}
}