86 lines
2.9 KiB
Go
86 lines
2.9 KiB
Go
package configkit_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"reflect"
|
|
"testing"
|
|
|
|
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
)
|
|
|
|
var (
|
|
_ error = (*configkit.LoadError)(nil)
|
|
_ fmt.Formatter = configkit.Secret{}
|
|
_ fmt.Stringer = configkit.Path{}
|
|
_ json.Marshaler = configkit.Secret{}
|
|
_ io.WriterTo = (*configkit.SecretsReport)(nil)
|
|
)
|
|
|
|
type facadeCompatibilityConfig struct {
|
|
Value string `yaml:"value"`
|
|
Derived string `yaml:"derived"`
|
|
}
|
|
|
|
func TestFacadeCompatibility(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const packagePath = "gitea.maximumdirect.net/eric/ConfigKit"
|
|
for _, candidate := range []struct {
|
|
name string
|
|
type_ reflect.Type
|
|
}{
|
|
{"Path", reflect.TypeFor[configkit.Path]()},
|
|
{"Source", reflect.TypeFor[configkit.Source]()},
|
|
{"Secret", reflect.TypeFor[configkit.Secret]()},
|
|
{"Definition", reflect.TypeFor[configkit.Definition[facadeCompatibilityConfig]]()},
|
|
{"Options", reflect.TypeFor[configkit.Options[facadeCompatibilityConfig]]()},
|
|
{"Result", reflect.TypeFor[configkit.Result[facadeCompatibilityConfig]]()},
|
|
} {
|
|
if candidate.type_.PkgPath() != packagePath {
|
|
t.Errorf("%s belongs to %q, want %q", candidate.name, candidate.type_.PkgPath(), packagePath)
|
|
}
|
|
}
|
|
|
|
constructed := configkit.Options[facadeCompatibilityConfig]{
|
|
Definition: configkit.Definition[facadeCompatibilityConfig]{},
|
|
Filesystem: &configkit.FilesystemContext{BaseDir: "configs", Root: "configs"},
|
|
Overrides: []configkit.Override{{
|
|
Path: configkit.Keys("value"),
|
|
Value: "override",
|
|
Flag: "--value",
|
|
}},
|
|
}
|
|
if got := constructed.Overrides[0].Path.String(); got != "/value" {
|
|
t.Fatalf("constructed override path = %q, want /value", got)
|
|
}
|
|
publicResult := configkit.Result[facadeCompatibilityConfig]{Config: facadeCompatibilityConfig{Value: "constructed"}}
|
|
if publicResult.Config.Value != "constructed" {
|
|
t.Fatal("constructed result lost its public configuration")
|
|
}
|
|
|
|
result, err := configkit.Load(configkit.Bytes("facade.yml", []byte("value: input\n")), configkit.Options[facadeCompatibilityConfig]{
|
|
Definition: configkit.Definition[facadeCompatibilityConfig]{
|
|
Resolve: func(configuration *facadeCompatibilityConfig, resolution *configkit.Resolution) error {
|
|
configkit.Default(resolution, configkit.Keys("derived"), &configuration.Derived, configuration.Value+" default", "derive value")
|
|
configkit.Normalize(resolution, configkit.Keys("value"), &configuration.Value, "normalized "+configuration.Value, "normalize value")
|
|
return nil
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("Load() error = %v", err)
|
|
}
|
|
if result.Config != (facadeCompatibilityConfig{Value: "normalized input", Derived: "input default"}) {
|
|
t.Fatalf("Load() Config = %#v", result.Config)
|
|
}
|
|
|
|
_, err = configkit.Load[facadeCompatibilityConfig](configkit.Bytes("invalid.yml", []byte("value: [\n")))
|
|
var loadError *configkit.LoadError
|
|
if !errors.As(err, &loadError) {
|
|
t.Fatalf("Load() error = %T, want *configkit.LoadError", err)
|
|
}
|
|
}
|