From 5ed0fc31f7f453625df314d8e66b9791e8d13003 Mon Sep 17 00:00:00 2001 From: Kevin GEORGES Date: Tue, 13 Dec 2016 10:38:49 +0100 Subject: [PATCH] Fix MergeInConfig error return UnsupportedConfigError was returned if config file not found * Swap getConfigFile and getConfigType call * Add a unit test --- viper.go | 8 ++++---- viper_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/viper.go b/viper.go index 4ed2d40..2603c78 100644 --- a/viper.go +++ b/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 diff --git a/viper_test.go b/viper_test.go index 60e75a3..cd7b65c 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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")