-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tronje Krop <[email protected]>
- Loading branch information
Tronje Krop
authored
Sep 22, 2024
1 parent
8e48841
commit 9eead42
Showing
13 changed files
with
306 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.0.3 | ||
0.0.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,123 @@ | ||
package config_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/tkrop/go-config/config" | ||
"github.com/tkrop/go-config/internal/filepath" | ||
"github.com/tkrop/go-config/log" | ||
"github.com/tkrop/go-testing/mock" | ||
"github.com/tkrop/go-testing/test" | ||
) | ||
|
||
func TestReadConfig(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Given | ||
reader := config.New("TC", "test", &config.Config{}). | ||
SetDefaults(func(c *config.Reader[config.Config]) { | ||
c.AddConfigPath("fixtures") | ||
}) | ||
var configPaths = []string{filepath.Normalize(".")} | ||
|
||
// When | ||
config := reader.LoadConfig(t.Name()) | ||
|
||
// Than | ||
assert.Equal(t, "prod", config.Env) | ||
assert.Equal(t, log.DefaultLogLevel, config.Log.Level) | ||
type testConfigParam struct { | ||
setenv func(test.Test) | ||
setup func(*config.Reader[config.Config]) | ||
expect mock.SetupFunc | ||
expectEnv string | ||
expectLogLevel string | ||
} | ||
|
||
func TestReadConfig_UnmarshalFailure(t *testing.T) { | ||
t.Parallel() | ||
|
||
type Config struct { | ||
config.Config `mapstructure:",squash"` | ||
Content int | ||
} | ||
|
||
// Given | ||
defer func() { _ = recover() }() | ||
reader := config.New("TC", "test", &Config{}). | ||
SetDefaults(func(c *config.Reader[Config]) { | ||
c.AddConfigPath("fixtures") | ||
}) | ||
|
||
// When | ||
_ = reader.LoadConfig(t.Name()) | ||
|
||
// Then | ||
require.Fail(t, "no panic after unmarschal failure") | ||
var testConfigParams = map[string]testConfigParam{ | ||
"default config without file": { | ||
expectEnv: "prod", | ||
expectLogLevel: "info", | ||
}, | ||
|
||
"default config with file": { | ||
setup: func(r *config.Reader[config.Config]) { | ||
r.AddConfigPath("fixtures") | ||
}, | ||
expectEnv: "prod", | ||
expectLogLevel: "debug", | ||
}, | ||
|
||
"read config with overriding env": { | ||
setenv: func(t test.Test) { | ||
t.Setenv("TC_ENV", "test") | ||
t.Setenv("TC_LOG_LEVEL", "trace") | ||
}, | ||
setup: func(r *config.Reader[config.Config]) { | ||
r.AddConfigPath("fixtures") | ||
}, | ||
expectEnv: "test", | ||
expectLogLevel: "trace", | ||
}, | ||
|
||
"read config with overriding func": { | ||
setup: func(r *config.Reader[config.Config]) { | ||
r.SetDefault("log.level", "trace") | ||
}, | ||
expectEnv: "prod", | ||
expectLogLevel: "trace", | ||
}, | ||
|
||
"panic after file not found": { | ||
setup: func(r *config.Reader[config.Config]) { | ||
r.SetDefault("viper.panic.load", true) | ||
}, | ||
expect: test.Panic(config.NewErrConfig("loading file", "test", | ||
test.Error(viper.ConfigFileNotFoundError{}).Set("name", "test"). | ||
Set("locations", fmt.Sprintf("%s", configPaths)). | ||
Get("").(error))), | ||
}, | ||
|
||
"panic after unmarschal failure": { | ||
setup: func(r *config.Reader[config.Config]) { | ||
r.AddConfigPath("fixtures") | ||
r.SetDefault("viper.panic.unmarshal", true) | ||
r.SetDefault("info.dirty", "5s") | ||
}, | ||
expect: test.Panic(config.NewErrConfig("unmarshal config", | ||
"test", &mapstructure.Error{ | ||
Errors: []string{"cannot parse 'Info.Dirty' as bool: " + | ||
"strconv.ParseBool: parsing \"5s\": invalid syntax"}, | ||
})), | ||
}, | ||
} | ||
|
||
func TestReadConfig_FileNotFound(t *testing.T) { | ||
// Given | ||
defer func() { _ = recover() }() | ||
t.Setenv("TC_ENV", "other") | ||
reader := config.New("TC", "test", &config.Config{}). | ||
SetDefaults(func(c *config.Reader[config.Config]) { | ||
c.AddConfigPath("fixtures") | ||
}) | ||
|
||
// When | ||
_ = reader.LoadConfig(t.Name()) | ||
|
||
// Then | ||
require.Fail(t, "no panic after missing config file") | ||
} | ||
|
||
func TestReadConfig_OverridingEnv(t *testing.T) { | ||
// Given | ||
t.Setenv("TC_LOG_LEVEL", "trace") | ||
reader := config.New("TC", "test", &config.Config{}). | ||
SetDefaults(func(c *config.Reader[config.Config]) { | ||
c.AddConfigPath("fixtures") | ||
func TestConfig(t *testing.T) { | ||
test.Map(t, testConfigParams). | ||
RunSeq(func(t test.Test, param testConfigParam) { | ||
// Given | ||
mock.NewMocks(t).Expect(param.expect) | ||
if param.setenv != nil { | ||
param.setenv(t) | ||
} | ||
reader := config.New("TC", "test", &config.Config{}). | ||
SetDefaults(param.setup) | ||
|
||
// When | ||
reader.LoadConfig("test") | ||
|
||
// Then | ||
assert.Equal(t, param.expectEnv, reader.GetString("env")) | ||
assert.Equal(t, param.expectLogLevel, reader.GetString("log.level")) | ||
}) | ||
|
||
// When | ||
config := reader.LoadConfig(t.Name()) | ||
|
||
// Then | ||
assert.Equal(t, "prod", config.Env) | ||
assert.Equal(t, "trace", config.Log.Level) | ||
} | ||
|
||
func TestReadConfig_OverridingFunc(t *testing.T) { | ||
func TestSetupLogger(t *testing.T) { | ||
t.Parallel() | ||
|
||
// Given | ||
reader := config.New("TC", "test", &config.Config{}). | ||
SetDefaults(func(c *config.Reader[config.Config]) { | ||
c.SetDefault("log.level", "trace") | ||
}) | ||
logger := log.New() | ||
config := config.New("TC", "test", &config.Config{}). | ||
SetDefaults(func(r *config.Reader[config.Config]) { | ||
r.AddConfigPath("fixtures") | ||
r.SetDefault("log.level", "trace") | ||
}). | ||
GetConfig(t.Name()) | ||
|
||
// When | ||
config := reader.GetConfig(t.Name()) | ||
config.SetupLogger(logger) | ||
|
||
// Then | ||
assert.Equal(t, "prod", config.Env) | ||
assert.Equal(t, "trace", config.Log.Level) | ||
assert.Equal(t, log.TraceLevel, logger.GetLevel()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
log: | ||
level: info | ||
level: debug | ||
caller: false | ||
|
||
info: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.