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:
Kevin GEORGES 2016-12-13 10:38:49 +01:00 committed by Bjørn Erik Pedersen
parent 651d9d916a
commit 5ed0fc31f7
2 changed files with 24 additions and 4 deletions

View file

@ -1102,15 +1102,15 @@ func (v *Viper) ReadInConfig() error {
func MergeInConfig() error { return v.MergeInConfig() } func MergeInConfig() error { return v.MergeInConfig() }
func (v *Viper) MergeInConfig() error { func (v *Viper) MergeInConfig() error {
jww.INFO.Println("Attempting to merge in config file") jww.INFO.Println("Attempting to merge in config file")
if !stringInSlice(v.getConfigType(), SupportedExts) {
return UnsupportedConfigError(v.getConfigType())
}
filename, err := v.getConfigFile() filename, err := v.getConfigFile()
if err != nil { if err != nil {
return err return err
} }
if !stringInSlice(v.getConfigType(), SupportedExts) {
return UnsupportedConfigError(v.getConfigType())
}
file, err := afero.ReadFile(v.fs, filename) file, err := afero.ReadFile(v.fs, filename)
if err != nil { if err != nil {
return err return err

View file

@ -771,6 +771,26 @@ func TestWrongDirsSearchNotFound(t *testing.T) {
assert.Equal(t, `default`, v.GetString(`key`)) 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) { func TestSub(t *testing.T) {
v := New() v := New()
v.SetConfigType("yaml") v.SetConfigType("yaml")