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:
1 parent
027b327c3d
commit
13801bec11
41 files changed
+4834
-2
No files matched your search
@@ -0,0 +1,78 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"yetano.dev/yetano/core/paths"
|
||||
)
|
||||
|
||||
func windowsEnv() map[string]string {
|
||||
return map[string]string{
|
||||
"PROGRAMDATA": `C:\ProgramData`,
|
||||
"APPDATA": `C:\Users\test\AppData\Roaming`,
|
||||
"LOCALAPPDATA": `C:\Users\test\AppData\Local`,
|
||||
"USERPROFILE": `C:\Users\test`,
|
||||
}
|
||||
}
|
||||
|
||||
func TestWindowsSearchOrder(t *testing.T) {
|
||||
r := mustResolver(t, yetApp(),
|
||||
WithEnvironment(windowsEnv()),
|
||||
WithPathsOptions(paths.WithPlatform("windows")),
|
||||
WithWorkdirPath(`C:\work\project`),
|
||||
)
|
||||
layers, err := r.ResolveAll()
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveAll: %v", err)
|
||||
}
|
||||
want := []string{
|
||||
`C:\work\project\yet.yaml`,
|
||||
`C:\Users\test\AppData\Local\yet\yet.yaml`,
|
||||
`C:\Users\test\AppData\Roaming\yet\yet.yaml`,
|
||||
`C:\ProgramData\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 TestWindowsMergeOrderIsSearchOrderReversed(t *testing.T) {
|
||||
r := mustResolver(t, yetApp(),
|
||||
WithEnvironment(windowsEnv()),
|
||||
WithPathsOptions(paths.WithPlatform("windows")),
|
||||
WithWorkdirPath(`C:\work\project`),
|
||||
)
|
||||
layers, err := r.ResolveAll()
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveAll: %v", err)
|
||||
}
|
||||
for i := 0; i < len(layers)-1; i++ {
|
||||
if layers[i].Priority < layers[i+1].Priority {
|
||||
t.Fatalf("ResolveAll() not in descending priority order: %v", layers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWindowsPathSeparatorsNeverForwardSlash(t *testing.T) {
|
||||
r := mustResolver(t, yetApp(),
|
||||
WithEnvironment(windowsEnv()),
|
||||
WithPathsOptions(paths.WithPlatform("windows")),
|
||||
WithWorkdirPath(`C:\work\project`),
|
||||
)
|
||||
layers, err := r.ResolveAll()
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveAll: %v", err)
|
||||
}
|
||||
for _, l := range layers {
|
||||
for _, c := range l.Path {
|
||||
if c == '/' {
|
||||
t.Errorf("layer path %q contains a forward slash", l.Path)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user