2026-09-13 16:50:40 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"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-17 20:42:25 +00:00
|
|
|
configkit "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-09-14 00:41:41 +00:00
|
|
|
writeConfigKitFile(t, main, "version: 5\n")
|
2026-09-25 21:46:35 +00:00
|
|
|
args = append(args, "--config-overlay", "development")
|
2026-09-14 00:41:41 +00:00
|
|
|
} else {
|
2026-11-17 20:50:07 +00:00
|
|
|
writeConfigKitFile(t, main, "version: 5\nconfig:\n imports:\n - path: "+strconv.Quote(filepath.Base(fragment))+"\n required: true\n")
|
2026-09-14 00:41:41 +00:00
|
|
|
}
|
|
|
|
|
writeConfigKitFile(t, fragment, "concurrency: {total_llm: invalid-private-value}\n")
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
if code := RunWithOptions(args, &stdout, &stderr, commandContractOptions(t)); code != 1 {
|
|
|
|
|
t.Fatalf("status = %d; %s", code, stderr.String())
|
|
|
|
|
}
|
|
|
|
|
got := stderr.String()
|
|
|
|
|
if stdout.Len() != 0 || !strings.Contains(got, "decode.type") ||
|
|
|
|
|
!strings.Contains(got, "source-2:1:") ||
|
|
|
|
|
!strings.Contains(got, strconv.Quote(fragment)) ||
|
|
|
|
|
strings.Contains(got, "invalid-private-value") ||
|
|
|
|
|
strings.ContainsRune(got, '\x1b') {
|
|
|
|
|
t.Fatalf("stdout = %q; stderr = %q", stdout.String(), got)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 21:46:35 +00:00
|
|
|
func TestLoadConfigKitUsesDiscoveryOverlayAndOverrides(t *testing.T) {
|
2026-09-13 16:50:40 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
path := filepath.Join(root, "notarius.yml")
|
|
|
|
|
writeConfigKitFile(t, path, "version: 5\npipelines:\n base:\n input: {module: input}\n")
|
2026-09-25 21:46:35 +00:00
|
|
|
overlays := filepath.Join(root, "overlays")
|
|
|
|
|
writeConfigKitFile(t, filepath.Join(overlays, "development.yml"), "pipelines:\n overlaid:\n input: {module: input}\n")
|
2026-09-13 16:50:40 +00:00
|
|
|
loaded, gotPath, err := loadConfigKit(path, "development", []configkit.Override{{Path: configkit.Keys("output", "directory"), Value: "generated", Flag: "--output"}}, commandContractOptions(t))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("loadConfigKit() error = %v", err)
|
|
|
|
|
}
|
2026-09-25 21:46:35 +00:00
|
|
|
if gotPath != path || loaded.Config.Pipelines["overlaid"].Input.Module != "input" || loaded.Config.Output.Directory == "generated" {
|
2026-09-13 16:50:40 +00:00
|
|
|
t.Fatalf("loaded = %#v, path=%q", loaded.Config, gotPath)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
previous, err := os.Getwd()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := os.Chdir(work); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
if err := os.Chdir(previous); err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
userFile := filepath.Join(user, "notarius", "config.yml")
|
|
|
|
|
writeConfigKitFile(t, userFile, "version: 5\npipelines: {user: {input: {module: input}}}\n")
|
|
|
|
|
writeConfigKitFile(t, filepath.Join(work, "config.yml"), "version: 5\npipelines: {generic: {input: {module: input}}}\n")
|
|
|
|
|
opts := commandContractOptions(t)
|
|
|
|
|
loaded, path, err := loadConfigKit("", "", nil, opts)
|
|
|
|
|
if err != nil || path != userFile || loaded.Config.Pipelines["user"].Input.Module != "input" {
|
|
|
|
|
t.Fatalf("user discovery = %q, %v", path, err)
|
|
|
|
|
}
|
|
|
|
|
workingYAML := filepath.Join(work, "notarius.yaml")
|
|
|
|
|
workingYML := filepath.Join(work, "notarius.yml")
|
|
|
|
|
writeConfigKitFile(t, workingYAML, "version: 5\npipelines: {yaml: {input: {module: input}}}\n")
|
|
|
|
|
writeConfigKitFile(t, workingYML, "version: 5\npipelines: {yml: {input: {module: input}}}\n")
|
|
|
|
|
loaded, path, err = loadConfigKit("", "", nil, opts)
|
|
|
|
|
if err != nil || path != workingYML || loaded.Config.Pipelines["yml"].Input.Module != "input" || loaded.Config.Output.Directory != filepath.Join(work, "notarius-output") {
|
|
|
|
|
t.Fatalf("working discovery = %q, %v", path, err)
|
|
|
|
|
}
|
|
|
|
|
for _, args := range [][]string{{"pipelines", "list"}, {"config", "validate"}, {"config", "effective"}, {"config", "explain", "/version"}} {
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
|
if code := RunWithOptions(args, &stdout, &stderr, opts); code != 0 || stderr.Len() != 0 || stdout.Len() == 0 {
|
|
|
|
|
t.Fatalf("%v: code=%d stdout=%q stderr=%q", args, code, stdout.String(), stderr.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
missingEnv := opts
|
|
|
|
|
missingEnv.LookupEnv = func(name string) (string, bool) { return filepath.Join(root, "missing.yml"), name == "NOTARIUS_CONFIG" }
|
|
|
|
|
if _, _, err := loadConfigKit("", "", nil, 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 := loadConfigKit("", "", nil, opts); err == nil {
|
|
|
|
|
t.Fatal("invalid selected file fell back")
|
|
|
|
|
}
|
|
|
|
|
if _, _, err := loadConfigKit(filepath.Join(root, "missing.yml"), "", nil, opts); err == nil {
|
|
|
|
|
t.Fatal("missing explicit file fell back")
|
|
|
|
|
}
|
|
|
|
|
if err := os.Remove(workingYML); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
loaded, path, err = loadConfigKit("", "", nil, opts)
|
|
|
|
|
if err != nil || path != workingYAML || loaded.Config.Pipelines["yaml"].Input.Module != "input" {
|
|
|
|
|
t.Fatalf("alternate named file = %q, %v", path, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-13 16:50:40 +00:00
|
|
|
func TestWriteConfigLoadErrorUsesSafeDiagnostics(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
path := filepath.Join(t.TempDir(), "invalid.yml")
|
|
|
|
|
writeConfigKitFile(t, path, "version: 4\n")
|
|
|
|
|
_, _, err := loadConfigKit(path, "", nil, commandContractOptions(t))
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("loadConfigKit() error = nil")
|
|
|
|
|
}
|
|
|
|
|
var output bytes.Buffer
|
|
|
|
|
writeConfigLoadError(&output, err)
|
|
|
|
|
if !strings.Contains(output.String(), "version") || strings.Contains(output.String(), "notarius: configkit") {
|
|
|
|
|
t.Fatalf("diagnostics = %q", output.String())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeConfigKitFile(t *testing.T, path, contents string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|