Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Feb 5, 2024
1 parent 9404f67 commit 639ce68
Show file tree
Hide file tree
Showing 16 changed files with 667 additions and 160 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ issues:
- govet
- path: _test\.go
linters:
- cyclop
- forcetypeassert
- funlen
- goconst
- goerr113
- wrapcheck
11 changes: 6 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,15 @@ func sub(values map[string]any, keys []string) any {
// If there are sensitive information (e.g. password, secret) which should not be exposed,
// you can use [WithValueFormatter] to pass a value formatter to blur the information.
func (c *Config) Explain(path string, opts ...ExplainOption) string {
option := &explainOptions{
valueFormatter: func(path string, loader Loader, value any) string {
return fmt.Sprint(value)
},
}
option := &explainOptions{}
for _, opt := range opts {
opt(option)
}
if option.valueFormatter == nil {
option.valueFormatter = func(path string, loader Loader, value any) string {
return fmt.Sprint(value)
}
}

explanation := &strings.Builder{}
c.explain(explanation, path, sub(c.values, strings.Split(path, c.delimiter)), *option)
Expand Down
61 changes: 60 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@ package konf_test

import (
"testing"
"time"

"github.com/go-viper/mapstructure/v2"

"github.com/nil-go/konf"
"github.com/nil-go/konf/internal/assert"
"github.com/nil-go/konf/provider/env"
)

func TestConfig_Load_panic(t *testing.T) {
t.Parallel()

defer func() {
if r := recover(); r != nil {
assert.Equal(t, r.(string), "cannot load config from nil loader")
}
}()
_ = konf.New().Load(nil)
}

func TestConfig_Unmarshal(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -81,6 +95,44 @@ func TestConfig_Unmarshal(t *testing.T) {
assert.Equal(t, "string", value)
},
},
{
description: "customized decode hook",
opts: []konf.Option{
konf.WithDecodeHook(mapstructure.StringToTimeDurationHookFunc()),
},
loaders: []konf.Loader{
mapLoader{
"config": map[string]any{
"nest": "1s",
},
},
},
assert: func(config *konf.Config) {
var value time.Duration
assert.NoError(t, config.Unmarshal("config.nest", &value))
assert.Equal(t, time.Second, value)
},
},
{
description: "customized tag name",
opts: []konf.Option{
konf.WithTagName("test"),
},
loaders: []konf.Loader{
mapLoader{
"config": map[string]any{
"nest": "string",
},
},
},
assert: func(config *konf.Config) {
var value struct {
N string `test:"nest"`
}
assert.NoError(t, config.Unmarshal("config", &value))
assert.Equal(t, "string", value.N)
},
},
{
description: "non string key",
loaders: []konf.Loader{
Expand Down Expand Up @@ -132,7 +184,14 @@ func TestConfig_Explain(t *testing.T) {
assert.NoError(t, err)

assert.Equal(t, "non-exist has no configuration.\n\n", config.Explain("non-exist"))
assert.Equal(t, "owner has value[map] that is loaded by loader[map].\n\n", config.Explain("owner"))
assert.Equal(t,
"owner has value[map] that is loaded by loader[map].\n\n",
config.Explain("owner", konf.WithValueFormatter(
func(_ string, _ konf.Loader, value any) string {
return value.(string)
},
)),
)
expected := `config.nest has value[map] that is loaded by loader[map].
Here are other value(loader)s:
- env(env)
Expand Down
14 changes: 5 additions & 9 deletions default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package konf_test

import (
"bytes"
"log"
"testing"

"github.com/nil-go/konf"
Expand Down Expand Up @@ -37,18 +36,15 @@ func TestGet(t *testing.T) {
}

func TestGet_error(t *testing.T) {
config := konf.New()
buf := new(bytes.Buffer)
config := konf.New(konf.WithLogHandler(logHandler(buf)))
err := config.Load(mapLoader{"config": "string"})
assert.NoError(t, err)
konf.SetDefault(config)

buf := new(bytes.Buffer)
log.SetOutput(buf)
log.SetFlags(0)

assert.True(t, !konf.Get[bool]("config"))
expected := "WARN Could not read config, return empty value instead." +
" error=\"decode: cannot parse '' as bool: strconv.ParseBool: parsing \\\"string\\\": invalid syntax\"" +
" path=config type=bool\n"
expected := `level=WARN msg="Could not read config, return empty value instead."` +
` error="decode: cannot parse '' as bool: strconv.ParseBool: parsing \"string\": invalid syntax"` +
` path=config type=bool` + "\n"
assert.Equal(t, expected, buf.String())
}
15 changes: 1 addition & 14 deletions provider/appconfig/appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type AppConfig struct {
logger *slog.Logger
unmarshal func([]byte, any) error

client appConfigClient
client *appconfigdata.Client
application string
environment string
profile string
Expand All @@ -39,19 +39,6 @@ type AppConfig struct {
token atomic.Pointer[string]
}

type appConfigClient interface {
StartConfigurationSession(
ctx context.Context,
params *appconfigdata.StartConfigurationSessionInput,
optFns ...func(*appconfigdata.Options),
) (*appconfigdata.StartConfigurationSessionOutput, error)
GetLatestConfiguration(
ctx context.Context,
params *appconfigdata.GetLatestConfigurationInput,
optFns ...func(*appconfigdata.Options),
) (*appconfigdata.GetLatestConfigurationOutput, error)
}

// New creates an AppConfig with the given application, environment, profile and Option(s).
func New(application, environment, profile string, opts ...Option) *AppConfig {
if application == "" {
Expand Down
Loading

0 comments on commit 639ce68

Please sign in to comment.