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()