Fix ConfigFileUsed to return config in search path

Returns the config file found in search paths as well instead of just
the configFile property.
This commit is contained in:
Warren Fernandes 2020-04-22 16:29:59 -06:00
parent c42a305a4b
commit cabc2530d4
2 changed files with 24 additions and 2 deletions

View file

@ -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.

View file

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