2026-09-13 16:48:44 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2026-11-17 20:42:25 +00:00
|
|
|
configkit "go.maximumdirect.net/configkit"
|
2026-09-13 16:48:44 +00:00
|
|
|
)
|
|
|
|
|
|
2026-11-18 00:36:26 +00:00
|
|
|
func TestSourceDirectoryLookupIsMemoizedPerLoad(t *testing.T) {
|
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
state := newDocumentLoadState(dir)
|
|
|
|
|
calls := make(map[string]int)
|
|
|
|
|
lookup := func(id string) (configkit.SourceInfo, bool) {
|
|
|
|
|
calls[id]++
|
|
|
|
|
if id == "missing" {
|
|
|
|
|
return configkit.SourceInfo{}, false
|
|
|
|
|
}
|
|
|
|
|
if id == "memory" {
|
|
|
|
|
return configkit.SourceInfo{}, true
|
|
|
|
|
}
|
|
|
|
|
return configkit.SourceInfo{Path: filepath.Join(dir, id, "config.yml")}, true
|
|
|
|
|
}
|
|
|
|
|
for range 2 {
|
|
|
|
|
for _, id := range []string{"first", "second", "missing", "memory"} {
|
|
|
|
|
want := ""
|
|
|
|
|
if id == "first" || id == "second" {
|
|
|
|
|
want = filepath.Join(dir, id)
|
|
|
|
|
}
|
|
|
|
|
if got := state.sourceDirectory(id, lookup); got != want {
|
|
|
|
|
t.Fatalf("directory(%s) = %q, want %q", id, got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for id, count := range calls {
|
|
|
|
|
if count != 1 {
|
|
|
|
|
t.Fatalf("source %s queried %d times", id, count)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
other := newDocumentLoadState(dir)
|
|
|
|
|
if got := other.sourceDirectory("first", func(string) (configkit.SourceInfo, bool) {
|
|
|
|
|
return configkit.SourceInfo{Path: filepath.Join(dir, "changed", "config.yml")}, true
|
|
|
|
|
}); got != filepath.Join(dir, "changed") {
|
|
|
|
|
t.Fatalf("another load reused directory %q", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-13 16:48:44 +00:00
|
|
|
func TestDocumentPathsUseDeclaringFilesAcrossImports(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
main := filepath.Join(root, "notarius.yml")
|
|
|
|
|
fragmentDirectory := filepath.Join(root, "fragments")
|
|
|
|
|
if err := os.MkdirAll(fragmentDirectory, 0o700); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
fragment := filepath.Join(fragmentDirectory, "pipelines.yml")
|
|
|
|
|
if err := os.WriteFile(fragment, []byte(`pipelines:
|
|
|
|
|
imported:
|
|
|
|
|
input: {module: input}
|
|
|
|
|
references:
|
|
|
|
|
source: {path: ./from-fragment.txt}
|
|
|
|
|
`), 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := os.WriteFile(main, []byte(`version: 5
|
2026-11-17 20:44:07 +00:00
|
|
|
config:
|
2026-09-13 16:48:44 +00:00
|
|
|
imports:
|
|
|
|
|
- path: fragments/pipelines.yml
|
|
|
|
|
output: {directory: ./output}
|
|
|
|
|
debug: {directory: ./debug}
|
|
|
|
|
pipelines:
|
|
|
|
|
main:
|
|
|
|
|
input: {module: input}
|
|
|
|
|
references:
|
|
|
|
|
source: {path: ./from-main.txt}
|
|
|
|
|
`), 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loaded, err := loadConfig(main, LoadOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if got := loaded.Config.Pipelines["main"].References["source"].Path; got != filepath.Join(root, "from-main.txt") {
|
|
|
|
|
t.Fatalf("main reference = %q", got)
|
|
|
|
|
}
|
|
|
|
|
if got := loaded.Config.Pipelines["imported"].References["source"].Path; got != filepath.Join(fragmentDirectory, "from-fragment.txt") {
|
|
|
|
|
t.Fatalf("fragment reference = %q", got)
|
|
|
|
|
}
|
|
|
|
|
if loaded.Config.Output.Directory != filepath.Join(root, "output") || loaded.Config.Debug.Directory != filepath.Join(root, "debug") {
|
|
|
|
|
t.Fatalf("destinations = %#v %#v", loaded.Config.Output, loaded.Config.Debug)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 20:44:07 +00:00
|
|
|
func TestDiscoveredMainAndStepReferenceNormalization(t *testing.T) {
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
fragmentDir := filepath.Join(root, "fragments")
|
|
|
|
|
if err := os.Mkdir(fragmentDir, 0700); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
main := filepath.Join(root, "notarius.yml")
|
|
|
|
|
fragment := filepath.Join(fragmentDir, "pipeline.yml")
|
|
|
|
|
if err := os.WriteFile(main, []byte("version: 5\nconfig:\n imports:\n - path: fragments/pipeline.yml\n required: true\n"), 0600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := os.WriteFile(fragment, []byte(`pipelines:
|
|
|
|
|
session:
|
|
|
|
|
input: {module: input}
|
|
|
|
|
steps:
|
|
|
|
|
- id: " first "
|
|
|
|
|
references:
|
|
|
|
|
" source ": {path: " ./source.txt "}
|
|
|
|
|
artifacts:
|
|
|
|
|
result:
|
|
|
|
|
extract: {module: " extract "}
|
2026-11-17 20:48:05 +00:00
|
|
|
- id: second
|
|
|
|
|
artifacts:
|
|
|
|
|
next:
|
|
|
|
|
extract: {module: " extract "}
|
2026-11-17 20:44:07 +00:00
|
|
|
`), 0600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
loaded, err := LoadSource(configkit.SearchConfig("", configkit.SearchPaths(main), configkit.SearchOptions{Required: true}), LoadOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if loaded.MainSourcePath() != main || loaded.Config.Output.Directory != filepath.Join(root, "notarius-output") {
|
|
|
|
|
t.Fatalf("discovered main or default directory: %q, %q", loaded.MainSourcePath(), loaded.Config.Output.Directory)
|
|
|
|
|
}
|
|
|
|
|
step := loaded.Config.Pipelines["session"].Steps[0]
|
|
|
|
|
if step.ID != "first" || step.Artifacts["result"].Extract.Module != "extract" || step.References["source"].Path != filepath.Join(fragmentDir, "source.txt") {
|
|
|
|
|
t.Fatalf("normalized step = %#v", step)
|
|
|
|
|
}
|
2026-11-17 20:48:05 +00:00
|
|
|
history := loaded.result.Metadata().History(configkit.Keys("pipelines", "session", "steps").Index(1).Key("artifacts").Key("next").Key("extract").Key("module"))
|
2026-11-17 20:44:07 +00:00
|
|
|
if len(history) < 2 || history[len(history)-1].Origin.Operation != configkit.ChangeNormalization {
|
|
|
|
|
t.Fatalf("module leaf history = %#v", history)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-13 16:48:44 +00:00
|
|
|
func TestDocumentPathsUseInvocationDirectoryForOverrides(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
configPath := filepath.Join(root, "notarius.yml")
|
|
|
|
|
if err := os.WriteFile(configPath, []byte("version: 5\n"), 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
invocation := t.TempDir()
|
|
|
|
|
loaded, err := loadConfig(configPath, LoadOptions{
|
|
|
|
|
InvocationDirectory: invocation,
|
|
|
|
|
Overrides: []configkit.Override{{Path: configkit.Keys("output", "directory"), Value: "generated", Flag: "--output"}},
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if got := loaded.Config.Output.Directory; got != filepath.Join(invocation, "generated") {
|
|
|
|
|
t.Fatalf("override directory = %q", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-17 20:53:07 +00:00
|
|
|
func TestDiscoveredSymlinkUsesCanonicalMainDirectory(t *testing.T) {
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
realDir := filepath.Join(root, "real")
|
|
|
|
|
if err := os.Mkdir(realDir, 0700); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
main := filepath.Join(realDir, "main.yml")
|
|
|
|
|
if err := os.WriteFile(main, []byte("version: 5\n"), 0600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
link := filepath.Join(root, "notarius.yml")
|
|
|
|
|
if err := os.Symlink(main, link); err != nil {
|
|
|
|
|
t.Skipf("symlink unavailable: %v", err)
|
|
|
|
|
}
|
|
|
|
|
loaded, err := LoadSource(configkit.SearchConfig("", configkit.SearchPaths(link), configkit.SearchOptions{Required: true}), LoadOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if loaded.MainSourcePath() != main || loaded.Config.Output.Directory != filepath.Join(realDir, "notarius-output") {
|
|
|
|
|
t.Fatalf("canonical source = %q, output = %q", loaded.MainSourcePath(), loaded.Config.Output.Directory)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRequiredSearchExhaustion(t *testing.T) {
|
|
|
|
|
missing := filepath.Join(t.TempDir(), "missing.yml")
|
|
|
|
|
_, err := LoadSource(configkit.SearchConfig("", configkit.SearchPaths(missing), configkit.SearchOptions{Required: true}), LoadOptions{})
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("missing required search succeeded")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 21:46:35 +00:00
|
|
|
func TestDocumentPathsUseOverlayDeclarationDirectory(t *testing.T) {
|
2026-09-13 16:48:44 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
root := t.TempDir()
|
2026-09-25 21:46:35 +00:00
|
|
|
overlays := filepath.Join(root, "overlays")
|
|
|
|
|
if err := os.MkdirAll(overlays, 0o700); err != nil {
|
2026-09-13 16:48:44 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
main := filepath.Join(root, "notarius.yml")
|
|
|
|
|
if err := os.WriteFile(main, []byte("version: 5\n"), 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-09-25 21:46:35 +00:00
|
|
|
if err := os.WriteFile(filepath.Join(overlays, "development.yml"), []byte("output: {directory: ./overlay-output}\n"), 0o600); err != nil {
|
2026-09-13 16:48:44 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-09-25 21:46:35 +00:00
|
|
|
loaded, err := loadConfig(main, LoadOptions{Overlay: "development"})
|
2026-09-13 16:48:44 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
|
|
|
}
|
2026-09-25 21:46:35 +00:00
|
|
|
if got := loaded.Config.Output.Directory; got != filepath.Join(overlays, "overlay-output") {
|
|
|
|
|
t.Fatalf("overlay directory = %q", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDocumentLoadDoesNotFallBackToLegacyProfileDirectory(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
main := filepath.Join(root, "notarius.yml")
|
|
|
|
|
if err := os.WriteFile(main, []byte("version: 5\n"), 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
legacy := filepath.Join(root, "profiles", "development.yml")
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(legacy), 0o700); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if err := os.WriteFile(legacy, []byte("output: {directory: ./legacy-output}\n"), 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := loadConfig(main, LoadOptions{Overlay: "development"}); err == nil {
|
|
|
|
|
t.Fatal("loadConfig() error = nil, want missing overlay")
|
2026-09-13 16:48:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDocumentPathsKeepEmptyCacheDirectoriesAndAbsolutePaths(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
configPath := filepath.Join(root, "notarius.yml")
|
|
|
|
|
abs := filepath.Join(t.TempDir(), "outside.txt")
|
|
|
|
|
if err := os.WriteFile(configPath, []byte("version: 5\ncache:\n chunk_plans: {directory: ''}\n checkpoints: {directory: ''}\npipelines:\n main:\n input: {module: input}\n references:\n outside: {path: "+abs+"}\n"), 0o600); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
loaded, err := loadConfig(configPath, LoadOptions{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("loadConfig() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if loaded.Config.Cache.ChunkPlans.Directory != "" || loaded.Config.Cache.Checkpoints.Directory != "" {
|
|
|
|
|
t.Fatalf("cache directories = %#v", loaded.Config.Cache)
|
|
|
|
|
}
|
|
|
|
|
if got := loaded.Config.Pipelines["main"].References["outside"].Path; got != abs {
|
|
|
|
|
t.Fatalf("absolute reference = %q", got)
|
|
|
|
|
}
|
|
|
|
|
}
|