config: add WithExplicitPath for a CLI's own --config flag
A highest-priority, must-exist override layer above the working directory -- the common "flag always wins, but a typo'd path should fail loudly" pattern most Yetano CLIs need, generalized here instead of reimplementing it in each application (first consumer: yet's own --config flag, see the yet submodule). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
13801bec11
commit
689469eb32
3 files changed
+110
-4
No files matched your search
@@ -0,0 +1,75 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"yetano.dev/yetano/core/paths"
|
||||
)
|
||||
|
||||
func TestWithExplicitPathWinsOverEverything(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeFile(t, filepath.Join(root, "etc", "yet", "yet.yaml"), "layer: system\n")
|
||||
writeFile(t, filepath.Join(root, "home", "test", ".config", "yet", "yet.yaml"), "layer: user\n")
|
||||
explicit := filepath.Join(root, "explicit.yaml")
|
||||
writeFile(t, explicit, "layer: explicit\n")
|
||||
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(),
|
||||
WithEnvironment(env),
|
||||
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
|
||||
WithWorkdirPath(filepath.Join(root, "workdir")),
|
||||
WithExplicitPath(explicit),
|
||||
)
|
||||
|
||||
layer, found, err := r.Resolve()
|
||||
if err != nil {
|
||||
t.Fatalf("Resolve: %v", err)
|
||||
}
|
||||
if !found || layer.Path != explicit {
|
||||
t.Fatalf("Resolve() = %+v, found=%v, want the explicit path to win", layer, found)
|
||||
}
|
||||
if layer.Priority != PriorityExplicit {
|
||||
t.Errorf("layer.Priority = %d, want %d", layer.Priority, PriorityExplicit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithExplicitPathMissingIsAnError(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(),
|
||||
WithEnvironment(env),
|
||||
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
|
||||
WithExplicitPath(filepath.Join(root, "does-not-exist.yaml")),
|
||||
)
|
||||
if _, _, err := r.Resolve(); err == nil {
|
||||
t.Fatal("Resolve() error = nil, want an error for a missing explicit path")
|
||||
}
|
||||
if _, err := r.ResolveAll(); err == nil {
|
||||
t.Fatal("ResolveAll() error = nil, want an error for a missing explicit path")
|
||||
}
|
||||
var target struct{}
|
||||
if err := r.LoadMerged(&target); err == nil {
|
||||
t.Fatal("LoadMerged() error = nil, want an error for a missing explicit path")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithExplicitPathEmptyDisablesLayer(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
env := map[string]string{"HOME": "/home/test"}
|
||||
r := mustResolver(t, yetApp(),
|
||||
WithEnvironment(env),
|
||||
WithPathsOptions(paths.WithPlatform("linux"), paths.WithRoot(root)),
|
||||
WithWorkdirPath(filepath.Join(root, "workdir")),
|
||||
WithExplicitPath(""),
|
||||
)
|
||||
layers, err := r.ResolveAll()
|
||||
if err != nil {
|
||||
t.Fatalf("ResolveAll: %v", err)
|
||||
}
|
||||
for _, l := range layers {
|
||||
if l.Priority == PriorityExplicit {
|
||||
t.Fatalf("ResolveAll() included an explicit layer despite WithExplicitPath(\"\"): %v", layers)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user