124 lines
4.6 KiB
Go
124 lines
4.6 KiB
Go
|
|
package configkit_test
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"reflect"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
configkit "go.maximumdirect.net/configkit"
|
||
|
|
)
|
||
|
|
|
||
|
|
type valueName string
|
||
|
|
type valueKey string
|
||
|
|
type valueEmbedded struct{ Label valueName }
|
||
|
|
type valueCodec struct{ Text string }
|
||
|
|
|
||
|
|
func (valueCodec) MarshalText() ([]byte, error) { panic("codec called during lookup") }
|
||
|
|
func (*valueCodec) UnmarshalText([]byte) error { panic("codec called during lookup") }
|
||
|
|
|
||
|
|
type valueConfig struct {
|
||
|
|
Embedded *valueEmbedded `yaml:",inline"`
|
||
|
|
Extra map[valueKey]any `yaml:",inline"`
|
||
|
|
List []string `yaml:"list"`
|
||
|
|
Array [2]int `yaml:"array"`
|
||
|
|
Dynamic any `yaml:"dynamic"`
|
||
|
|
Codec valueCodec `yaml:"codec"`
|
||
|
|
Ignored string `yaml:"-"`
|
||
|
|
private string
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestResultValueCurrentNavigation(t *testing.T) {
|
||
|
|
var missing *configkit.Result[valueConfig]
|
||
|
|
if _, ok := missing.Value(configkit.Path{}); ok {
|
||
|
|
t.Fatal("nil result")
|
||
|
|
}
|
||
|
|
var nilMap map[string]int
|
||
|
|
result := &configkit.Result[valueConfig]{Config: valueConfig{
|
||
|
|
Embedded: &valueEmbedded{Label: "named"}, Extra: map[valueKey]any{"a/b": nil, "": nilMap, "0": 7},
|
||
|
|
List: []string{"first"}, Array: [2]int{1, 2}, Dynamic: map[string]any{"list": []any{valueEmbedded{Label: "dynamic"}}},
|
||
|
|
}}
|
||
|
|
parsed, _ := configkit.ParsePath("/dynamic/list/0/label")
|
||
|
|
parsedKey, _ := configkit.ParsePath("/0")
|
||
|
|
if got, ok := result.Value(configkit.Path{}); !ok || !reflect.DeepEqual(got, result.Config) {
|
||
|
|
t.Fatalf("root = %#v, %v", got, ok)
|
||
|
|
}
|
||
|
|
pointer, ok := result.Value(configkit.Keys("dynamic", "list").Index(0))
|
||
|
|
if !ok || pointer.(valueEmbedded).Label != "dynamic" {
|
||
|
|
t.Fatalf("struct terminal = %#v, %v", pointer, ok)
|
||
|
|
}
|
||
|
|
for _, tc := range []struct {
|
||
|
|
path configkit.Path
|
||
|
|
want any
|
||
|
|
ok bool
|
||
|
|
}{
|
||
|
|
{configkit.Keys("label"), valueName("named"), true},
|
||
|
|
{configkit.Keys("a/b"), nil, true}, {configkit.Keys(""), nilMap, true},
|
||
|
|
{parsedKey, 7, true}, {configkit.Path{}.Index(0), nil, false},
|
||
|
|
{configkit.Keys("list").Index(0), "first", true}, {configkit.Keys("list", "0"), nil, false},
|
||
|
|
{configkit.Keys("list").Index(1), nil, false}, {configkit.Keys("list").Index(-1), nil, false},
|
||
|
|
{parsed, valueName("dynamic"), true}, {configkit.Keys("ignored"), nil, false}, {configkit.Keys("private"), nil, false},
|
||
|
|
{configkit.Keys("codec"), valueCodec{}, true}, {configkit.Keys("codec", "text"), nil, false},
|
||
|
|
} {
|
||
|
|
got, ok := result.Value(tc.path)
|
||
|
|
if ok != tc.ok || !reflect.DeepEqual(got, tc.want) {
|
||
|
|
t.Errorf("%s: got %#v, %v; want %#v, %v", tc.path.String(), got, ok, tc.want, tc.ok)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
copyValue, _ := result.Value(configkit.Keys("array"))
|
||
|
|
array := copyValue.([2]int)
|
||
|
|
array[0] = 9
|
||
|
|
listValue, _ := result.Value(configkit.Keys("list"))
|
||
|
|
listValue.([]string)[0] = "changed"
|
||
|
|
mapValue, _ := result.Value(configkit.Keys("dynamic"))
|
||
|
|
mapValue.(map[string]any)["added"] = true
|
||
|
|
if result.Config.Array[0] != 1 || result.Config.List[0] != "changed" {
|
||
|
|
t.Fatal("assignment ownership differs")
|
||
|
|
}
|
||
|
|
if result.Config.Dynamic.(map[string]any)["added"] != true {
|
||
|
|
t.Fatal("map assignment did not preserve alias")
|
||
|
|
}
|
||
|
|
result.Config.Embedded = nil
|
||
|
|
if _, ok := result.Value(configkit.Keys("label")); ok {
|
||
|
|
t.Fatal("nil inline pointer")
|
||
|
|
}
|
||
|
|
result.Config.Dynamic = make(chan int)
|
||
|
|
if _, ok := result.Value(configkit.Keys("dynamic")); !ok {
|
||
|
|
t.Fatal("unsupported terminal unavailable")
|
||
|
|
}
|
||
|
|
if _, ok := result.Value(configkit.Keys("dynamic", "x")); ok {
|
||
|
|
t.Fatal("unsupported value traversed")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestResultValueFrozenInspectionAndSecret(t *testing.T) {
|
||
|
|
t.Setenv("CONFIGKIT_VALUE_TEST_SECRET", "disposable-value-canary")
|
||
|
|
type settings struct {
|
||
|
|
Name string `yaml:"name"`
|
||
|
|
Count int `yaml:"count"`
|
||
|
|
Token configkit.Secret `yaml:"token"`
|
||
|
|
}
|
||
|
|
result, err := configkit.Load[settings](configkit.Bytes("settings.yml", []byte("name: before\ntoken: CONFIGKIT_VALUE_TEST_SECRET\n")))
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if got, ok := result.Value(configkit.Keys("count")); !ok || got != 0 || result.Metadata().IsExplicit(configkit.Keys("count")) {
|
||
|
|
t.Fatalf("default = %#v, %v", got, ok)
|
||
|
|
}
|
||
|
|
secret, ok := result.Value(configkit.Keys("token"))
|
||
|
|
if !ok || secret.(configkit.Secret).Value() != "disposable-value-canary" {
|
||
|
|
t.Fatal("resolved secret unavailable")
|
||
|
|
}
|
||
|
|
result.Config.Name = "after"
|
||
|
|
if got, _ := result.Value(configkit.Keys("name")); got != "after" {
|
||
|
|
t.Fatalf("current value = %v", got)
|
||
|
|
}
|
||
|
|
var output bytes.Buffer
|
||
|
|
if err := result.WriteEffective(&output); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !strings.Contains(output.String(), "before") || strings.Contains(output.String(), "after") || strings.Contains(output.String(), "disposable-value-canary") {
|
||
|
|
t.Fatalf("frozen output = %s", output.String())
|
||
|
|
}
|
||
|
|
}
|