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.
90 lines
2.8 KiB
Go
90 lines
2.8 KiB
Go
package paths
|
|
|
|
import "testing"
|
|
|
|
func TestUnixJoin(t *testing.T) {
|
|
cases := []struct {
|
|
elems []string
|
|
want string
|
|
}{
|
|
{[]string{"/etc", "yet"}, "/etc/yet"},
|
|
{[]string{"/", "etc", "yet"}, "/etc/yet"},
|
|
{[]string{"relative", "path"}, "relative/path"},
|
|
{[]string{"/a/", "/b/", "c"}, "/a/b/c"},
|
|
}
|
|
for _, c := range cases {
|
|
if got := unixJoin(c.elems...); got != c.want {
|
|
t.Errorf("unixJoin(%v) = %q, want %q", c.elems, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWindowsJoin(t *testing.T) {
|
|
cases := []struct {
|
|
elems []string
|
|
want string
|
|
}{
|
|
{[]string{`C:\Users\test`, "yet"}, `C:\Users\test\yet`},
|
|
{[]string{`C:\Users\test\`, `\yet\`}, `C:\Users\test\yet`},
|
|
{[]string{`\\server\share`, "yet"}, `\\server\share\yet`},
|
|
{[]string{"relative", "path"}, `relative\path`},
|
|
}
|
|
for _, c := range cases {
|
|
if got := windowsJoin(c.elems...); got != c.want {
|
|
t.Errorf("windowsJoin(%v) = %q, want %q", c.elems, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSplitWindowsRoot(t *testing.T) {
|
|
cases := []struct {
|
|
path string
|
|
wantRoot string
|
|
wantRest string
|
|
wantOK bool
|
|
}{
|
|
{`C:\Users\test\yet`, `C:`, `Users\test\yet`, true},
|
|
{`C:\`, `C:`, ``, true},
|
|
{`\\server\share\yet`, `\\server\share`, `yet`, true},
|
|
{`\\server\share`, `\\server\share`, ``, true},
|
|
{`relative\path`, "", "", false},
|
|
}
|
|
for _, c := range cases {
|
|
root, rest, ok := splitWindowsRoot(c.path)
|
|
if ok != c.wantOK || root != c.wantRoot || rest != c.wantRest {
|
|
t.Errorf("splitWindowsRoot(%q) = (%q, %q, %v), want (%q, %q, %v)",
|
|
c.path, root, rest, ok, c.wantRoot, c.wantRest, c.wantOK)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestApplyRootUnix(t *testing.T) {
|
|
if got, want := applyRootUnix("", "/etc/yet"), "/etc/yet"; got != want {
|
|
t.Errorf("applyRootUnix(\"\", ...) = %q, want %q", got, want)
|
|
}
|
|
if got, want := applyRootUnix("/tmp/root", "/etc/yet"), "/tmp/root/etc/yet"; got != want {
|
|
t.Errorf("applyRootUnix = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestApplyRootWindows(t *testing.T) {
|
|
if got, want := applyRootWindows("", `C:\ProgramData\yet`), `C:\ProgramData\yet`; got != want {
|
|
t.Errorf("applyRootWindows(\"\", ...) = %q, want %q", got, want)
|
|
}
|
|
if got, want := applyRootWindows(`C:\sandbox`, `C:\ProgramData\yet`), `C:\sandbox\ProgramData\yet`; got != want {
|
|
t.Errorf("applyRootWindows = %q, want %q", got, want)
|
|
}
|
|
if got, want := applyRootWindows(`C:\sandbox`, `\\server\share\yet`), `C:\sandbox\yet`; got != want {
|
|
t.Errorf("applyRootWindows(UNC) = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestJoinDirFile(t *testing.T) {
|
|
if got, want := JoinDirFile("/etc/yet", "yet.yaml"), "/etc/yet/yet.yaml"; got != want {
|
|
t.Errorf("JoinDirFile(unix) = %q, want %q", got, want)
|
|
}
|
|
if got, want := JoinDirFile(`C:\ProgramData\yet`, "yet.yaml"), `C:\ProgramData\yet\yet.yaml`; got != want {
|
|
t.Errorf("JoinDirFile(windows) = %q, want %q", got, want)
|
|
}
|
|
}
|