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

+95
View File
@@ -0,0 +1,95 @@
package paths
import (
"testing"
"yetano.dev/yetano/core"
)
func TestDarwinCLIUsesXDGLikeLayout(t *testing.T) {
env := map[string]string{"HOME": "/Users/test"}
r := mustResolver(t, yetApp(), WithPlatform("darwin"), WithEnvironment(env))
got, err := r.ConfigDir()
if err != nil {
t.Fatalf("ConfigDir: %v", err)
}
if want := "/Users/test/.config/yet"; got != want {
t.Errorf("ConfigDir() = %q, want %q (macOS CLI should match Linux layout)", got, want)
}
}
func TestDarwinDesktopUsesNativeLayout(t *testing.T) {
env := map[string]string{"HOME": "/Users/test"}
app := core.Application{Name: "yapama", Kind: core.Desktop}
r := mustResolver(t, app, WithPlatform("darwin"), WithEnvironment(env))
checks := []struct {
name string
fn func() (string, error)
want string
}{
{"ConfigDir", r.ConfigDir, "/Users/test/Library/Application Support/yapama"},
{"DataDir", r.DataDir, "/Users/test/Library/Application Support/yapama"},
{"StateDir", r.StateDir, "/Users/test/Library/Application Support/yapama/State"},
{"CacheDir", r.CacheDir, "/Users/test/Library/Caches/yapama"},
{"LogDir", r.LogDir, "/Users/test/Library/Logs/yapama"},
}
for _, c := range checks {
got, err := c.fn()
if err != nil {
t.Fatalf("%s: %v", c.name, err)
}
if got != c.want {
t.Errorf("%s = %q, want %q", c.name, got, c.want)
}
}
}
func TestDarwinDesktopSystemScope(t *testing.T) {
app := core.Application{Name: "yapama", Kind: core.Desktop, Scope: core.SystemScope}
r := mustResolver(t, app, WithPlatform("darwin"), WithEnvironment(map[string]string{}))
got, err := r.ConfigDir()
if err != nil {
t.Fatalf("ConfigDir: %v", err)
}
if want := "/Library/Application Support/yapama"; got != want {
t.Errorf("ConfigDir() = %q, want %q", got, want)
}
}
func TestDarwinDesktopConfigSearchDirsUserOverridesSystem(t *testing.T) {
env := map[string]string{"HOME": "/Users/test"}
app := core.Application{Name: "yapama", Kind: core.Desktop}
r := mustResolver(t, app, WithPlatform("darwin"), WithEnvironment(env))
dirs, err := r.ConfigSearchDirs()
if err != nil {
t.Fatalf("ConfigSearchDirs: %v", err)
}
want := []string{
"/Users/test/Library/Application Support/yapama",
"/Library/Application Support/yapama",
}
if len(dirs) != len(want) {
t.Fatalf("ConfigSearchDirs() = %v, want %v", dirs, want)
}
for i, w := range want {
if dirs[i].Path != w {
t.Errorf("ConfigSearchDirs()[%d].Path = %q, want %q", i, dirs[i].Path, w)
}
}
}
func TestDarwinServiceUsesXDGLikeLayout(t *testing.T) {
env := map[string]string{"HOME": "/Users/test"}
app := core.Application{Name: "yasta", Kind: core.Service}
r := mustResolver(t, app, WithPlatform("darwin"), WithEnvironment(env))
got, err := r.ConfigDir()
if err != nil {
t.Fatalf("ConfigDir: %v", err)
}
if want := "/Users/test/.config/yasta"; got != want {
t.Errorf("ConfigDir() = %q, want %q (macOS Service should match Linux layout)", got, want)
}
}