Allow absent version headers below structural controls
This commit is contained in:
@@ -170,6 +170,68 @@ func TestFacadeSourceVersionAbsenceKeepsCompleteEvidencePath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFacadeSourceVersionMissingHeaderDoesNotOverlapControl(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
mode configkit.SourceVersionMode
|
||||
control string
|
||||
wantCode configkit.Code
|
||||
}{
|
||||
{"optional omission", configkit.SourceVersionModeIfPresent, "base", ""},
|
||||
{"required omission", configkit.SourceVersionModeRequired, "base", configkit.CodeVersionError},
|
||||
{"invalid control", configkit.SourceVersionModeIfPresent, "true", configkit.CodeInheritanceInvalid},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
main := configkit.Bytes("main", []byte("{}\n"))
|
||||
if test.mode == configkit.SourceVersionModeRequired {
|
||||
main = configkit.NoSource()
|
||||
}
|
||||
checks := 0
|
||||
path := configkit.Keys("entries", "child", "extends", "version")
|
||||
loaded, err := configkit.LoadLayered(configkit.Input{Source: main, Destination: configkit.Keys("main")}, configkit.LayeredOptions[map[string]any]{
|
||||
SourceVersionMode: test.mode,
|
||||
Definition: configkit.Definition[map[string]any]{Version: &configkit.VersionRule{
|
||||
Path: path, Check: func(version configkit.Version, _ *configkit.Validation) error {
|
||||
checks++
|
||||
if version.Present {
|
||||
t.Error("checker received a nonexistent declaration")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}},
|
||||
Underlays: []configkit.Underlay{{Inputs: []configkit.Input{{
|
||||
Source: configkit.Bytes("underlay", []byte("entries:\n base: {value: good}\n child: {extends: "+test.control+"}\n")), Destination: configkit.Keys("under"),
|
||||
}}}},
|
||||
})
|
||||
if checks != 1 {
|
||||
t.Errorf("checker ran %d times, want absent main only", checks)
|
||||
}
|
||||
if test.wantCode != "" {
|
||||
var failure *configkit.LoadError
|
||||
if loaded != nil || !errors.As(err, &failure) {
|
||||
t.Fatal("invalid input did not fail safely")
|
||||
}
|
||||
if diagnostics := failure.Diagnostics(); len(diagnostics) != 1 || diagnostics[0].Code != test.wantCode {
|
||||
t.Fatalf("diagnostics=%#v", diagnostics)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
child := loaded.Config["under"].(map[string]any)["entries"].(map[string]any)["child"].(map[string]any)
|
||||
if child["value"] != "good" {
|
||||
t.Fatal("valid sibling inheritance was not applied")
|
||||
}
|
||||
check := loaded.Sources()[1].VersionCheck
|
||||
if check == nil || check.Version.Present || check.Path.String() != path.String() {
|
||||
t.Fatal("optional omission evidence was not retained")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (reader *sourceVersionModeReader) Read(data []byte) (int, error) {
|
||||
reader.reads++
|
||||
return reader.Reader.Read(data)
|
||||
|
||||
Reference in New Issue
Block a user