mirror of
https://github.com/spf13/viper
synced 2024-12-22 03:27:03 +00:00
Fix MergeInConfig error return
UnsupportedConfigError was returned if config file not found * Swap getConfigFile and getConfigType call * Add a unit test
This commit is contained in:
parent
651d9d916a
commit
5ed0fc31f7
2 changed files with 24 additions and 4 deletions
8
viper.go
8
viper.go
|
@ -1102,15 +1102,15 @@ func (v *Viper) ReadInConfig() error {
|
|||
func MergeInConfig() error { return v.MergeInConfig() }
|
||||
func (v *Viper) MergeInConfig() error {
|
||||
jww.INFO.Println("Attempting to merge in config file")
|
||||
if !stringInSlice(v.getConfigType(), SupportedExts) {
|
||||
return UnsupportedConfigError(v.getConfigType())
|
||||
}
|
||||
|
||||
filename, err := v.getConfigFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !stringInSlice(v.getConfigType(), SupportedExts) {
|
||||
return UnsupportedConfigError(v.getConfigType())
|
||||
}
|
||||
|
||||
file, err := afero.ReadFile(v.fs, filename)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -771,6 +771,26 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
|
|||
assert.Equal(t, `default`, v.GetString(`key`))
|
||||
}
|
||||
|
||||
func TestWrongDirsSearchNotFoundForMerge(t *testing.T) {
|
||||
|
||||
_, config, cleanup := initDirs(t)
|
||||
defer cleanup()
|
||||
|
||||
v := New()
|
||||
v.SetConfigName(config)
|
||||
v.SetDefault(`key`, `default`)
|
||||
|
||||
v.AddConfigPath(`whattayoutalkingbout`)
|
||||
v.AddConfigPath(`thispathaintthere`)
|
||||
|
||||
err := v.MergeInConfig()
|
||||
assert.Equal(t, reflect.TypeOf(ConfigFileNotFoundError{"", ""}), reflect.TypeOf(err))
|
||||
|
||||
// Even though config did not load and the error might have
|
||||
// been ignored by the client, the default still loads
|
||||
assert.Equal(t, `default`, v.GetString(`key`))
|
||||
}
|
||||
|
||||
func TestSub(t *testing.T) {
|
||||
v := New()
|
||||
v.SetConfigType("yaml")
|
||||
|
|
Loading…
Reference in a new issue