Strengthen composition and import selection tests
This commit is contained in:
@@ -43,9 +43,10 @@ func FuzzCompositionAndOverlay(f *testing.F) {
|
||||
if differences := compareConfigurationValues(composed.root, reversed.root); len(differences) != 0 {
|
||||
t.Fatalf("composition order changed effective values: %#v", differences)
|
||||
}
|
||||
composed.root.Mapping = append(composed.root.Mapping, document.Entry{Key: "fuzz-detachment"})
|
||||
if !reflect.DeepEqual(left, leftBefore) || !reflect.DeepEqual(right, rightBefore) {
|
||||
t.Fatal("composition result aliases an input document")
|
||||
if mutateExistingDocumentDescendant(composed.root) {
|
||||
if !reflect.DeepEqual(left, leftBefore) || !reflect.DeepEqual(right, rightBefore) {
|
||||
t.Fatal("composition result aliases an input document")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,9 +61,41 @@ func FuzzCompositionAndOverlay(f *testing.F) {
|
||||
if !reflect.DeepEqual(left, leftBefore) || !reflect.DeepEqual(right, rightBefore) {
|
||||
t.Fatal("overlay mutated an input document")
|
||||
}
|
||||
overlaid.root.Mapping = append(overlaid.root.Mapping, document.Entry{Key: "fuzz-detachment"})
|
||||
if !reflect.DeepEqual(left, leftBefore) || !reflect.DeepEqual(right, rightBefore) {
|
||||
t.Fatal("overlay result aliases an input document")
|
||||
if mutateExistingDocumentDescendant(overlaid.root) {
|
||||
if !reflect.DeepEqual(left, leftBefore) || !reflect.DeepEqual(right, rightBefore) {
|
||||
t.Fatal("overlay result aliases an input document")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func mutateExistingDocumentDescendant(node *document.Node) bool {
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
// Empty containers have no existing element for the alias probe.
|
||||
switch node.Kind {
|
||||
case document.Mapping:
|
||||
if len(node.Mapping) == 0 {
|
||||
return false
|
||||
}
|
||||
entry := &node.Mapping[0]
|
||||
if mutateExistingDocumentDescendant(entry.Value) {
|
||||
return true
|
||||
}
|
||||
entry.Key += "-fuzz-detachment"
|
||||
return true
|
||||
case document.Sequence:
|
||||
if len(node.Sequence) == 0 {
|
||||
return false
|
||||
}
|
||||
if mutateExistingDocumentDescendant(node.Sequence[0]) {
|
||||
return true
|
||||
}
|
||||
node.Sequence[0].Text += "fuzz-detachment"
|
||||
return true
|
||||
default:
|
||||
node.Text += "fuzz-detachment"
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,8 +161,21 @@ func TestSelectNamedImportsChecksNormalizedPathsLexically(t *testing.T) {
|
||||
if len(diagnostics) != 0 || len(selection.candidates) != 2 {
|
||||
t.Fatalf("selection, diagnostics = %#v, %#v", selection, diagnostics)
|
||||
}
|
||||
if strings.Join(filesystem.statPaths, ",") != "a.yaml,a.yaml,z.yaml,z.yaml" {
|
||||
t.Fatalf("target stat order = %#v", filesystem.statPaths)
|
||||
if selection.candidates[0].path != "a.yaml" || selection.candidates[1].path != "z.yaml" {
|
||||
t.Fatalf("candidate order = %#v, want a.yaml then z.yaml", selection.candidates)
|
||||
}
|
||||
|
||||
failingFilesystem := &statOnlyImportFilesystem{statErrors: map[string]error{
|
||||
"a.yaml": fs.ErrPermission,
|
||||
"z.yaml": fs.ErrPermission,
|
||||
}}
|
||||
failingAccess := newNativeAccess(failingFilesystem, ".", &namespaceToken{})
|
||||
_, diagnostics = selectNamedImportCandidates(failingAccess, ".", specs[0], resolvedImportPolicy{})
|
||||
if len(diagnostics) != 1 || diagnostics[0].Code != CodeImportInvalid || diagnostics[0].Path.String() != "/configkit/imports/0/path/1" {
|
||||
t.Fatalf("first lexical failure = %#v, want /configkit/imports/0/path/1", diagnostics)
|
||||
}
|
||||
if diagnostics[0].Primary == nil || diagnostics[0].Primary.SourceID != "source-1" {
|
||||
t.Fatalf("first lexical failure location = %#v", diagnostics[0].Primary)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,7 +239,6 @@ type statOnlyImportFilesystem struct {
|
||||
statErrors map[string]error
|
||||
openCalls int
|
||||
statCalls int
|
||||
statPaths []string
|
||||
}
|
||||
|
||||
func (filesystem *statOnlyImportFilesystem) Open(name string) (fs.File, error) {
|
||||
@@ -236,7 +248,6 @@ func (filesystem *statOnlyImportFilesystem) Open(name string) (fs.File, error) {
|
||||
|
||||
func (filesystem *statOnlyImportFilesystem) Stat(name string) (fs.FileInfo, error) {
|
||||
filesystem.statCalls++
|
||||
filesystem.statPaths = append(filesystem.statPaths, name)
|
||||
if err := filesystem.statErrors[name]; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user