Prepare reusable CLI override batches

This commit is contained in:
2026-10-23 21:02:46 +00:00
parent 0023617a07
commit 23f22d8982
4 changed files with 151 additions and 15 deletions

View File

@@ -446,6 +446,85 @@ func TestOverrideAggregateLimitsFailBeforeApplication(t *testing.T) {
}
}
func TestPreparedOverrideBatchDetachesInputsAndAllocatesSourcesInOrder(t *testing.T) {
base, diagnostics := composeDocuments(parseCompositionDocument(t, "base", "settings: {}\n"))
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
sources := []SourceInfo{{ID: "source-1", Label: "base", Layer: LayerBase, Kind: SourceKindMapping}}
firstValue := map[string]any{"nested": map[string]any{"name": "original"}}
secondValue := []any{map[string]any{"enabled": true}}
batch, diagnostics := prepareOverrides(base, sources, []Override{
{Path: Keys("settings", "first"), Value: firstValue, Flag: "--first"},
{Path: Keys("settings", "second"), Value: secondValue, Flag: "--second"},
}, nil, defaultImportLimits())
if len(diagnostics) != 0 {
t.Fatalf("prepare overrides: %#v", diagnostics)
}
if len(batch.overrides) != 2 || len(batch.sources) != 3 {
t.Fatalf("prepared counts = %d overrides, %d sources", len(batch.overrides), len(batch.sources))
}
for index, want := range []string{"source-2", "source-3"} {
if batch.overrides[index].source.ID != want || batch.sources[index+1].ID != want {
t.Errorf("prepared source %d = %#v / %#v, want %s", index, batch.overrides[index].source, batch.sources[index+1], want)
}
}
if batch.sources[1].Label != "--first" || batch.sources[2].Label != "--second" {
t.Fatalf("synthetic source order = %#v", batch.sources)
}
firstValue["nested"].(map[string]any)["name"] = "changed after preparation"
preparedName, found := documentAtPath(batch.overrides[0].value, Keys("nested", "name"))
if !found || preparedName.Text != "original" {
t.Fatalf("prepared override aliases input value: %#v, %v", preparedName, found)
}
preparedName.Text = "changed prepared snapshot"
if firstValue["nested"].(map[string]any)["name"] != "changed after preparation" {
t.Fatal("prepared override mutation changed caller input")
}
batch.sources[0].Label = "changed batch source"
if sources[0].Label != "base" {
t.Fatal("prepared source list aliases the input source list")
}
if len(base.root.Mapping) != 1 || base.root.Mapping[0].Value.Kind != document.Mapping || len(base.root.Mapping[0].Value.Mapping) != 0 {
t.Fatalf("preparation mutated assembled input: %#v", base.root)
}
}
func TestPostStructuralOverridePreparationRejectsInheritanceDirectives(t *testing.T) {
base, diagnostics := composeDocuments(parseCompositionDocument(t, "base", "child: {}\nitems: []\n"))
if len(diagnostics) != 0 {
t.Fatal(diagnostics)
}
tests := []struct {
name string
override Override
path string
}{
{
name: "supplied path",
override: Override{Path: Keys("child", "extends"), Value: "parent", Flag: "--extends"},
path: "/child/extends",
},
{
name: "nested value",
override: Override{Path: Keys("items"), Value: []any{map[string]any{"extends": "parent"}}, Flag: "--items"},
path: "/items/0/extends",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if _, diagnostics := prepareOverrides(base, nil, []Override{test.override}, nil, defaultImportLimits()); len(diagnostics) != 0 {
t.Fatalf("legacy preparation changed before lifecycle activation: %#v", diagnostics)
}
_, diagnostics := preparePostStructuralOverrides(overridePreparationInputFor(base), nil, []Override{test.override}, nil, defaultImportLimits())
if len(diagnostics) != 1 || diagnostics[0].Code != CodeOverrideDirective || diagnostics[0].Path.String() != test.path {
t.Fatalf("post-structural diagnostics = %#v, want directive at %s", diagnostics, test.path)
}
})
}
}
func historyLayers(history []Change) []Layer {
layers := make([]Layer, len(history))
for index, change := range history {