Preserve import provenance and resource contexts
This commit is contained in:
@@ -2,10 +2,12 @@ package configkit_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
@@ -219,3 +221,77 @@ func TestFacadeSyntheticSourceContextsAndContextlessSources(t *testing.T) {
|
||||
t.Fatal("CLI source returned a context")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFacadeRelativeImportDestinationsPreserveNativeResourceContexts(t *testing.T) {
|
||||
borrowed := &observedResourceFS{files: fstest.MapFS{
|
||||
"bundle/config/main.yml": &fstest.MapFile{Data: []byte("configkit:\n imports:\n - path: entries/parent.yml\n destination: [logical]\n")},
|
||||
"bundle/config/entries/parent.yml": &fstest.MapFile{Data: []byte("configkit:\n imports:\n - path: nested/details.yml\n destination: [details]\nresource: icon.txt\n")},
|
||||
"bundle/config/entries/nested/details.yml": &fstest.MapFile{Data: []byte("resource: detail.txt\n")},
|
||||
"bundle/config/entries/icon.txt": &fstest.MapFile{Data: []byte("parent-resource")},
|
||||
"bundle/config/entries/nested/detail.txt": &fstest.MapFile{Data: []byte("nested-resource")},
|
||||
}}
|
||||
namespace := configkit.NewFilesystem("bundle", borrowed)
|
||||
input := configkit.Input{
|
||||
Source: namespace.File("bundle/config/main.yml"), Imports: true,
|
||||
Filesystem: &configkit.FilesystemContext{Root: "bundle", FS: &namespace},
|
||||
}
|
||||
load := func() *configkit.Result[map[string]any] {
|
||||
t.Helper()
|
||||
result, err := configkit.LoadLayered[map[string]any](input)
|
||||
if err != nil {
|
||||
t.Fatalf("load relative native imports: %v", err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
result := load()
|
||||
wantDestinations := []string{configkit.Keys().String(), "/logical", "/logical/details"}
|
||||
wantPaths := []string{"bundle/config/main.yml", "bundle/config/entries/parent.yml", "bundle/config/entries/nested/details.yml"}
|
||||
sources := result.Sources()
|
||||
if len(sources) != len(wantPaths) {
|
||||
t.Fatalf("sources = %#v", sources)
|
||||
}
|
||||
for index, source := range sources {
|
||||
if source.ID != fmt.Sprintf("source-%d", index+1) || source.Kind != configkit.SourceKindFilesystem || source.Path != wantPaths[index] || source.Destination.String() != wantDestinations[index] {
|
||||
t.Errorf("source %d = %#v", index, source)
|
||||
}
|
||||
if index == 0 {
|
||||
if source.ImportedFrom != nil {
|
||||
t.Errorf("entry source has import location %#v", source.ImportedFrom)
|
||||
}
|
||||
} else if source.ImportedFrom == nil || source.ImportedFrom.SourceID != fmt.Sprintf("source-%d", index) || source.ImportedFrom.Line == 0 || source.ImportedFrom.Column == 0 {
|
||||
t.Errorf("source %d import location = %#v", index, source.ImportedFrom)
|
||||
}
|
||||
}
|
||||
|
||||
for _, test := range []struct {
|
||||
field, effectivePath, wantBase, wantResource, wantContents string
|
||||
}{
|
||||
{"parent resource", "/logical/resource", "config/entries", "icon.txt", "parent-resource"},
|
||||
{"nested resource", "/logical/details/resource", "config/entries/nested", "detail.txt", "nested-resource"},
|
||||
} {
|
||||
info, ok := result.Metadata().Lookup(configkit.Keys(strings.Split(strings.Trim(test.effectivePath, "/"), "/")...))
|
||||
if !ok || len(info.Origin.Locations) != 1 {
|
||||
t.Fatalf("%s origin = %#v, %v", test.field, info, ok)
|
||||
}
|
||||
context, ok := result.SourceContext(info.Origin.Locations[0].SourceID)
|
||||
if !ok || context.BaseDir != test.wantBase || context.FS == nil {
|
||||
t.Fatalf("%s context = %#v, %v", test.field, context, ok)
|
||||
}
|
||||
contents, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, test.wantResource))
|
||||
if err != nil || string(contents) != test.wantContents {
|
||||
t.Fatalf("%s opened resource = %q, %v", test.field, contents, err)
|
||||
}
|
||||
}
|
||||
|
||||
sources[1].Destination = configkit.Keys("caller", "mutation")
|
||||
for query := 0; query < 2; query++ {
|
||||
fresh := result.Sources()
|
||||
if fresh[1].Destination.String() != "/logical" {
|
||||
t.Fatalf("query %d source destination = %q", query, fresh[1].Destination.String())
|
||||
}
|
||||
}
|
||||
second := load()
|
||||
if !reflect.DeepEqual(second.Sources(), result.Sources()) {
|
||||
t.Fatalf("independent source records differ: %#v vs %#v", second.Sources(), result.Sources())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user