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,323 @@
|
||||
package paths
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"yetano.dev/yetano/core"
|
||||
)
|
||||
|
||||
func yetApp() core.Application {
|
||||
return core.Application{Name: "yet", Kind: core.CLI}
|
||||
}
|
||||
|
||||
func mustResolver(t *testing.T, app core.Application, opts ...Option) *Resolver {
|
||||
t.Helper()
|
||||
r, err := New(app, opts...)
|
||||
if err != nil {
|
||||
t.Fatalf("New: %v", err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func TestLinuxDefaults(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||||
|
||||
checks := []struct {
|
||||
name string
|
||||
fn func() (string, error)
|
||||
want string
|
||||
}{
|
||||
{"ConfigDir", r.ConfigDir, "/home/test/.config/yet"},
|
||||
{"DataDir", r.DataDir, "/home/test/.local/share/yet"},
|
||||
{"StateDir", r.StateDir, "/home/test/.local/state/yet"},
|
||||
{"CacheDir", r.CacheDir, "/home/test/.cache/yet"},
|
||||
{"LogDir", r.LogDir, "/home/test/.local/state/yet/log"},
|
||||
{"BinDir", r.BinDir, "/home/test/.local/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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinuxRuntimeDirUnavailableWithoutXDG(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||||
if _, err := r.RuntimeDir(); err != ErrRuntimeDirUnavailable {
|
||||
t.Fatalf("RuntimeDir() error = %v, want ErrRuntimeDirUnavailable", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinuxRuntimeDirFromXDG(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test", "XDG_RUNTIME_DIR": "/run/user/1000"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||||
got, err := r.RuntimeDir()
|
||||
if err != nil {
|
||||
t.Fatalf("RuntimeDir: %v", err)
|
||||
}
|
||||
if want := "/run/user/1000/yet"; got != want {
|
||||
t.Errorf("RuntimeDir() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestXDGOverride(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test", "XDG_CONFIG_HOME": "/custom/config"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||||
got, err := r.ConfigDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigDir: %v", err)
|
||||
}
|
||||
if want := "/custom/config/yet"; got != want {
|
||||
t.Errorf("ConfigDir() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestYetanoOverrideWinsOverXDG(t *testing.T) {
|
||||
env := map[string]string{
|
||||
"HOME": "/home/test",
|
||||
"XDG_CONFIG_HOME": "/custom/config",
|
||||
"YETANO_CONFIG_HOME": "/yetano/config",
|
||||
}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||||
got, err := r.ConfigDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigDir: %v", err)
|
||||
}
|
||||
if want := "/yetano/config/yet"; got != want {
|
||||
t.Errorf("ConfigDir() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppOverrideWinsOverYetano(t *testing.T) {
|
||||
env := map[string]string{
|
||||
"HOME": "/home/test",
|
||||
"XDG_CONFIG_HOME": "/custom/config",
|
||||
"YETANO_CONFIG_HOME": "/yetano/config",
|
||||
"YET_CONFIG_HOME": "/yet/config",
|
||||
}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||||
got, err := r.ConfigDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigDir: %v", err)
|
||||
}
|
||||
// App-specific overrides are used as-is (no "/yet" suffix appended):
|
||||
// the variable's own name already names the application.
|
||||
if want := "/yet/config"; got != want {
|
||||
t.Errorf("ConfigDir() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIOverrideWinsOverEverything(t *testing.T) {
|
||||
env := map[string]string{
|
||||
"HOME": "/home/test",
|
||||
"XDG_CONFIG_HOME": "/custom/config",
|
||||
"YETANO_CONFIG_HOME": "/yetano/config",
|
||||
"YET_CONFIG_HOME": "/yet/config",
|
||||
}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithConfigDir("/api/config"))
|
||||
got, err := r.ConfigDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigDir: %v", err)
|
||||
}
|
||||
if want := "/api/config"; got != want {
|
||||
t.Errorf("ConfigDir() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithoutAppEnv(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test", "YET_CONFIG_HOME": "/yet/config"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithoutAppEnv())
|
||||
got, err := r.ConfigDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigDir: %v", err)
|
||||
}
|
||||
if want := "/home/test/.config/yet"; got != want {
|
||||
t.Errorf("ConfigDir() = %q, want %q (YET_CONFIG_HOME should be ignored)", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinuxConfigSearchDirs(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||||
dirs, err := r.ConfigSearchDirs()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigSearchDirs: %v", err)
|
||||
}
|
||||
want := []string{"/home/test/.config/yet", "/etc/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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSearchResultSelected uses WithRoot over a real temp directory so
|
||||
// Exists/Selected (which stat the real filesystem) are meaningful.
|
||||
func TestSearchResultSelected(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithRoot(root))
|
||||
|
||||
if _, err := r.EnsureConfigDir(); err != nil {
|
||||
t.Fatalf("EnsureConfigDir: %v", err)
|
||||
}
|
||||
dirs, err := r.ConfigSearchDirs()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigSearchDirs: %v", err)
|
||||
}
|
||||
if !dirs[0].Exists || !dirs[0].Selected {
|
||||
t.Errorf("ConfigSearchDirs()[0] = %+v, want Exists=true Selected=true", dirs[0])
|
||||
}
|
||||
if dirs[1].Exists || dirs[1].Selected {
|
||||
t.Errorf("ConfigSearchDirs()[1] = %+v, want Exists=false Selected=false (nothing created there)", dirs[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinuxDataSearchDirs(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||||
dirs, err := r.DataSearchDirs()
|
||||
if err != nil {
|
||||
t.Fatalf("DataSearchDirs: %v", err)
|
||||
}
|
||||
want := []string{
|
||||
"/home/test/.local/share/yet",
|
||||
"/usr/local/share/yet",
|
||||
"/usr/share/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 TestLinuxDataSearchDirsXDGDataDirs(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test", "XDG_DATA_DIRS": "/opt/a/share:/opt/b/share"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||||
dirs, err := r.DataSearchDirs()
|
||||
if err != nil {
|
||||
t.Fatalf("DataSearchDirs: %v", err)
|
||||
}
|
||||
want := []string{
|
||||
"/home/test/.local/share/yet",
|
||||
"/opt/a/share/yet",
|
||||
"/opt/b/share/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 TestLinuxSystemScope(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
app := core.Application{Name: "yasta", Kind: core.Service, Scope: core.SystemScope}
|
||||
r := mustResolver(t, app, WithPlatform("linux"), WithEnvironment(env))
|
||||
|
||||
checks := []struct {
|
||||
name string
|
||||
fn func() (string, error)
|
||||
want string
|
||||
}{
|
||||
{"ConfigDir", r.ConfigDir, "/etc/yasta"},
|
||||
{"DataDir", r.DataDir, "/var/lib/yasta"},
|
||||
{"StateDir", r.StateDir, "/var/lib/yasta"},
|
||||
{"CacheDir", r.CacheDir, "/var/cache/yasta"},
|
||||
{"RuntimeDir", r.RuntimeDir, "/run/yasta"},
|
||||
{"LogDir", r.LogDir, "/var/log/yasta"},
|
||||
{"BinDir", r.BinDir, "/usr/local/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)
|
||||
}
|
||||
}
|
||||
|
||||
dirs, err := r.ConfigSearchDirs()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigSearchDirs: %v", err)
|
||||
}
|
||||
if len(dirs) != 1 || dirs[0].Path != "/etc/yasta" {
|
||||
t.Errorf("SystemScope ConfigSearchDirs() = %v, want exactly [/etc/yasta] (no user layer)", dirs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecutableSearchDirsUserOverridesSystem(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||||
dirs, err := r.ExecutableSearchDirs()
|
||||
if err != nil {
|
||||
t.Fatalf("ExecutableSearchDirs: %v", err)
|
||||
}
|
||||
want := []string{"/home/test/.local/bin", "/usr/local/bin"}
|
||||
for i, w := range want {
|
||||
if dirs[i].Path != w {
|
||||
t.Errorf("ExecutableSearchDirs()[%d] = %q, want %q", i, dirs[i].Path, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreeBSDFlavor(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
app := core.Application{Name: "yasta", Kind: core.Service, Scope: core.SystemScope}
|
||||
r := mustResolver(t, app, WithPlatform("freebsd"), WithEnvironment(env))
|
||||
|
||||
checks := []struct {
|
||||
name string
|
||||
fn func() (string, error)
|
||||
want string
|
||||
}{
|
||||
{"ConfigDir", r.ConfigDir, "/usr/local/etc/yasta"},
|
||||
{"DataDir", r.DataDir, "/var/db/yasta"},
|
||||
{"RuntimeDir", r.RuntimeDir, "/var/run/yasta"},
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMissingHomeDirectoryIsCleanError(t *testing.T) {
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(map[string]string{}))
|
||||
if _, err := r.ConfigDir(); err != ErrNoHomeDirectory {
|
||||
t.Fatalf("ConfigDir() error = %v, want ErrNoHomeDirectory", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithRootRebasesUnixPath(t *testing.T) {
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithRoot("/tmp/sandbox"))
|
||||
got, err := r.ConfigDir()
|
||||
if err != nil {
|
||||
t.Fatalf("ConfigDir: %v", err)
|
||||
}
|
||||
if want := "/tmp/sandbox/home/test/.config/yet"; got != want {
|
||||
t.Errorf("ConfigDir() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user