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,82 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"yetano.dev/yetano/core/paths"
|
||||
)
|
||||
|
||||
func TestConfigResolverAccessors(t *testing.T) {
|
||||
r := mustResolver(t, yetApp(), WithFilename("custom.yaml"))
|
||||
if r.Application().Name != "yet" {
|
||||
t.Errorf("Application().Name = %q, want yet", r.Application().Name)
|
||||
}
|
||||
if r.Filename() != "custom.yaml" {
|
||||
t.Errorf("Filename() = %q, want custom.yaml", r.Filename())
|
||||
}
|
||||
|
||||
r2, err := For(yetApp())
|
||||
if err != nil {
|
||||
t.Fatalf("For: %v", err)
|
||||
}
|
||||
if r2.Filename() != "yet.yaml" {
|
||||
t.Errorf("For(...).Filename() = %q, want yet.yaml", r2.Filename())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithEnvironmentProviderAndExpansionToggle(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeFile(t, filepath.Join(root, "etc", "yet", "yet.yaml"), "path: ${HOME}/data\n")
|
||||
|
||||
env := paths.MapEnvironment{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(),
|
||||
WithEnvironmentProvider(env),
|
||||
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
|
||||
WithWorkingDirectory(false),
|
||||
)
|
||||
var target struct {
|
||||
Path string `yaml:"path"`
|
||||
}
|
||||
if err := r.LoadMerged(&target); err != nil {
|
||||
t.Fatalf("LoadMerged: %v", err)
|
||||
}
|
||||
if want := "/home/test/data"; target.Path != want {
|
||||
t.Errorf("Path = %q, want %q", target.Path, want)
|
||||
}
|
||||
|
||||
r2 := mustResolver(t, yetApp(),
|
||||
WithEnvironmentProvider(env),
|
||||
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
|
||||
WithWorkingDirectory(false),
|
||||
WithEnvExpansion(false),
|
||||
)
|
||||
var target2 struct {
|
||||
Path string `yaml:"path"`
|
||||
}
|
||||
if err := r2.LoadMerged(&target2); err != nil {
|
||||
t.Fatalf("LoadMerged: %v", err)
|
||||
}
|
||||
if want := "${HOME}/data"; target2.Path != want {
|
||||
t.Errorf("Path = %q, want %q (expansion disabled)", target2.Path, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadLayerMissingFileIsNotAnError(t *testing.T) {
|
||||
m, err := readLayer("/does/not/exist/yet.yaml")
|
||||
if err != nil {
|
||||
t.Fatalf("readLayer() error = %v, want nil", err)
|
||||
}
|
||||
if len(m) != 0 {
|
||||
t.Errorf("readLayer() = %v, want empty map", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadLayerInvalidYAML(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, "bad.yaml")
|
||||
writeFile(t, path, "not: valid: yaml: [")
|
||||
if _, err := readLayer(path); err == nil {
|
||||
t.Fatal("readLayer() error = nil, want a parse error")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user