Verify external imports across the public loading lifecycle
This commit is contained in:
@@ -430,6 +430,11 @@ and the existing secrets contract.
|
||||
|
||||
## Stage 12 — Verify public lifecycle integration and minor compatibility
|
||||
|
||||
Completed: public tests cover an external mounted fragment, inherited resource
|
||||
origins/history, safe equivalent failure inspection and source-version evidence.
|
||||
Legacy implicit-containment tests now request explicit roots with deterministic
|
||||
temporary paths. The focused engine/public suites and ./scripts/check.sh pass.
|
||||
|
||||
Use public facade tests for a representative externally imported fragment that
|
||||
is mounted into a mapping or identified sequence, contributes an inherited
|
||||
value, and supplies a relative resource reference. Reuse current lifecycle
|
||||
|
||||
126
facade_absolute_import_test.go
Normal file
126
facade_absolute_import_test.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package configkit_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAbsoluteImportMountedInheritanceAndFailureInspection(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := t.TempDir()
|
||||
write := func(name, content string) string {
|
||||
t.Helper()
|
||||
file := filepath.Join(root, name)
|
||||
if err := os.MkdirAll(filepath.Dir(file), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(file, []byte(content), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return file
|
||||
}
|
||||
fragment := write("shared/fragment.yml", "parent: {resource: asset.txt, enabled: false, count: 0}\n")
|
||||
write("shared/asset.txt", "external resource")
|
||||
main := write("app/main.yml", "config:\n imports:\n - path: "+strconv.Quote(fragment)+"\n destination: [entries]\nentries:\n child: {extends: parent}\n")
|
||||
load := func(reject bool) configkit.Inspection {
|
||||
options := configkit.Options[inheritedConfiguration]{Imports: true, Definition: configkit.Definition[inheritedConfiguration]{
|
||||
Sensitive: func(p configkit.Path) bool { return strings.HasSuffix(p.String(), "/resource") },
|
||||
Validate: func(_ *inheritedConfiguration, v *configkit.Validation) {
|
||||
if reject {
|
||||
v.Error(configkit.Keys("entries", "child"), "rejected")
|
||||
}
|
||||
},
|
||||
}}
|
||||
result, err := configkit.Load(configkit.File(main), options)
|
||||
if reject {
|
||||
var failure *configkit.LoadError
|
||||
if result != nil || !errors.As(err, &failure) {
|
||||
t.Fatalf("rejected=%v, %v", result, err)
|
||||
}
|
||||
view, ok := failure.InspectSubtree(configkit.Path{})
|
||||
if !ok {
|
||||
t.Fatal("missing failed inspection")
|
||||
}
|
||||
return view
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if child := result.Config.Entries["child"]; child.Resource != "asset.txt" || child.Enabled || child.Count != 0 {
|
||||
t.Fatalf("child=%#v", child)
|
||||
}
|
||||
view, ok := result.InspectSubtree(configkit.Path{})
|
||||
if !ok {
|
||||
t.Fatal("missing accepted inspection")
|
||||
}
|
||||
return view
|
||||
}
|
||||
accepted, rejected := load(false), load(true)
|
||||
if difference, err := accepted.Diff(rejected); err != nil || len(difference) != 0 {
|
||||
t.Fatalf("failure changed evidence=%#v, %v", difference, err)
|
||||
}
|
||||
for _, view := range []configkit.Inspection{accepted, rejected} {
|
||||
resource := configkit.Keys("entries", "child", "resource")
|
||||
info, ok := view.Metadata().Lookup(resource)
|
||||
if !ok || len(info.Origin.Locations) == 0 || len(info.Origin.Inheritance) == 0 {
|
||||
t.Fatalf("inherited origin=%#v", info)
|
||||
}
|
||||
id := info.Origin.Locations[0].SourceID
|
||||
context, ok := view.SourceContext(id)
|
||||
if !ok || context.BaseDir != "." {
|
||||
t.Fatalf("external context=%#v, %v", context, ok)
|
||||
}
|
||||
data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, "asset.txt"))
|
||||
if err != nil || string(data) != "external resource" {
|
||||
t.Fatalf("resource=%q, %v", data, err)
|
||||
}
|
||||
sources := view.Sources()
|
||||
if len(sources) != 2 || sources[1].ID != id || sources[1].Destination.String() != "/entries" {
|
||||
t.Fatalf("placement=%#v", sources)
|
||||
}
|
||||
explanation, ok := view.Explain(resource)
|
||||
if !ok || len(explanation.History) == 0 || !explanation.Effective.Redacted {
|
||||
t.Fatalf("explanation=%#v", explanation)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
if err := view.WriteEffective(&output); err != nil || strings.Contains(output.String(), "asset.txt") {
|
||||
t.Fatalf("unsafe inspection=%q, %v", output.String(), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAbsoluteImportUsesSourceVersionProcessing(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := t.TempDir()
|
||||
child := filepath.Join(root, "versioned.yml")
|
||||
if err := os.WriteFile(child, []byte("version: v1\nvalue: imported\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
main := filepath.Join(t.TempDir(), "main.yml")
|
||||
if err := os.WriteFile(main, []byte("config: {imports: [{path: "+strconv.Quote(child)+"}]}\nversion: v1\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
checked := 0
|
||||
result, err := configkit.Load[map[string]any](configkit.File(main), configkit.Options[map[string]any]{Imports: true, SourceVersionMode: configkit.SourceVersionModeIfPresent, Definition: configkit.Definition[map[string]any]{Version: &configkit.VersionRule{Path: configkit.Keys("version"), Check: func(version configkit.Version, _ *configkit.Validation) error {
|
||||
checked++
|
||||
if version.Text != "v1" {
|
||||
return errors.New("unexpected version")
|
||||
}
|
||||
return nil
|
||||
}}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sources := result.Sources()
|
||||
if checked != 2 || len(sources) != 2 || sources[1].VersionEvidence == nil || sources[1].VersionEvidence.Version.Text != "v1" || result.Config["value"] != "imported" {
|
||||
t.Fatalf("checks=%d, sources=%#v, config=%#v", checked, sources, result.Config)
|
||||
}
|
||||
}
|
||||
@@ -96,9 +96,9 @@ func TestLoadGlobImportsRejectInvalidAndUnmatchedPatterns(t *testing.T) {
|
||||
{name: "recursive star segment", pattern: "empty/**/value.yml", code: CodeImportInvalid},
|
||||
{name: "recursive adjacent stars", pattern: "empty/value**.yml", code: CodeImportInvalid},
|
||||
{name: "trailing separator", pattern: "empty/", code: CodeImportInvalid},
|
||||
{name: "absolute", pattern: "/tmp/*.yml", code: CodeImportInvalid},
|
||||
{name: "absolute outside root", code: CodeImportOutsideRoot},
|
||||
{name: "URL", pattern: "https://example.invalid/*.yml", code: CodeImportInvalid},
|
||||
{name: "missing parent escape", pattern: "../missing/*.yml", code: CodeImportOutsideRoot},
|
||||
{name: "parent discovery escape", pattern: "../*/*.yml", code: CodeImportOutsideRoot},
|
||||
{name: "unmatched", pattern: "empty/*.yml", code: CodeImportMissing},
|
||||
}
|
||||
for _, test := range tests {
|
||||
@@ -107,8 +107,12 @@ func TestLoadGlobImportsRejectInvalidAndUnmatchedPatterns(t *testing.T) {
|
||||
if err := os.Mkdir(filepath.Join(directory, "empty"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mainPath := writeGlobRoot(t, directory, test.pattern, test.code == CodeImportMissing)
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true}, test.code)
|
||||
pattern := test.pattern
|
||||
if test.name == "absolute outside root" {
|
||||
pattern = filepath.ToSlash(filepath.Join(t.TempDir(), "*.yml"))
|
||||
}
|
||||
mainPath := writeGlobRoot(t, directory, pattern, test.code == CodeImportMissing)
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true, Filesystem: &FilesystemContext{Root: directory}}, test.code)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -185,7 +189,7 @@ func TestLoadGlobImportsConfineSymlinkDirectories(t *testing.T) {
|
||||
t.Skipf("symlinks unavailable: %v", err)
|
||||
}
|
||||
mainPath := writeGlobRoot(t, root, "linked/*.yml", false)
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true}, CodeImportOutsideRoot)
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true, Filesystem: &FilesystemContext{Root: root}}, CodeImportOutsideRoot)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -252,7 +252,7 @@ func TestLoadImportDirectiveValidationAndTemporaryGlobGate(t *testing.T) {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
mainPath := writeImportFile(t, directory, "main.yml", test.body)
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true}, test.code)
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true, Filesystem: &FilesystemContext{Root: directory}}, test.code)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -856,7 +856,7 @@ settings: {root: retained}
|
||||
t.Fatal(err)
|
||||
}
|
||||
}, code: CodeSourceNotRegular},
|
||||
{name: "absolute", path: "/tmp/configkit.yml", code: CodeImportInvalid},
|
||||
{name: "absolute outside root", code: CodeImportOutsideRoot},
|
||||
{name: "network URL", path: "https://example.invalid/config.yml", code: CodeImportInvalid},
|
||||
}
|
||||
for _, test := range tests {
|
||||
@@ -869,8 +869,12 @@ settings: {root: retained}
|
||||
if test.code == CodeImportMissing {
|
||||
required = "\n required: true"
|
||||
}
|
||||
mainPath := writeImportFile(t, directory, "main.yml", "config:\n imports:\n - path: \""+test.path+"\""+required+"\n")
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true}, test.code)
|
||||
selectedPath := test.path
|
||||
if test.name == "absolute outside root" {
|
||||
selectedPath = filepath.ToSlash(filepath.Join(t.TempDir(), "external.yml"))
|
||||
}
|
||||
mainPath := writeImportFile(t, directory, "main.yml", "config:\n imports:\n - path: \""+selectedPath+"\""+required+"\n")
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true, Filesystem: &FilesystemContext{Root: directory}}, test.code)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1245,7 +1249,7 @@ func TestLoadImportsCanonicalSymlinksAndKeepsHardLinksDistinct(t *testing.T) {
|
||||
t.Skipf("symlinks unavailable: %v", err)
|
||||
}
|
||||
mainPath := writeImportFile(t, root, "main.yml", "config: {imports: [{path: escape.yml}]}\n")
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true}, CodeImportOutsideRoot)
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true, Filesystem: &FilesystemContext{Root: root}}, CodeImportOutsideRoot)
|
||||
})
|
||||
|
||||
t.Run("hard links are path identities", func(t *testing.T) {
|
||||
@@ -1332,7 +1336,7 @@ func TestImportedParseAndDecodeDiagnosticsKeepTheirSource(t *testing.T) {
|
||||
func TestOptionalImportDoesNotSuppressUnsafeOrUnreadableTargets(t *testing.T) {
|
||||
directory := t.TempDir()
|
||||
mainPath := writeImportFile(t, directory, "outside.yml", "config: {imports: [{path: ../missing.yml}]}\n")
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true}, CodeImportOutsideRoot)
|
||||
requireLoadCode(t, File(mainPath), Options[importTestConfig]{Imports: true, Filesystem: &FilesystemContext{Root: directory}}, CodeImportOutsideRoot)
|
||||
|
||||
locked := writeImportFile(t, directory, "locked.yml", "settings: {root: locked}\n")
|
||||
if err := os.Chmod(locked, 0); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user