Provide passive resource contexts for external imported sources
This commit is contained in:
@@ -350,6 +350,11 @@ behavior rather than behaving like a prematurely cleaned literal path.
|
||||
|
||||
## Stage 9 — Produce passive contexts for external imported sources
|
||||
|
||||
Completed: external unbounded host sources receive their own passive directory
|
||||
root; contained/native/bounded coordinates remain intact. Resource tests cover
|
||||
origins, resolution, failed validation inspection, synthetic contexts and imports
|
||||
beyond an external source's resource directory.
|
||||
|
||||
Update `contextsForResolvedSources` in `source_context.go` to select a passive
|
||||
root per source under the rules above. Keep the context anchor unchanged for
|
||||
existing contained sources. External unbounded host imports use their own
|
||||
|
||||
@@ -4,8 +4,10 @@ import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
@@ -370,3 +372,70 @@ func TestDirectorySelectorsTreatMissingOnOpenAsOptional(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalImportResourceContexts(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := canonicalTestPath(t, t.TempDir())
|
||||
external := writeImportFile(t, root, "shared/fragment.yml", "config:\n imports: [{path: ../other/extra.yml}]\nitems: {external: resource.txt}\n")
|
||||
writeImportFile(t, root, "shared/resource.txt", "external resource")
|
||||
writeImportFile(t, root, "other/extra.yml", "items: {transitive: retained}\n")
|
||||
main := writeImportFile(t, root, "app/main.yml", "config:\n imports:\n - path: "+strconv.Quote(external)+"\n - path: fragments/contained.yml\nitems: {main: resource.txt}\n")
|
||||
writeImportFile(t, root, "app/fragments/contained.yml", "items: {contained: resource.txt}\n")
|
||||
writeImportFile(t, root, "app/fragments/resource.txt", "contained resource")
|
||||
writeImportFile(t, root, "app/resource.txt", "main resource")
|
||||
check := func(inspection *Inspection) {
|
||||
t.Helper()
|
||||
for _, entry := range []struct{ key, base, content string }{
|
||||
{"main", ".", "main resource"}, {"contained", "fragments", "contained resource"}, {"external", ".", "external resource"},
|
||||
} {
|
||||
info, ok := inspection.Metadata().Lookup(Keys("items", entry.key))
|
||||
if !ok || len(info.Origin.Locations) == 0 {
|
||||
t.Fatalf("origin missing: %#v", info)
|
||||
}
|
||||
context, ok := inspection.SourceContext(info.Origin.Locations[0].SourceID)
|
||||
if !ok || context.BaseDir != entry.base {
|
||||
t.Fatalf("%s context=%#v, %v", entry.key, context, ok)
|
||||
}
|
||||
data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, "resource.txt"))
|
||||
if err != nil || string(data) != entry.content {
|
||||
t.Fatalf("%s resource=%q, %v", entry.key, data, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
options := Options[importTestConfig]{Imports: true, Definition: Definition[importTestConfig]{Resolve: func(_ *importTestConfig, resolution *Resolution) error {
|
||||
context, ok := resolution.SourceContext("source-2")
|
||||
if !ok {
|
||||
return errors.New("missing external context during resolution")
|
||||
}
|
||||
if _, err := fs.ReadFile(context.FS, "resource.txt"); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}}}
|
||||
result, err := testLoad(File(main), options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Config.Items["transitive"] != "retained" {
|
||||
t.Fatal("resource root constrained transitive import")
|
||||
}
|
||||
check(result.Inspection)
|
||||
options.Definition.Validate = func(_ *importTestConfig, validation *Validation) { validation.Error(Keys("items"), "rejected") }
|
||||
_, err = testLoad(File(main), options)
|
||||
var failure *LoadError
|
||||
if !errors.As(err, &failure) || failure.Inspection() == nil {
|
||||
t.Fatalf("failed inspection=%v", err)
|
||||
}
|
||||
check(failure.Inspection())
|
||||
synthetic, err := testLoad[importTestConfig](Bytes("synthetic", []byte("items: {own: resource.txt}\n")), Options[importTestConfig]{Filesystem: &FilesystemContext{BaseDir: filepath.Dir(main)}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
context, ok := synthetic.Inspection.SourceContext("source-1")
|
||||
if !ok || context.BaseDir != "." {
|
||||
t.Fatalf("synthetic context=%#v, %v", context, ok)
|
||||
}
|
||||
if data, err := fs.ReadFile(context.FS, "resource.txt"); err != nil || string(data) != "main resource" {
|
||||
t.Fatalf("synthetic resource=%q, %v", data, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,6 @@ func contextsForResolvedSources(resolved *resolvedFilesystemContext, sources []S
|
||||
if resolved.native != nil {
|
||||
filesystem = resolved.native.filesystem
|
||||
}
|
||||
var rooted fs.FS
|
||||
if filesystem != nil {
|
||||
rooted = rootedNativeFS{filesystem: filesystem, root: rootPath}
|
||||
} else {
|
||||
rooted = rootedHostFS{root: rootPath}
|
||||
}
|
||||
result := make(map[string]ResourceContext, len(sources))
|
||||
for _, acquired := range sources {
|
||||
base := basePath
|
||||
@@ -35,6 +29,16 @@ func contextsForResolvedSources(resolved *resolvedFilesystemContext, sources []S
|
||||
base = filepath.Dir(acquired.Path)
|
||||
}
|
||||
}
|
||||
resourceRoot := rootPath
|
||||
if filesystem == nil && resolved.importRoot == "" && !containedPath(resourceRoot, base) {
|
||||
resourceRoot = base
|
||||
}
|
||||
var rooted fs.FS
|
||||
if filesystem != nil {
|
||||
rooted = rootedNativeFS{filesystem: filesystem, root: resourceRoot}
|
||||
} else {
|
||||
rooted = rootedHostFS{root: resourceRoot}
|
||||
}
|
||||
var relative string
|
||||
if filesystem != nil {
|
||||
if !containsNativePath(rootPath, base) {
|
||||
@@ -49,12 +53,12 @@ func contextsForResolvedSources(resolved *resolvedFilesystemContext, sources []S
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if !containedPath(rootPath, base) {
|
||||
if !containedPath(resourceRoot, base) {
|
||||
value := importDiagnostic(CodeImportOutsideRoot, "source base is outside the configuration root", nil)
|
||||
return nil, &value
|
||||
}
|
||||
var err error
|
||||
relative, err = filepath.Rel(rootPath, base)
|
||||
relative, err = filepath.Rel(resourceRoot, base)
|
||||
if err != nil {
|
||||
value := importDiagnostic(CodeImportInvalid, "source base could not be addressed in the configuration root", nil)
|
||||
return nil, &value
|
||||
|
||||
Reference in New Issue
Block a user