Use native pipeline discovery and independent secrets bootstrap
This commit is contained in:
87
internal/config/pipeline_discovery_test.go
Normal file
87
internal/config/pipeline_discovery_test.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestPipelineDiscoverySelectsFirstSourceAndExplicitFileWins(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first := filepath.Join(dir, "first.yml")
|
||||
second := filepath.Join(dir, "second.yml")
|
||||
for path, root := range map[string]string{first: "/first", second: "/second"} {
|
||||
if err := os.WriteFile(path, []byte("workspace:\n root: "+root+"\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
search := configkit.SearchPaths(first, second)
|
||||
for _, test := range []struct{ explicit, root, path string }{
|
||||
{"", "/first", first},
|
||||
{second, "/second", second},
|
||||
} {
|
||||
cfg, err := LoadPipelineWithOptions(test.explicit, PipelineLoadOptions{Search: &search})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Workspace.Root != test.root || EffectivePipelineRootPath(cfg) != test.path {
|
||||
t.Fatalf("selected root = %q, source = %q", cfg.Workspace.Root, EffectivePipelineRootPath(cfg))
|
||||
}
|
||||
}
|
||||
if _, err := LoadPipelineWithOptions(filepath.Join(dir, "missing.yml"), PipelineLoadOptions{Search: &search}); err == nil {
|
||||
t.Fatal("missing explicit file fell back to search")
|
||||
}
|
||||
if err := os.WriteFile(first, []byte("workspace: [invalid\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := LoadPipelineWithOptions("", PipelineLoadOptions{Search: &search}); err == nil || !strings.Contains(err.Error(), first) {
|
||||
t.Fatalf("invalid selected source should fail: %v", err)
|
||||
}
|
||||
missing := configkit.SearchPaths(filepath.Join(dir, "missing.yml"))
|
||||
if _, err := LoadPipelineWithOptions("", PipelineLoadOptions{Search: &missing}); err == nil {
|
||||
t.Fatal("empty required search succeeded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPipelineDiscoveryUsesWorkingDirectoryBeforeUserConfig(t *testing.T) {
|
||||
working := t.TempDir()
|
||||
user := t.TempDir()
|
||||
t.Setenv("XDG_CONFIG_HOME", user)
|
||||
original, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chdir(working); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Chdir(original) })
|
||||
userDir := filepath.Join(user, "narratio")
|
||||
if err := os.MkdirAll(userDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
userPath := filepath.Join(userDir, "config.yml")
|
||||
localPath := filepath.Join(working, "narratio.yml")
|
||||
for path, root := range map[string]string{userPath: "/user", localPath: "/working"} {
|
||||
if err := os.WriteFile(path, []byte("workspace:\n root: "+root+"\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
search := configkit.Searches(configkit.WorkingDirectory(), configkit.UserConfigDirectory())
|
||||
for _, want := range []string{localPath, userPath} {
|
||||
cfg, err := LoadPipelineWithOptions("", PipelineLoadOptions{Search: &search})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := EffectivePipelineRootPath(cfg); got != want {
|
||||
t.Fatalf("selected %q, want %q", got, want)
|
||||
}
|
||||
if want == localPath {
|
||||
if err := os.Remove(localPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user