88 lines
2.9 KiB
Go
88 lines
2.9 KiB
Go
package configkit_test
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
|
|
configkit "go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
type inspectionProvider interface {
|
|
InspectSubtree(configkit.Path) (configkit.Inspection, bool)
|
|
}
|
|
|
|
func TestLoadErrorInspectionUsesSharedView(t *testing.T) {
|
|
type settings struct {
|
|
Items []string `yaml:"items"`
|
|
Other string `yaml:"other"`
|
|
}
|
|
input := configkit.Bytes("input.yml", []byte("items: [first, second]\nother: evidence\n"))
|
|
options := configkit.Options[settings]{Definition: configkit.Definition[settings]{
|
|
Validate: func(_ *settings, v *configkit.Validation) { v.Error(configkit.Keys("other"), "rejected") },
|
|
}}
|
|
result, err := configkit.Load(input, options)
|
|
var failure *configkit.LoadError
|
|
if result != nil || !errors.As(err, &failure) || len(failure.Diagnostics()) == 0 {
|
|
t.Fatalf("failed load = %v, %v", result, err)
|
|
}
|
|
var provider inspectionProvider = failure
|
|
root, ok := provider.InspectSubtree(configkit.Path{})
|
|
if !ok || root.Stage() != configkit.InspectionStageResolved {
|
|
t.Fatalf("failed root checkpoint = %q, %v", root.Stage(), ok)
|
|
}
|
|
if !reflect.DeepEqual(root.Sources(), failure.Sources()) {
|
|
t.Fatal("failure and view source catalogs differ")
|
|
}
|
|
key, ok := provider.InspectSubtree(configkit.Keys("items", "0"))
|
|
if !ok {
|
|
t.Fatal("numeric key scope unavailable")
|
|
}
|
|
index, ok := provider.InspectSubtree(configkit.Keys("items").Index(0))
|
|
if !ok || !reflect.DeepEqual(key.HistoricalPaths(), index.HistoricalPaths()) {
|
|
t.Fatal("equivalent numeric scopes disagree")
|
|
}
|
|
if _, ok := key.Explain(configkit.Keys("other")); !ok {
|
|
t.Fatal("outside native query unavailable")
|
|
}
|
|
if _, ok := provider.InspectSubtree(configkit.Keys("absent")); !ok {
|
|
t.Fatal("absent scope rejected")
|
|
}
|
|
if _, ok := provider.InspectSubtree(configkit.Path{}.Index(-1)); ok {
|
|
t.Fatal("invalid scope accepted")
|
|
}
|
|
var out bytes.Buffer
|
|
if err := key.WriteEffective(&out); err != nil || out.String() != "first\n" {
|
|
t.Fatalf("failed view output = %q, %v", out.String(), err)
|
|
}
|
|
success, err := configkit.Load[settings](input)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
provider = success
|
|
successView, ok := provider.InspectSubtree(configkit.Path{})
|
|
if !ok || successView.Stage() != configkit.InspectionStageValidated {
|
|
t.Fatalf("successful checkpoint = %q, %v", successView.Stage(), ok)
|
|
}
|
|
}
|
|
|
|
func TestLoadErrorInspectionUnavailable(t *testing.T) {
|
|
for _, failure := range []*configkit.LoadError{nil, {}} {
|
|
if _, ok := failure.InspectSubtree(configkit.Path{}); ok {
|
|
t.Fatal("unavailable error accepted")
|
|
}
|
|
}
|
|
_, err := configkit.Load[map[string]any](configkit.Bytes("bad.yml", []byte("[\n")))
|
|
var failure *configkit.LoadError
|
|
if !errors.As(err, &failure) {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := failure.InspectSubtree(configkit.Path{}); ok {
|
|
t.Fatal("malformed input produced inspection")
|
|
}
|
|
if (configkit.Inspection{}).Stage() != "" {
|
|
t.Fatal("zero view has checkpoint")
|
|
}
|
|
}
|