All checks were successful
ci/woodpecker/push/verify Pipeline was successful
135 lines
4.6 KiB
Go
135 lines
4.6 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"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(testTranscriptionConfig+"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)
|
|
}
|
|
_, err := LoadPipelineWithOptions("", PipelineLoadOptions{Search: &search})
|
|
var native *configkit.LoadError
|
|
if !errors.As(err, &native) || len(native.Sources()) == 0 || native.Sources()[0].Path != 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(testTranscriptionConfig+"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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPipelineSourceRetainsPreparedMainAndPureLoading(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "pipeline.yml")
|
|
secretDir := filepath.Join(dir, "secrets")
|
|
if err := os.Mkdir(secretDir, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const name = "NARRATIO_SOURCE_HANDOFF_TEST"
|
|
// Register cleanup for the original value and presence before unsetting it.
|
|
t.Setenv(name, "")
|
|
if err := os.Unsetenv(name); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(secretDir, name), []byte("synthetic-secret"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := "config: {secrets_dir: secrets}\n" + testTranscriptionConfig + "workspace: {root: captured}\n"
|
|
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := LoadPipelineWithOptions(path, PipelineLoadOptions{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, present := os.LookupEnv(name); present {
|
|
t.Fatal("pure loading changed environment")
|
|
}
|
|
_, prepared, err := configkit.LoadSecretsIntoEnvironmentFromConfig(PipelineSource(path, PipelineLoadOptions{}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := os.Getenv(name); got != "synthetic-secret" {
|
|
t.Fatal("explicit bootstrap did not populate the test credential")
|
|
}
|
|
if err := os.WriteFile(path, []byte("invalid: ["), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := LoadPipelineSourceWithOptions(prepared, PipelineLoadOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Workspace.Root != filepath.Join(dir, "captured") || EffectivePipelineRootPath(cfg) != path {
|
|
t.Fatalf("prepared context lost: root=%q source=%q", cfg.Workspace.Root, EffectivePipelineRootPath(cfg))
|
|
}
|
|
}
|