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.
This commit is contained in:
terminar committed 2026-09-19 16:26:01 +02:00
1 parent 027b327c3d
commit 13801bec11
41 files changed
+4834 -2

No files matched your search

+193
View File
@@ -0,0 +1,193 @@
package config
import (
"os"
"path/filepath"
"testing"
"yetano.dev/yetano/core"
"yetano.dev/yetano/core/paths"
)
func yetApp() core.Application {
return core.Application{Name: "yet", Kind: core.CLI}
}
func mustResolver(t *testing.T, app core.Application, opts ...Option) *Resolver {
t.Helper()
r, err := New(app, opts...)
if err != nil {
t.Fatalf("New: %v", err)
}
return r
}
func writeFile(t *testing.T, path, content string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
func TestLinuxLayerOrder(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithWorkdirPath(filepath.Join(root, "workdir")),
)
layers, err := r.ResolveAll()
if err != nil {
t.Fatalf("ResolveAll: %v", err)
}
want := []string{
filepath.Join(root, "workdir", "yet.yaml"),
filepath.Join(root, "home", "test", ".config", "yet", "yet.yaml"),
filepath.Join(root, "etc", "yet", "yet.yaml"),
}
if len(layers) != len(want) {
t.Fatalf("ResolveAll() = %v, want %v", layers, want)
}
for i, w := range want {
if layers[i].Path != w {
t.Errorf("layers[%d].Path = %q, want %q", i, layers[i].Path, w)
}
}
}
func TestWorkdirDisabled(t *testing.T) {
root := t.TempDir()
writeFile(t, filepath.Join(root, "workdir", "yet.yaml"), "ignored: true\n")
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithWorkdirPath(filepath.Join(root, "workdir")),
WithWorkingDirectory(false),
)
layers, err := r.ResolveAll()
if err != nil {
t.Fatalf("ResolveAll: %v", err)
}
for _, l := range layers {
if l.Source == paths.WorkingDirectory {
t.Fatalf("ResolveAll() included a working-directory layer despite WithWorkingDirectory(false): %v", layers)
}
}
}
func TestSystemConfigDisabled(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithSystemConfig(false),
)
layers, err := r.ResolveAll()
if err != nil {
t.Fatalf("ResolveAll: %v", err)
}
for _, l := range layers {
if l.Priority <= paths.PrioritySystem {
t.Errorf("ResolveAll() included a system layer despite WithSystemConfig(false): %v", layers)
}
}
}
func TestResolveFirstMatch(t *testing.T) {
root := t.TempDir()
writeFile(t, filepath.Join(root, "etc", "yet", "yet.yaml"), "layer: system\n")
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithWorkdirPath(filepath.Join(root, "workdir")),
)
layer, found, err := r.Resolve()
if err != nil {
t.Fatalf("Resolve: %v", err)
}
if !found {
t.Fatal("Resolve() found = false, want true")
}
if want := filepath.Join(root, "etc", "yet", "yet.yaml"); layer.Path != want {
t.Errorf("Resolve().Path = %q, want %q", layer.Path, want)
}
writeFile(t, filepath.Join(root, "home", "test", ".config", "yet", "yet.yaml"), "layer: user\n")
layer, found, err = r.Resolve()
if err != nil {
t.Fatalf("Resolve: %v", err)
}
if !found {
t.Fatal("Resolve() found = false, want true")
}
if want := filepath.Join(root, "home", "test", ".config", "yet", "yet.yaml"); layer.Path != want {
t.Errorf("Resolve().Path = %q, want %q (user should now win over system)", layer.Path, want)
}
}
func TestResolveNotFoundIsNotAnError(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithWorkdirPath(filepath.Join(root, "workdir")),
)
_, found, err := r.Resolve()
if err != nil {
t.Fatalf("Resolve() error = %v, want nil", err)
}
if found {
t.Fatalf("Resolve() found = true, want false")
}
}
func TestYetanoDefaultsFallbackLayer(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithWorkdirPath(filepath.Join(root, "workdir")),
WithYetanoDefaults(true),
)
layers, err := r.ResolveAll()
if err != nil {
t.Fatalf("ResolveAll: %v", err)
}
last := layers[len(layers)-1]
if want := filepath.Join(root, "home", "test", ".config", "yetano", "yet.yaml"); last.Path != want {
t.Errorf("last layer = %q, want %q", last.Path, want)
}
if last.Priority != PriorityYetanoDefaults {
t.Errorf("last layer priority = %d, want %d", last.Priority, PriorityYetanoDefaults)
}
}
func TestWithFilename(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(),
WithEnvironment(env),
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
WithFilename("repositories.yaml"),
WithWorkingDirectory(false),
)
layers, err := r.ResolveAll()
if err != nil {
t.Fatalf("ResolveAll: %v", err)
}
if want := filepath.Join(root, "home", "test", ".config", "yet", "repositories.yaml"); layers[0].Path != want {
t.Errorf("layers[0].Path = %q, want %q", layers[0].Path, want)
}
}