2026-09-13 16:50:40 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2026-11-22 05:16:24 +00:00
|
|
|
"context"
|
2026-09-13 16:50:40 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2026-11-17 20:53:07 +00:00
|
|
|
"runtime"
|
2026-09-14 00:41:41 +00:00
|
|
|
"strconv"
|
2026-09-13 16:50:40 +00:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-11-22 05:16:24 +00:00
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/config"
|
|
|
|
|
"go.maximumdirect.net/ckconfig"
|
|
|
|
|
"go.maximumdirect.net/commandkit"
|
|
|
|
|
"go.maximumdirect.net/configkit"
|
2026-09-13 16:50:40 +00:00
|
|
|
)
|
|
|
|
|
|
2026-09-14 00:41:41 +00:00
|
|
|
func TestConfigLoadDiagnosticsIdentifyComposedSources(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
2026-09-25 21:46:35 +00:00
|
|
|
for _, overlay := range []bool{false, true} {
|
|
|
|
|
t.Run(strconv.FormatBool(overlay), func(t *testing.T) {
|
2026-09-14 00:41:41 +00:00
|
|
|
root := t.TempDir()
|
|
|
|
|
main := filepath.Join(root, "config.yml")
|
|
|
|
|
fragment := filepath.Join(root, "pipeline\n\x1b.yml")
|
|
|
|
|
args := []string{"config", "validate", "--config", main}
|
2026-09-25 21:46:35 +00:00
|
|
|
if overlay {
|
|
|
|
|
fragment = filepath.Join(root, "overlays", "development.yml")
|
2026-11-22 05:16:24 +00:00
|
|
|
writeConfigKitFile(t, main, "version: 6\n")
|
|
|
|
|
args = append(args, "--overlay", "development")
|
2026-09-14 00:41:41 +00:00
|
|
|
} else {
|
2026-11-22 05:16:24 +00:00
|
|
|
writeConfigKitFile(t, main, "version: 6\nconfig:\n imports:\n - path: "+strconv.Quote(filepath.Base(fragment))+"\n required: true\n")
|
2026-09-14 00:41:41 +00:00
|
|
|
}
|
2026-11-22 05:16:24 +00:00
|
|
|
writeConfigKitFile(t, fragment, "concurrency: {workers: invalid-private-value}\n")
|
2026-09-14 00:41:41 +00:00
|
|
|
var stdout, stderr bytes.Buffer
|
2026-11-22 05:16:24 +00:00
|
|
|
code := RunWithOptions(args, &stdout, &stderr, commandContractOptions(t))
|
2026-09-14 00:41:41 +00:00
|
|
|
got := stderr.String()
|
2026-11-22 05:16:24 +00:00
|
|
|
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)
|
2026-09-14 00:41:41 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 05:16:24 +00:00
|
|
|
func TestApplicationLoadUsesOverlayAndOverrides(t *testing.T) {
|
2026-09-13 16:50:40 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
path := filepath.Join(root, "notarius.yml")
|
2026-11-22 05:16:24 +00:00
|
|
|
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)
|
2026-09-13 16:50:40 +00:00
|
|
|
if err != nil {
|
2026-11-22 05:16:24 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
loaded, err := config.FromResult(result)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
2026-09-13 16:50:40 +00:00
|
|
|
}
|
2026-11-22 05:16:24 +00:00
|
|
|
cfg := loaded.RuntimeConfig()
|
|
|
|
|
if cfg.Pipelines["overlaid"].Input.Module != "input" || cfg.Output.Directory != selectedDirectory {
|
|
|
|
|
t.Fatalf("loaded configuration=%#v", cfg)
|
2026-09-13 16:50:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 20:53:07 +00:00
|
|
|
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)
|
|
|
|
|
}
|
2026-11-22 05:16:24 +00:00
|
|
|
t.Chdir(work)
|
2026-11-17 20:53:07 +00:00
|
|
|
userFile := filepath.Join(user, "notarius", "config.yml")
|
2026-11-22 05:16:24 +00:00
|
|
|
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"}} {
|
2026-11-17 20:53:07 +00:00
|
|
|
var stdout, stderr bytes.Buffer
|
2026-11-22 05:16:24 +00:00
|
|
|
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())
|
2026-11-17 20:53:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-11-22 05:16:24 +00:00
|
|
|
missingEnv := func(name string) (string, bool) { return filepath.Join(root, "missing.yml"), name == "NOTARIUS_CONFIG" }
|
|
|
|
|
if _, err := load(ckconfig.Selection{}, missingEnv); err == nil {
|
2026-11-17 20:53:07 +00:00
|
|
|
t.Fatal("missing environment selection fell back")
|
|
|
|
|
}
|
|
|
|
|
if err := os.WriteFile(workingYML, []byte("version: [\n"), 0600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-22 05:16:24 +00:00
|
|
|
if _, err := load(ckconfig.Selection{}, emptyLookup); err == nil {
|
2026-11-17 20:53:07 +00:00
|
|
|
t.Fatal("invalid selected file fell back")
|
|
|
|
|
}
|
2026-11-22 05:16:24 +00:00
|
|
|
if _, err := load(productionSelection(filepath.Join(root, "missing.yml")), emptyLookup); err == nil {
|
|
|
|
|
t.Fatal("missing explicit selection fell back")
|
2026-11-17 20:53:07 +00:00
|
|
|
}
|
|
|
|
|
if err := os.Remove(workingYML); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-22 05:16:24 +00:00
|
|
|
assertSelection(workingYAML, "yaml")
|
2026-11-17 20:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-22 05:37:15 +00:00
|
|
|
func TestUnsupportedConfigurationUsesNativeDiagnostics(t *testing.T) {
|
2026-09-13 16:50:40 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
path := filepath.Join(t.TempDir(), "invalid.yml")
|
2026-11-22 05:16:24 +00:00
|
|
|
writeConfigKitFile(t, path, "version: 5\n")
|
2026-11-22 05:37:15 +00:00
|
|
|
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())
|
2026-09-13 16:50:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeConfigKitFile(t *testing.T, path, contents string) {
|
|
|
|
|
t.Helper()
|
2026-11-22 05:16:24 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
2026-09-13 16:50:40 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-22 05:16:24 +00:00
|
|
|
if err := os.WriteFile(path, []byte(contents), 0600); err != nil {
|
2026-09-13 16:50:40 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|