Verify configuration discovery and provenance
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -59,6 +60,79 @@ func TestLoadConfigKitUsesDiscoveryOverlayAndOverrides(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteConfigLoadErrorUsesSafeDiagnostics(t *testing.T) {
|
||||
t.Parallel()
|
||||
path := filepath.Join(t.TempDir(), "invalid.yml")
|
||||
|
||||
@@ -271,6 +271,41 @@ func TestRunAppliesConfigurationOverlayAndRecordsSelection(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunRecordsDiscoveredConfigurationPath(t *testing.T) {
|
||||
roots := newStateTestRoots(t)
|
||||
work := t.TempDir()
|
||||
selected := filepath.Join(work, "notarius.yml")
|
||||
data, err := os.ReadFile(roots.config)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(selected, data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
})
|
||||
var stdout, stderr bytes.Buffer
|
||||
code := RunWithOptions([]string{"run", "sample", "--input", roots.input, "--chunk_cache", "bypass", "--debug"}, &stdout, &stderr, newStateTestHarness().options())
|
||||
if code != 0 {
|
||||
t.Fatalf("code=%d stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
||||
}
|
||||
var invocation debugbundle.Invocation
|
||||
readStateTestSummaryJSON(t, onlyChildDir(t, roots.debug), "invocation.json", &invocation)
|
||||
if invocation.ConfigPath != selected {
|
||||
t.Fatalf("recorded config path = %q, want %q", invocation.ConfigPath, selected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveConfigOverlayOmitsBaseSelection(t *testing.T) {
|
||||
for _, overlay := range []string{"", "default"} {
|
||||
if got := effectiveConfigOverlay(overlay); got != "" {
|
||||
|
||||
Reference in New Issue
Block a user