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.
216 lines
6.2 KiB
Go
216 lines
6.2 KiB
Go
package paths
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"yetano.dev/yetano/core"
|
|
)
|
|
|
|
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 TestWindowsCLIDefaults(t *testing.T) {
|
|
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(windowsEnv()))
|
|
|
|
checks := []struct {
|
|
name string
|
|
fn func() (string, error)
|
|
want string
|
|
}{
|
|
{"ConfigDir", r.ConfigDir, `C:\Users\test\AppData\Roaming\yet`},
|
|
{"DataDir", r.DataDir, `C:\Users\test\AppData\Local\yet`},
|
|
{"StateDir", r.StateDir, `C:\Users\test\AppData\Local\yet\state`},
|
|
{"CacheDir", r.CacheDir, `C:\Users\test\AppData\Local\yet\cache`},
|
|
{"LogDir", r.LogDir, `C:\Users\test\AppData\Local\yet\log`},
|
|
{"RuntimeDir", r.RuntimeDir, `C:\Users\test\AppData\Local\yet\runtime`},
|
|
{"BinDir", r.BinDir, `C:\Users\test\AppData\Local\Yetano\bin`},
|
|
}
|
|
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)
|
|
}
|
|
if containsForwardSlash(got) {
|
|
t.Errorf("%s = %q contains a forward slash, want only backslashes", c.name, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWindowsConfigSearchDirsFirstMatchOrder(t *testing.T) {
|
|
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(windowsEnv()))
|
|
dirs, err := r.ConfigSearchDirs()
|
|
if err != nil {
|
|
t.Fatalf("ConfigSearchDirs: %v", err)
|
|
}
|
|
want := []string{
|
|
`C:\Users\test\AppData\Local\yet`,
|
|
`C:\Users\test\AppData\Roaming\yet`,
|
|
`C:\ProgramData\yet`,
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
// The canonical write target (APPDATA) is not the highest-priority
|
|
// search entry (LOCALAPPDATA) -- spec section 46.
|
|
canonical, err := r.ConfigDir()
|
|
if err != nil {
|
|
t.Fatalf("ConfigDir: %v", err)
|
|
}
|
|
if canonical != want[1] {
|
|
t.Errorf("ConfigDir() = %q, want %q (the roaming/APPDATA layer)", canonical, want[1])
|
|
}
|
|
if dirs[0].Path == canonical {
|
|
t.Errorf("highest-priority search entry unexpectedly equals the canonical write target")
|
|
}
|
|
}
|
|
|
|
func TestWindowsDataSearchDirs(t *testing.T) {
|
|
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(windowsEnv()))
|
|
dirs, err := r.DataSearchDirs()
|
|
if err != nil {
|
|
t.Fatalf("DataSearchDirs: %v", err)
|
|
}
|
|
want := []string{
|
|
`C:\Users\test\AppData\Local\yet`,
|
|
`C:\ProgramData\yet`,
|
|
}
|
|
if len(dirs) != len(want) {
|
|
t.Fatalf("DataSearchDirs() = %v, want %v", dirs, want)
|
|
}
|
|
for i, w := range want {
|
|
if dirs[i].Path != w {
|
|
t.Errorf("DataSearchDirs()[%d].Path = %q, want %q", i, dirs[i].Path, w)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWindowsSystemScope(t *testing.T) {
|
|
app := core.Application{Name: "yasta", Kind: core.Service, Scope: core.SystemScope}
|
|
r := mustResolver(t, app, WithPlatform("windows"), WithEnvironment(windowsEnv()))
|
|
|
|
want := `C:\ProgramData\yasta`
|
|
got, err := r.ConfigDir()
|
|
if err != nil {
|
|
t.Fatalf("ConfigDir: %v", err)
|
|
}
|
|
if got != want {
|
|
t.Errorf("ConfigDir() = %q, want %q", got, want)
|
|
}
|
|
|
|
dirs, err := r.ConfigSearchDirs()
|
|
if err != nil {
|
|
t.Fatalf("ConfigSearchDirs: %v", err)
|
|
}
|
|
if len(dirs) != 1 || dirs[0].Path != want {
|
|
t.Errorf("SystemScope ConfigSearchDirs() = %v, want exactly [%s]", dirs, want)
|
|
}
|
|
|
|
bin, err := r.BinDir()
|
|
if err != nil {
|
|
t.Fatalf("BinDir: %v", err)
|
|
}
|
|
if wantBin := `C:\ProgramData\Yetano\bin`; bin != wantBin {
|
|
t.Errorf("BinDir() = %q, want %q", bin, wantBin)
|
|
}
|
|
}
|
|
|
|
func TestWindowsUserScopeMergeOrderMirrorsSearchOrderReversed(t *testing.T) {
|
|
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(windowsEnv()))
|
|
dirs, err := r.ConfigSearchDirs()
|
|
if err != nil {
|
|
t.Fatalf("ConfigSearchDirs: %v", err)
|
|
}
|
|
for i := 0; i < len(dirs)-1; i++ {
|
|
if dirs[i].Priority < dirs[i+1].Priority {
|
|
t.Fatalf("ConfigSearchDirs() not in descending priority order: %v", dirs)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWindowsDesktopSimplerLayering(t *testing.T) {
|
|
app := core.Application{Name: "yapama", Kind: core.Desktop}
|
|
r := mustResolver(t, app, WithPlatform("windows"), WithEnvironment(windowsEnv()))
|
|
|
|
dirs, err := r.ConfigSearchDirs()
|
|
if err != nil {
|
|
t.Fatalf("ConfigSearchDirs: %v", err)
|
|
}
|
|
want := []string{
|
|
`C:\Users\test\AppData\Roaming\yapama`,
|
|
`C:\ProgramData\yapama`,
|
|
}
|
|
if len(dirs) != len(want) {
|
|
t.Fatalf("Desktop ConfigSearchDirs() = %v, want %v (no LOCALAPPDATA override layer)", 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 TestWindowsMissingHomeIsCleanError(t *testing.T) {
|
|
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(map[string]string{}))
|
|
if _, err := r.ConfigDir(); err != ErrNoHomeDirectory {
|
|
t.Fatalf("ConfigDir() error = %v, want ErrNoHomeDirectory", err)
|
|
}
|
|
}
|
|
|
|
func TestWindowsAPIOverride(t *testing.T) {
|
|
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(windowsEnv()), WithConfigDir(`D:\portable\yet-config`))
|
|
got, err := r.ConfigDir()
|
|
if err != nil {
|
|
t.Fatalf("ConfigDir: %v", err)
|
|
}
|
|
if want := `D:\portable\yet-config`; got != want {
|
|
t.Errorf("ConfigDir() = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestWithRootRebasesWindowsPath(t *testing.T) {
|
|
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(windowsEnv()), WithRoot(`C:\tmp\test-root`))
|
|
got, err := r.ConfigDir()
|
|
if err != nil {
|
|
t.Fatalf("ConfigDir: %v", err)
|
|
}
|
|
if want := `C:\tmp\test-root\Users\test\AppData\Roaming\yet`; got != want {
|
|
t.Errorf("ConfigDir() = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestWithRootRebasesWindowsUNCPath(t *testing.T) {
|
|
env := windowsEnv()
|
|
env["APPDATA"] = `\\server\share\test\AppData\Roaming`
|
|
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(env), WithRoot(`C:\tmp\test-root`))
|
|
got, err := r.ConfigDir()
|
|
if err != nil {
|
|
t.Fatalf("ConfigDir: %v", err)
|
|
}
|
|
if want := `C:\tmp\test-root\test\AppData\Roaming\yet`; got != want {
|
|
t.Errorf("ConfigDir() = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func containsForwardSlash(s string) bool {
|
|
for _, c := range s {
|
|
if c == '/' {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|