Skip to content

Commit

Permalink
Fix MergeInConfig error return
Browse files Browse the repository at this point in the history
UnsupportedConfigError was returned if config file not found

* Swap getConfigFile and getConfigType call
* Add a unit test
  • Loading branch information
d33d33 authored and bep committed Dec 13, 2016
1 parent 651d9d9 commit 5ed0fc3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 5ed0fc3

Please sign in to comment.