From cabc2530d4c9733583e2ac52933b39b50f2da1ed Mon Sep 17 00:00:00 2001 From: Warren Fernandes Date: Wed, 22 Apr 2020 16:29:59 -0600 Subject: [PATCH] Fix ConfigFileUsed to return config in search path Returns the config file found in search paths as well instead of just the configFile property. --- viper.go | 7 +++++-- viper_test.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/viper.go b/viper.go index 7b12b36..617cee6 100644 --- a/viper.go +++ b/viper.go @@ -459,8 +459,11 @@ func (v *Viper) getEnv(key string) (string, bool) { } // ConfigFileUsed returns the file used to populate the config registry. -func ConfigFileUsed() string { return v.ConfigFileUsed() } -func (v *Viper) ConfigFileUsed() string { return v.configFile } +func ConfigFileUsed() string { return v.ConfigFileUsed() } +func (v *Viper) ConfigFileUsed() string { + f, _ := v.getConfigFile() + return f +} // AddConfigPath adds a path for Viper to search for the config file in. // Can be called multiple times to define multiple search paths. diff --git a/viper_test.go b/viper_test.go index b8ceccb..fa1be9c 100644 --- a/viper_test.go +++ b/viper_test.go @@ -300,6 +300,25 @@ func (s *stringValue) String() string { return string(*s) } +func TestConfigFileUsed(t *testing.T) { + fs := afero.NewMemMapFs() + + err := fs.Mkdir("/tmp/config", 0777) + require.NoError(t, err) + + _, err = fs.Create("/tmp/config/myconfig.yaml") + require.NoError(t, err) + + v := New() + + v.SetFs(fs) + v.SetConfigName("myconfig") + v.AddConfigPath("/tmp/config") + + filename := v.ConfigFileUsed() + assert.Equal(t, "/tmp/config/myconfig.yaml", filename) +} + func TestBasics(t *testing.T) { SetConfigFile("/tmp/config.yaml") filename, err := v.getConfigFile()