86 lines
3.8 KiB
Go
86 lines
3.8 KiB
Go
|
|
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)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|