85 lines
2.7 KiB
Go
85 lines
2.7 KiB
Go
package paths
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestFindConfigFirstMatch(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
env := map[string]string{"HOME": "/home/test"}
|
||
|
|
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithRoot(root))
|
||
|
|
|
||
|
|
// Nothing exists yet.
|
||
|
|
if _, found, err := r.FindConfig("yet.yaml"); err != nil || found {
|
||
|
|
t.Fatalf("FindConfig() = found=%v err=%v, want found=false", found, err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := os.MkdirAll(filepath.Join(root, "etc", "yet"), 0o755); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(filepath.Join(root, "etc", "yet", "yet.yaml"), []byte("system: true\n"), 0o644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
|
||
|
|
result, found, err := r.FindConfig("yet.yaml")
|
||
|
|
if err != nil || !found {
|
||
|
|
t.Fatalf("FindConfig() = found=%v err=%v, want found=true", found, err)
|
||
|
|
}
|
||
|
|
if result.Source != SystemDirectory {
|
||
|
|
t.Errorf("FindConfig() source = %v, want SystemDirectory", result.Source)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Now also create the higher-priority user config; it should win.
|
||
|
|
if err := os.MkdirAll(filepath.Join(root, "home", "test", ".config", "yet"), 0o755); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := os.WriteFile(filepath.Join(root, "home", "test", ".config", "yet", "yet.yaml"), []byte("user: true\n"), 0o644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
result, found, err = r.FindConfig("yet.yaml")
|
||
|
|
if err != nil || !found {
|
||
|
|
t.Fatalf("FindConfig() = found=%v err=%v, want found=true", found, err)
|
||
|
|
}
|
||
|
|
if result.Source == SystemDirectory {
|
||
|
|
t.Errorf("FindConfig() should prefer the user layer once it exists, got source=%v path=%v", result.Source, result.Path)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestEnsureConfigDirCreatesDirectory(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
env := map[string]string{"HOME": "/home/test"}
|
||
|
|
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env), WithRoot(root))
|
||
|
|
|
||
|
|
dir, err := r.EnsureConfigDir()
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("EnsureConfigDir: %v", err)
|
||
|
|
}
|
||
|
|
info, err := os.Stat(dir)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("stat %s: %v", dir, err)
|
||
|
|
}
|
||
|
|
if !info.IsDir() {
|
||
|
|
t.Errorf("%s is not a directory", dir)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestEnsureRuntimeDirPropagatesUnavailable(t *testing.T) {
|
||
|
|
env := map[string]string{"HOME": "/home/test"}
|
||
|
|
r := mustResolver(t, yetApp(), WithPlatform("linux"), WithEnvironment(env))
|
||
|
|
if _, err := r.EnsureRuntimeDir(); err != ErrRuntimeDirUnavailable {
|
||
|
|
t.Fatalf("EnsureRuntimeDir() error = %v, want ErrRuntimeDirUnavailable", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestFindFileNotFoundIsNotAnError(t *testing.T) {
|
||
|
|
dirs := []SearchResult{{Path: "/does/not/exist", Source: SystemDirectory}}
|
||
|
|
_, found, err := FindFile(dirs, "missing.yaml")
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("FindFile() error = %v, want nil", err)
|
||
|
|
}
|
||
|
|
if found {
|
||
|
|
t.Fatalf("FindFile() found = true, want false")
|
||
|
|
}
|
||
|
|
}
|