Files
core/paths/coverage_test.go
T
terminarandClaude Sonnet 5 da2ec85381 Refine macOS/LogAlias platform tuning, fix a real WithRoot bug found on Windows
Platform tuning:
- macOS Desktop's RuntimeDir() now prefers $TMPDIR (the OS-managed,
  per-user ephemeral directory NSTemporaryDirectory() returns) over
  the previous ~/Library/Application Support/<App>/Runtime placeholder.
- Resolver.EnsureLogAlias() creates the optional LogAliasDir() symlink
  to LogDir(), idempotently, leaving any pre-existing entry alone.

Verified the SHGetKnownFolderPath fallback (knownfolders_windows.go)
on a real Windows host for the first time: with APPDATA/LOCALAPPDATA/
PROGRAMDATA all unset, it correctly resolves the real known folders.
go vet's one unsafeptr finding there is a documented, permanent,
unavoidable characteristic of this raw-syscall pattern, not a bug.

That same real-Windows run surfaced a genuine, previously-invisible
bug: WithRoot assumed root's own address family always matched the
*simulated* platform's, which silently broke (mixed separators, or an
outright invalid mid-path drive letter) whenever the real host's
native family differed from what WithPlatform was simulating -- e.g.
WithPlatform("linux") with WithRoot(t.TempDir()) on a real Windows
host. This was invisible on macOS/Linux, where t.TempDir() happens to
already share the simulated Unix family.

