Schedule ordinary import contributions by dependency

This commit is contained in:
2026-10-19 19:29:35 +00:00
parent 91ab37a39d
commit 5ed0f55970
7 changed files with 159 additions and 13 deletions

View File

@@ -91,6 +91,80 @@ settings:
}
}
func TestLoadOrdinaryImportsUseDependencyReadyCompositionOrder(t *testing.T) {
type task struct {
ID string `yaml:"id"`
}
type config struct {
Tasks []task `yaml:"tasks"`
}
directory := t.TempDir()
mainPath := writeImportFile(t, directory, "main.yml", `configkit:
imports:
- path: z-parent.yml
- path: a-independent.yml
tasks: [{id: main}]
`)
zParentPath := writeImportFile(t, directory, "z-parent.yml", `configkit:
imports:
- path: b-dependency.yml
tasks: [{id: z-parent}]
`)
aIndependentPath := writeImportFile(t, directory, "a-independent.yml", "tasks: [{id: a-independent}]\n")
bDependencyPath := writeImportFile(t, directory, "b-dependency.yml", "tasks: [{id: b-dependency}]\n")
input, diagnostics := assembleBaseInput[config](File(mainPath), Options[config]{
Imports: true,
sequencePolicy: sequencePolicy{identityFields: map[string]string{"/tasks": "id"}},
})
if len(diagnostics) != 0 {
t.Fatalf("assemble imports: %#v", diagnostics)
}
tasks, ok := documentAtPath(input.document, Keys("tasks"))
if !ok || tasks.Kind != document.Sequence {
t.Fatalf("tasks document = %#v, %v", tasks, ok)
}
wantOrder := []string{"a-independent", "b-dependency", "z-parent", "main"}
if len(tasks.Sequence) != len(wantOrder) {
t.Fatalf("tasks = %#v", tasks.Sequence)
}
for index, want := range wantOrder {
id, found := documentAtPath(tasks, Path{}.Index(index).Key("id"))
if !found || id.Text != want {
t.Errorf("task %d ID = %#v, want %q", index, id, want)
}
}
wantAcquisition := []string{
canonicalTestPath(t, mainPath), canonicalTestPath(t, zParentPath),
canonicalTestPath(t, bDependencyPath), canonicalTestPath(t, aIndependentPath),
}
for index, want := range wantAcquisition {
if got := input.sources[index].Path; got != want {
t.Errorf("acquired source %d = %q, want %q", index, got, want)
}
}
}
func TestAcquiredContextsUseSelectedFileWhenSourceRecordsAreReordered(t *testing.T) {
directory := t.TempDir()
mainPath := writeImportFile(t, directory, "main.yml", "settings: {root: main}\n")
importedPath := writeImportFile(t, directory, "nested/imported.yml", "settings: {nested: imported}\n")
sources := []SourceInfo{
{ID: "source-2", Path: canonicalTestPath(t, importedPath), Kind: SourceKindFile},
{ID: "source-1", Path: canonicalTestPath(t, mainPath), Kind: SourceKindFile},
}
contexts, diagnostic := contextsForAcquiredSources(File(mainPath), nil, nil, sources)
if diagnostic != nil {
t.Fatalf("source contexts: %#v", diagnostic)
}
if got := contexts["source-1"].BaseDir; got != "." {
t.Errorf("main source base = %q, want root-relative current directory", got)
}
if got := contexts["source-2"].BaseDir; got != "nested" {
t.Errorf("imported source base = %q, want nested", got)
}
}
func TestLoadImportsFromProfilesDirectoryAsBaseInput(t *testing.T) {
directory := t.TempDir()
mainPath := writeImportFile(t, directory, "main.yml", "configkit: {imports: [{path: profiles/shared.yml}]}\n")