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 TestExpandStringBraceForm(t *testing.T) {
|
||||
env := paths.MapEnvironment{"HOME": "/home/test"}
|
||||
if got, want := expandString("${HOME}/data", env), "/home/test/data"; got != want {
|
||||
t.Errorf("expandString() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandStringBareForm(t *testing.T) {
|
||||
env := paths.MapEnvironment{"HOME": "/home/test"}
|
||||
if got, want := expandString("$HOME/data", env), "/home/test/data"; got != want {
|
||||
t.Errorf("expandString() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandStringUnknownVariableLeftUnexpanded(t *testing.T) {
|
||||
env := paths.MapEnvironment{"HOME": "/home/test"}
|
||||
if got, want := expandString("${MISSING}/data", env), "${MISSING}/data"; got != want {
|
||||
t.Errorf("expandString() = %q, want %q (unset var left literal)", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandStringSetButEmptyExpandsToEmpty(t *testing.T) {
|
||||
env := paths.MapEnvironment{"EMPTY": ""}
|
||||
if got, want := expandString("[${EMPTY}]", env), "[]"; got != want {
|
||||
t.Errorf("expandString() = %q, want %q (set-but-empty expands to empty string)", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandStringNoCommandSubstitution(t *testing.T) {
|
||||
env := paths.MapEnvironment{}
|
||||
input := "$(rm -rf /) `echo hi` plain text"
|
||||
if got := expandString(input, env); got != input {
|
||||
t.Errorf("expandString() = %q, want unchanged %q (no shell eval)", got, input)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandStringWindowsPathValue(t *testing.T) {
|
||||
env := paths.MapEnvironment{"LOCALAPPDATA": `C:\Users\test\AppData\Local`}
|
||||
got := expandString(`${LOCALAPPDATA}\yet\objects`, env)
|
||||
if want := `C:\Users\test\AppData\Local\yet\objects`; got != want {
|
||||
t.Errorf("expandString() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandTreeOnlyExpandsStringValues(t *testing.T) {
|
||||
env := paths.MapEnvironment{"HOME": "/home/test"}
|
||||
tree := map[string]any{
|
||||
"path": "${HOME}/data",
|
||||
"count": 5,
|
||||
"list": []any{"${HOME}/a", 1, true},
|
||||
"nested": map[string]any{
|
||||
"path": "${HOME}/b",
|
||||
},
|
||||
}
|
||||
got := expandTree(tree, env).(map[string]any)
|
||||
if got["path"] != "/home/test/data" {
|
||||
t.Errorf("path = %v, want /home/test/data", got["path"])
|
||||
}
|
||||
if got["count"] != 5 {
|
||||
t.Errorf("count = %v, want 5 (non-string scalar untouched)", got["count"])
|
||||
}
|
||||
list := got["list"].([]any)
|
||||
if list[0] != "/home/test/a" || list[1] != 1 || list[2] != true {
|
||||
t.Errorf("list = %v, want [/home/test/a 1 true]", list)
|
||||
}
|
||||
nested := got["nested"].(map[string]any)
|
||||
if nested["path"] != "/home/test/b" {
|
||||
t.Errorf("nested.path = %v, want /home/test/b", nested["path"])
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user