Files
core/config/explicit_test.go
terminarandClaude Sonnet 5 689469eb32 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>
2026-09-19 16:50:22 +02:00

76 lines
2.3 KiB
Go

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)
}
}
}