Resolve named and directory imports within their selected filesystem
This commit is contained in:
@@ -266,6 +266,11 @@ native Go argument validation and native symlink target parsing remain separate.
|
||||
|
||||
## Stage 6 — Route named and directory imports through the shared resolver
|
||||
|
||||
Completed: named/list/directory selectors resolve before candidate normalization
|
||||
and use existing target validation and acquisition. Host external/transitive,
|
||||
absence, union, repeat, cycle, explicit-bound and native-root tests pass, as do
|
||||
the existing directory selection policy/link tests.
|
||||
|
||||
Update `selectNamedImportCandidates` and `selectDirectoryImportCandidates` to
|
||||
resolve each authored selector before candidate normalization. Preserve source
|
||||
sites for list elements, candidate union/exclusion order, lexical selection
|
||||
|
||||
85
internal/engine/absolute_import_test.go
Normal file
85
internal/engine/absolute_import_test.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func TestExternalNamedAndDirectoryImportAcquisition(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := canonicalTestPath(t, t.TempDir())
|
||||
first := writeImportFile(t, root, "shared/first.yml", "config:\n imports: [{path: nested/second.yml}]\nitems: {first: one}\n")
|
||||
writeImportFile(t, root, "shared/nested/second.yml", "items: {second: two}\n")
|
||||
for _, entry := range []struct{ name, selector string }{
|
||||
{"absolute", "path: " + strconv.Quote(first)},
|
||||
{"parent relative", "path: ../shared/first.yml"},
|
||||
{"path union", "path: [" + strconv.Quote(first) + ", ../shared/first.yml]"},
|
||||
{"directory", "dir: " + strconv.Quote(filepath.Dir(first)) + "\n pattern: first.yml"},
|
||||
} {
|
||||
t.Run(entry.name, func(t *testing.T) {
|
||||
main := writeImportFile(t, root, "app/main.yml", "config:\n imports:\n - "+entry.selector+"\n required: true\nitems: {main: selected}\n")
|
||||
request := testImportRequest(File(main), nil, parseImportMain(t, main), Path{}, nil, nil)
|
||||
_, sources, diagnostics := testAcquireImportWithLimits(request, defaultImportLimits())
|
||||
if len(diagnostics) != 0 || len(sources) != 3 {
|
||||
t.Fatalf("sources=%#v, diagnostics=%#v", sources, diagnostics)
|
||||
}
|
||||
if sources[1].Path != first || sources[2].Path != filepath.Join(filepath.Dir(first), "nested", "second.yml") {
|
||||
t.Fatalf("sources=%#v", sources)
|
||||
}
|
||||
request.input.input.filesystem = &FilesystemContext{Root: filepath.Dir(main)}
|
||||
_, _, diagnostics = testAcquireImportWithLimits(request, defaultImportLimits())
|
||||
if len(diagnostics) == 0 || diagnostics[0].Code != CodeImportOutsideRoot {
|
||||
t.Fatalf("bounded diagnostics=%#v", diagnostics)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsoluteImportAdmissionAndAbsence(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := canonicalTestPath(t, t.TempDir())
|
||||
first := writeImportFile(t, root, "shared/first.yml", "items: {first: one}\n")
|
||||
for _, entry := range []struct {
|
||||
name, imports string
|
||||
code Code
|
||||
}{
|
||||
{name: "optional missing", imports: " - path: " + strconv.Quote(filepath.Join(root, "missing.yml"))},
|
||||
{name: "required missing", imports: " - path: " + strconv.Quote(filepath.Join(root, "missing.yml")) + "\n required: true", code: CodeImportMissing},
|
||||
{name: "repeated spelling", imports: " - path: " + strconv.Quote(first) + "\n - path: ../shared/first.yml", code: CodeImportRepeated},
|
||||
{name: "cycle", imports: " - path: " + strconv.Quote(filepath.Join(root, "app/main.yml")), code: CodeImportCycle},
|
||||
} {
|
||||
t.Run(entry.name, func(t *testing.T) {
|
||||
main := writeImportFile(t, root, "app/main.yml", "config:\n imports:\n"+entry.imports+"\n")
|
||||
_, _, diagnostics := testAcquireImportWithLimits(testImportRequest(File(main), nil, parseImportMain(t, main), Path{}, nil, nil), defaultImportLimits())
|
||||
if entry.code == "" {
|
||||
if len(diagnostics) != 0 {
|
||||
t.Fatal(diagnostics)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(diagnostics) == 0 || diagnostics[0].Code != entry.code {
|
||||
t.Fatalf("diagnostics=%#v", diagnostics)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeRootNamedAndDirectoryImports(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, selector := range []string{"path: /shared/part.yml", "dir: /shared\n pattern: part.yml"} {
|
||||
filesystem := fstest.MapFS{
|
||||
"app/main.yml": &fstest.MapFile{Data: []byte("config:\n imports:\n - " + selector + "\n")},
|
||||
"shared/part.yml": &fstest.MapFile{Data: []byte("config:\n imports: [{path: ../neighbor.yml}]\nitems: {shared: part}\n")},
|
||||
"neighbor.yml": &fstest.MapFile{Data: []byte("items: {neighbor: transitive}\n")},
|
||||
}
|
||||
result, err := testLoad[importTestConfig](filesystemFile(newNativeNamespace("bundle", filesystem), "app/main.yml"), Options[importTestConfig]{Imports: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Config.Items["shared"] != "part" || result.Config.Items["neighbor"] != "transitive" {
|
||||
t.Fatalf("items = %#v", result.Config.Items)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,21 +25,19 @@ func selectNamedImportCandidates(access *filesystemAccess, basePath string, spec
|
||||
normalized := make([]selectedImportCandidate, 0, len(spec.paths))
|
||||
seen := make(map[string]struct{}, len(spec.paths))
|
||||
for index, relative := range spec.paths {
|
||||
accessPath := access.join(basePath, relative)
|
||||
candidatePath := accessPath
|
||||
if access.native == nil {
|
||||
candidatePath = filepath.ToSlash(filepath.Clean(accessPath))
|
||||
site := spec.fieldSites["path"]
|
||||
if index < len(spec.selectorSites) {
|
||||
site = spec.selectorSites[index]
|
||||
}
|
||||
accessPath, err := resolveLiteralImportSelector(access, basePath, relative)
|
||||
if err != nil {
|
||||
return importSelection{}, []Diagnostic{*importSelectionDiagnostic(site, CodeImportOutsideRoot, "import selector is outside the configuration root")}
|
||||
}
|
||||
candidatePath := normalizedImportCandidatePath(access, accessPath)
|
||||
if _, exists := seen[candidatePath]; exists {
|
||||
continue
|
||||
}
|
||||
seen[candidatePath] = struct{}{}
|
||||
var site importFieldSite
|
||||
if index < len(spec.selectorSites) {
|
||||
site = spec.selectorSites[index]
|
||||
} else {
|
||||
site = spec.fieldSites["path"]
|
||||
}
|
||||
normalized = append(normalized, selectedImportCandidate{path: candidatePath, accessPath: accessPath, label: relativeImportLabel(access, basePath, accessPath), site: site})
|
||||
}
|
||||
sort.Slice(normalized, func(left, right int) bool {
|
||||
@@ -67,16 +65,19 @@ func selectDirectoryImportCandidates(access *filesystemAccess, basePath string,
|
||||
roots := make([]selectedImportCandidate, 0, len(spec.paths))
|
||||
seenRoots := make(map[string]struct{}, len(spec.paths))
|
||||
for index, relative := range spec.paths {
|
||||
accessPath := access.join(basePath, relative)
|
||||
site := spec.fieldSites["dir"]
|
||||
if index < len(spec.selectorSites) {
|
||||
site = spec.selectorSites[index]
|
||||
}
|
||||
accessPath, err := resolveLiteralImportSelector(access, basePath, relative)
|
||||
if err != nil {
|
||||
return importSelection{}, []Diagnostic{*importSelectionDiagnostic(site, CodeImportOutsideRoot, "import selector is outside the configuration root")}
|
||||
}
|
||||
candidatePath := normalizedImportCandidatePath(access, accessPath)
|
||||
if _, exists := seenRoots[candidatePath]; exists {
|
||||
continue
|
||||
}
|
||||
seenRoots[candidatePath] = struct{}{}
|
||||
site := spec.fieldSites["dir"]
|
||||
if index < len(spec.selectorSites) {
|
||||
site = spec.selectorSites[index]
|
||||
}
|
||||
roots = append(roots, selectedImportCandidate{path: candidatePath, accessPath: accessPath, site: site})
|
||||
}
|
||||
sort.Slice(roots, func(left, right int) bool { return roots[left].path < roots[right].path })
|
||||
|
||||
Reference in New Issue
Block a user