Fixed applyRoot (now applyRootRebase) to decompose the resolved path
into segments and re-join them onto root using root's own family,
inferred from its content like JoinDirFile already does, not from the
simulated platform. Two call sites chaining onto an already-rooted
value (logDirUnix, darwin.go's State/Runtime fallback) switched from a
hardcoded unixJoin to JoinDirFile for the same reason. core/config's
working-directory layer had the identical bug one level up; fixed via
a new paths.JoinDirFileFamily.

TODO.md/ARCHITECTURE.md/README.md updated accordingly; core/TODO.md's
remaining items are now all explicitly out-of-scope or deliberately
deprioritized, not open work.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-20 16:37:30 +02:00

313 lines
9.3 KiB
Go

package paths
import (
"os"
"testing"
"yetano.dev/yetano/core"
)
func TestResolverAccessors(t *testing.T) {
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(map[string]string{"HOME": "/home/test"}))
if r.Application().Name != "yet" {
t.Errorf("Application().Name = %q, want yet", r.Application().Name)
}
if r.Platform() != "linux" {
t.Errorf("Platform() = %q, want linux", r.Platform())
}
r2, err := For(yetApp(), WithPlatform("linux"), WithEnvironment(map[string]string{"HOME": "/home/test"}))
if err != nil {
t.Fatalf("For: %v", err)
}
if r2.Platform() != "linux" {
t.Errorf("For(...).Platform() = %q, want linux", r2.Platform())
}
}
func TestOSEnvironmentLookup(t *testing.T) {
t.Setenv("YETANO_CORE_TEST_VAR", "hello")
if v, ok := OSEnvironment.Lookup("YETANO_CORE_TEST_VAR"); !ok || v != "hello" {
t.Errorf("OSEnvironment.Lookup() = (%q, %v), want (hello, true)", v, ok)
}
if _, ok := OSEnvironment.Lookup("YETANO_CORE_TEST_VAR_UNSET"); ok {
t.Errorf("OSEnvironment.Lookup() of an unset var returned ok=true")
}
}
func TestAllWithOverrides(t *testing.T) {
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env),
WithDataDir("/api/data"),
WithStateDir("/api/state"),
WithCacheDir("/api/cache"),
WithRuntimeDir("/api/runtime"),
WithLogDir("/api/log"),
WithBinDir("/api/bin"),
)
checks := map[string]func() (string, error){
"/api/data": r.DataDir,
"/api/state": r.StateDir,
"/api/cache": r.CacheDir,
"/api/runtime": r.RuntimeDir,
"/api/log": r.LogDir,
"/api/bin": r.BinDir,
}
for want, fn := range checks {
got, err := fn()
if err != nil {
t.Fatalf("%v", err)
}
if got != want {
t.Errorf("got %q, want %q", got, want)
}
}
}
func TestWithEnvPrefixAndEnvironmentProvider(t *testing.T) {
env := MapEnvironment{"HOME": "/home/test", "CUSTOM_CONFIG_HOME": "/custom-prefix/config"}
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironmentProvider(env), WithEnvPrefix("CUSTOM"))
got, err := r.ConfigDir()
if err != nil {
t.Fatalf("ConfigDir: %v", err)
}
if want := "/custom-prefix/config"; got != want {
t.Errorf("ConfigDir() = %q, want %q", got, want)
}
}
func TestLogAliasDirUnix(t *testing.T) {
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
got, err := r.LogAliasDir()
if err != nil {
t.Fatalf("LogAliasDir: %v", err)
}
if want := "/home/test/.local/log/yet"; got != want {
t.Errorf("LogAliasDir() = %q, want %q", got, want)
}
}
func TestLogAliasDirWindowsFallsBackToLogDir(t *testing.T) {
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(windowsEnv()))
alias, err := r.LogAliasDir()
if err != nil {
t.Fatalf("LogAliasDir: %v", err)
}
log, err := r.LogDir()
if err != nil {
t.Fatalf("LogDir: %v", err)
}
if alias != log {
t.Errorf("LogAliasDir() = %q, want equal to LogDir() = %q", alias, log)
}
}
func TestEnsureLogAliasCreatesSymlink(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithRoot(root))
alias, err := r.EnsureLogAlias()
if err != nil {
t.Fatalf("EnsureLogAlias: %v", err)
}
wantAlias, err := r.LogAliasDir()
if err != nil {
t.Fatalf("LogAliasDir: %v", err)
}
if alias != wantAlias {
t.Errorf("EnsureLogAlias() = %q, want %q", alias, wantAlias)
}
info, err := os.Lstat(alias)
if err != nil {
t.Fatalf("Lstat(%q): %v", alias, err)
}
if info.Mode()&os.ModeSymlink == 0 {
t.Fatalf("%q was not created as a symlink", alias)
}
target, err := os.Readlink(alias)
if err != nil {
t.Fatalf("Readlink(%q): %v", alias, err)
}
logDir, err := r.LogDir()
if err != nil {
t.Fatalf("LogDir: %v", err)
}
if target != logDir {
t.Errorf("symlink target = %q, want %q", target, logDir)
}
// Idempotent: a second call must not fail or touch the existing link.
if _, err := r.EnsureLogAlias(); err != nil {
t.Fatalf("EnsureLogAlias (second call): %v", err)
}
}
func TestEnsureLogAliasLeavesExistingEntryAlone(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithRoot(root))
alias, err := r.LogAliasDir()
if err != nil {
t.Fatalf("LogAliasDir: %v", err)
}
if err := os.MkdirAll(alias, 0o700); err != nil {
t.Fatalf("MkdirAll(%q): %v", alias, err)
}
got, err := r.EnsureLogAlias()
if err != nil {
t.Fatalf("EnsureLogAlias: %v", err)
}
if got != alias {
t.Errorf("EnsureLogAlias() = %q, want %q", got, alias)
}
info, err := os.Lstat(alias)
if err != nil || !info.IsDir() {
t.Errorf("pre-existing directory at %q was replaced (err=%v)", alias, err)
}
}
func TestEnsureLogAliasWindowsEqualsLogDir(t *testing.T) {
root := t.TempDir()
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(windowsEnv()), WithRoot(root))
alias, err := r.EnsureLogAlias()
if err != nil {
t.Fatalf("EnsureLogAlias: %v", err)
}
logDir, err := r.LogDir()
if err != nil {
t.Fatalf("LogDir: %v", err)
}
if alias != logDir {
t.Errorf("EnsureLogAlias() = %q, want %q (LogDir)", alias, logDir)
}
}
func TestDarwinDesktopRuntimeDirPrefersTMPDIR(t *testing.T) {
env := map[string]string{"HOME": "/Users/test", "TMPDIR": "/var/folders/zz/T/"}
app := core.Application{Name: "yapama", Kind: core.Desktop}
r := mustResolver(t, app, WithPlatform("darwin"), WithEnvironment(env))
if got, err := r.RuntimeDir(); err != nil || got != "/var/folders/zz/T/yapama" {
t.Errorf("RuntimeDir() = (%q, %v), want /var/folders/zz/T/yapama", got, err)
}
}
func TestEnsureAllCategories(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
// WithRuntimeDir takes a bare path in the simulated platform's
// family (here Linux/Unix); applyRoot nests it under root exactly
// once -- pre-pending root here too would double-root it (and, on a
// real Windows host where root is itself Windows-native, produce an
// invalid mid-path drive letter).
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithRoot(root),
WithRuntimeDir("/runtime-override"))
ensures := []func() (string, error){
r.EnsureConfigDir, r.EnsureDataDir, r.EnsureStateDir,
r.EnsureCacheDir, r.EnsureRuntimeDir, r.EnsureLogDir, r.EnsureBinDir,
}
for _, fn := range ensures {
dir, err := fn()
if err != nil {
t.Fatalf("Ensure: %v", err)
}
info, err := os.Stat(dir)
if err != nil || !info.IsDir() {
t.Errorf("%s was not created as a directory (err=%v)", dir, err)
}
}
}
func TestFindDataAndFindExecutable(t *testing.T) {
root := t.TempDir()
env := map[string]string{"HOME": "/home/test"}
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithRoot(root))
if _, found, err := r.FindData("plugin.so"); err != nil || found {
t.Fatalf("FindData() = found=%v err=%v, want false", found, err)
}
if _, found, err := r.FindExecutable("yet"); err != nil || found {
t.Fatalf("FindExecutable() = found=%v err=%v, want false", found, err)
}
if _, err := r.EnsureDataDir(); err != nil {
t.Fatal(err)
}
dataDir, _ := r.DataDir()
if err := os.WriteFile(dataDir+"/plugin.so", []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
if _, found, err := r.FindData("plugin.so"); err != nil || !found {
t.Fatalf("FindData() = found=%v err=%v, want true", found, err)
}
if _, err := r.EnsureBinDir(); err != nil {
t.Fatal(err)
}
binDir, _ := r.BinDir()
if err := os.WriteFile(binDir+"/yet", []byte("x"), 0o755); err != nil {
t.Fatal(err)
}
if _, found, err := r.FindExecutable("yet"); err != nil || !found {
t.Fatalf("FindExecutable() = found=%v err=%v, want true", found, err)
}
}
func TestDarwinDesktopRemainingCategories(t *testing.T) {
env := map[string]string{"HOME": "/Users/test"}
app := core.Application{Name: "yapama", Kind: core.Desktop}
r := mustResolver(t, app, WithPlatform("darwin"), WithEnvironment(env))
if got, err := r.RuntimeDir(); err != nil || got != "/Users/test/Library/Application Support/yapama/Runtime" {
t.Errorf("RuntimeDir() = (%q, %v)", got, err)
}
if got, err := r.BinDir(); err != nil || got != "/Users/test/.local/bin" {
t.Errorf("BinDir() = (%q, %v)", got, err)
}
dirs, err := r.DataSearchDirs()
if err != nil {
t.Fatalf("DataSearchDirs: %v", err)
}
want := []string{
"/Users/test/Library/Application Support/yapama",
"/Library/Application Support/yapama",
}
for i, w := range want {
if dirs[i].Path != w {
t.Errorf("DataSearchDirs()[%d] = %q, want %q", i, dirs[i].Path, w)
}
}
}
func TestWindowsExecutableSearchDirs(t *testing.T) {
r := mustResolver(t, yetApp(), WithPlatform("windows"), WithEnvironment(windowsEnv()))
dirs, err := r.ExecutableSearchDirs()
if err != nil {
t.Fatalf("ExecutableSearchDirs: %v", err)
}
want := []string{
`C:\Users\test\AppData\Local\Yetano\bin`,
`C:\ProgramData\Yetano\bin`,
}
for i, w := range want {
if dirs[i].Path != w {
t.Errorf("ExecutableSearchDirs()[%d] = %q, want %q", i, dirs[i].Path, w)
}
}
}
func TestJoinFamily(t *testing.T) {
if got, want := JoinFamily("windows", ".", "yet.yaml"), `.\yet.yaml`; got != want {
t.Errorf("JoinFamily(windows) = %q, want %q", got, want)
}
if got, want := JoinFamily("linux", ".", "yet.yaml"), "./yet.yaml"; got != want {
t.Errorf("JoinFamily(linux) = %q, want %q", got, want)
}
}