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) { // windowsBase's native SHGetKnownFolderPath fallback (see // knownfolders_windows.go) is only consulted when actually running // on a real Windows host (runtime.GOOS == "windows"), regardless of // WithPlatform -- deliberately, so it reflects genuine behavior // there instead of firing under simulation. That means this specific // scenario (empty environment, no override) resolves a *real* // known-folder path on an actual Windows host instead of erroring, // which is correct, intended behavior, not a bug -- but it makes // this test non-deterministic across hosts. Disable the native // fallback here so the "no override, no env, no fallback available" // path is exercised deterministically on every host, including real // Windows (see the Windows-verification note in TODO.md). withoutNativeKnownFolder(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) } } // withoutNativeKnownFolder disables nativeKnownFolder for the duration // of t, restoring its previous value afterwards. func withoutNativeKnownFolder(t *testing.T) { t.Helper() prev := nativeKnownFolder nativeKnownFolder = nil t.Cleanup(func() { nativeKnownFolder = prev }) } 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 }