Render focused inspection values safely
This commit is contained in:
@@ -2,7 +2,9 @@ package configkit_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
@@ -61,6 +63,66 @@ func TestInspectionViewScopeAndQueries(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type viewFailWriter struct{}
|
||||
|
||||
func (viewFailWriter) Write([]byte) (int, error) { return 0, errors.New("write failed") }
|
||||
|
||||
func TestInspectionViewWriteEffective(t *testing.T) {
|
||||
options := configkit.Options[map[string]any]{Definition: configkit.Definition[map[string]any]{
|
||||
Sensitive: func(path configkit.Path) bool { return path.String() == "/private" || path.String() == "/nested/token" },
|
||||
Resolve: func(current *map[string]any, resolution *configkit.Resolution) error {
|
||||
delete(*current, "removed")
|
||||
resolution.RecordNormalization(configkit.Keys("removed"), "remove")
|
||||
return nil
|
||||
},
|
||||
}}
|
||||
result, err := configkit.Load(configkit.Bytes("view.yml", []byte("private: {child: canary-private}\nnested: {token: canary-token, name: public}\nitems: [zero, one]\n'0': numeric\n'null': null\nremoved: earlier\n")), options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
path configkit.Path
|
||||
contains string
|
||||
absent string
|
||||
fails bool
|
||||
}{
|
||||
{configkit.Path{}, "public", "canary-token", false},
|
||||
{configkit.Keys("nested"), "public", "canary-token", false},
|
||||
{configkit.Keys("private", "child"), "REDACTED", "canary-private", false},
|
||||
{configkit.Keys("items"), "one", "", false},
|
||||
{configkit.Keys("items").Index(1), "one", "", false},
|
||||
{configkit.Path{}.Index(0), "numeric", "", false},
|
||||
{configkit.Keys("null"), "null", "", false},
|
||||
{configkit.Keys("removed"), "", "", true},
|
||||
{configkit.Keys("missing"), "", "", true},
|
||||
} {
|
||||
view, ok := result.InspectSubtree(tc.path)
|
||||
if !ok {
|
||||
t.Fatal("view unavailable")
|
||||
}
|
||||
var out bytes.Buffer
|
||||
err := view.WriteEffective(&out)
|
||||
if (err != nil) != tc.fails || (tc.fails && out.Len() != 0) || (tc.contains != "" && !strings.Contains(out.String(), tc.contains)) || (tc.absent != "" && strings.Contains(out.String(), tc.absent)) {
|
||||
t.Errorf("%s: %q, %v", tc.path.String(), out.String(), err)
|
||||
}
|
||||
}
|
||||
view, _ := result.InspectSubtree(configkit.Keys("nested"))
|
||||
result.Config["nested"] = "changed"
|
||||
var out bytes.Buffer
|
||||
if err := view.WriteEffective(&out); err != nil || !strings.Contains(out.String(), "public") {
|
||||
t.Fatalf("frozen output = %q, %v", out.String(), err)
|
||||
}
|
||||
if err := view.WriteEffective(viewFailWriter{}); err == nil {
|
||||
t.Fatal("writer error lost")
|
||||
}
|
||||
if err := view.WriteEffective(nil); err == nil {
|
||||
t.Fatal("nil writer accepted")
|
||||
}
|
||||
if err := (configkit.Inspection{}).WriteEffective(&bytes.Buffer{}); err == nil {
|
||||
t.Fatal("zero view accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInspectionViewUnavailable(t *testing.T) {
|
||||
var result *configkit.Result[map[string]any]
|
||||
if _, ok := result.InspectSubtree(configkit.Path{}); ok {
|
||||
|
||||
Reference in New Issue
Block a user