package cli import ( "bytes" "context" "os" "path/filepath" "runtime" "strconv" "strings" "testing" "gitea.maximumdirect.net/eric/notarius/internal/core/config" "go.maximumdirect.net/ckconfig" "go.maximumdirect.net/commandkit" "go.maximumdirect.net/configkit" ) func TestConfigLoadDiagnosticsIdentifyComposedSources(t *testing.T) { t.Parallel() for _, overlay := range []bool{false, true} { t.Run(strconv.FormatBool(overlay), func(t *testing.T) { root := t.TempDir() main := filepath.Join(root, "config.yml") fragment := filepath.Join(root, "pipeline\n\x1b.yml") args := []string{"config", "validate", "--config", main} if overlay { fragment = filepath.Join(root, "overlays", "development.yml") writeConfigKitFile(t, main, "version: 6\n") args = append(args, "--overlay", "development") } else { writeConfigKitFile(t, main, "version: 6\nconfig:\n imports:\n - path: "+strconv.Quote(filepath.Base(fragment))+"\n required: true\n") } writeConfigKitFile(t, fragment, "concurrency: {workers: invalid-private-value}\n") var stdout, stderr bytes.Buffer code := RunWithOptions(args, &stdout, &stderr, commandContractOptions(t)) got := stderr.String() if code == 0 || !strings.Contains(got, "decode.type") || !strings.Contains(got, strconv.Quote(fragment)) || strings.Contains(got, "invalid-private-value") || strings.ContainsRune(got, '\x1b') { t.Fatalf("status=%d stdout=%q stderr=%q", code, stdout.String(), got) } }) } } func TestApplicationLoadUsesOverlayAndOverrides(t *testing.T) { t.Parallel() root := t.TempDir() path := filepath.Join(root, "notarius.yml") writeConfigKitFile(t, path, "version: 6\npipelines: {base: {input: {module: input}}}\n") writeConfigKitFile(t, filepath.Join(root, "overlays", "development.yml"), "pipelines: {overlaid: {input: {module: input}}}\n") selection := productionSelection(path) selection.Overlay = commandkit.Supplied[string]{Set: true, Value: "development"} selectedDirectory := filepath.Join(root, "generated") selection.Overrides = []configkit.Override{{Path: configkit.Keys("output", "directory"), Value: selectedDirectory, Flag: "--set"}} loaders := &nativeLoaders{lookupEnv: emptyLookup} result, err := loaders.application(context.Background(), selection) if err != nil { t.Fatal(err) } loaded, err := config.FromResult(result) if err != nil { t.Fatal(err) } cfg := loaded.RuntimeConfig() if cfg.Pipelines["overlaid"].Input.Module != "input" || cfg.Output.Directory != selectedDirectory { t.Fatalf("loaded configuration=%#v", cfg) } } func TestNativeDiscoverySelectionAndTerminalFailures(t *testing.T) { root := t.TempDir() work := filepath.Join(root, "work") user := filepath.Join(root, "user") if err := os.MkdirAll(work, 0700); err != nil { t.Fatal(err) } switch runtime.GOOS { case "darwin": t.Setenv("HOME", user) user = filepath.Join(user, "Library", "Application Support") case "windows": t.Setenv("AppData", user) default: t.Setenv("XDG_CONFIG_HOME", user) } t.Chdir(work) userFile := filepath.Join(user, "notarius", "config.yml") writeConfigKitFile(t, userFile, "version: 6\npipelines: {user: {input: {module: input}}}\n") writeConfigKitFile(t, filepath.Join(work, "config.yml"), "version: 6\npipelines: {generic: {input: {module: input}}}\n") load := func(selection ckconfig.Selection, lookup func(string) (string, bool)) (*configkit.Result[config.Document], error) { loaders := &nativeLoaders{lookupEnv: lookup} return loaders.application(context.Background(), selection) } assertSelection := func(wantPath, wantID string) { t.Helper() result, err := load(ckconfig.Selection{}, emptyLookup) if err != nil { t.Fatal(err) } found := false for _, source := range result.Sources() { if source.Layer == configkit.LayerMain && source.Path == wantPath { found = true } } if !found || result.Config.Pipelines[wantID].Input.Module != "input" { t.Fatalf("discovery missed %s/%s: sources=%#v config=%#v", wantPath, wantID, result.Sources(), result.Config.Pipelines) } } assertSelection(userFile, "user") workingYAML, workingYML := filepath.Join(work, "notarius.yaml"), filepath.Join(work, "notarius.yml") writeConfigKitFile(t, workingYAML, "version: 6\npipelines: {yaml: {input: {module: input}}}\n") writeConfigKitFile(t, workingYML, "version: 6\npipelines: {yml: {input: {module: input}}}\n") assertSelection(workingYML, "yml") for _, args := range [][]string{{"pipeline", "list"}, {"config", "validate"}, {"config", "show"}, {"config", "explain", "--path", "/version"}} { var stdout, stderr bytes.Buffer if code := RunWithOptions(args, &stdout, &stderr, commandContractOptions(t)); code != 0 || stdout.Len() == 0 { t.Fatalf("%v status=%d stdout=%q stderr=%q", args, code, stdout.String(), stderr.String()) } } missingEnv := func(name string) (string, bool) { return filepath.Join(root, "missing.yml"), name == "NOTARIUS_CONFIG" } if _, err := load(ckconfig.Selection{}, missingEnv); err == nil { t.Fatal("missing environment selection fell back") } if err := os.WriteFile(workingYML, []byte("version: [\n"), 0600); err != nil { t.Fatal(err) } if _, err := load(ckconfig.Selection{}, emptyLookup); err == nil { t.Fatal("invalid selected file fell back") } if _, err := load(productionSelection(filepath.Join(root, "missing.yml")), emptyLookup); err == nil { t.Fatal("missing explicit selection fell back") } if err := os.Remove(workingYML); err != nil { t.Fatal(err) } assertSelection(workingYAML, "yaml") } func TestUnsupportedConfigurationUsesNativeDiagnostics(t *testing.T) { t.Parallel() path := filepath.Join(t.TempDir(), "invalid.yml") writeConfigKitFile(t, path, "version: 5\n") var stdout, stderr bytes.Buffer code := RunWithOptions([]string{"pipeline", "list", "--config", path}, &stdout, &stderr, Options{}) if code != 2 || !strings.Contains(stderr.String(), "version") || !strings.Contains(stderr.String(), path) { t.Fatalf("status=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String()) } } func writeConfigKitFile(t *testing.T, path, contents string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(contents), 0600); err != nil { t.Fatal(err) } }