Reset cache on config name change

I stumbled over this when trying to merge multiple configs.

```
viper.SetConfigName("default")
err := viper.MergeInConfig()
```
which caches file path resolvemenet in `v.configFile`

and then
```
viper.SetConfigName("prod")
err := viper.MergeInConfig()
```

which reuses `v.configFile` without updating it accordingly to the new name.

See c1ccc378a0/viper.go (L1240)
This commit is contained in:
Roland Schilter 2016-08-05 09:18:19 +02:00 committed by Bjørn Erik Pedersen
parent 64dc6f6810
commit 5619c0edbe
2 changed files with 7 additions and 0 deletions

View file

@ -1214,6 +1214,7 @@ func SetConfigName(in string) { v.SetConfigName(in) }
func (v *Viper) SetConfigName(in string) {
if in != "" {
v.configName = in
v.configFile = ""
}
}

View file

@ -901,3 +901,9 @@ func TestUnmarshalingWithAliases(t *testing.T) {
assert.Equal(t, &C, &config{Id: 1, FirstName: "Steve", Surname: "Owen"})
}
func TestSetConfigNameClearsFileCache(t *testing.T) {
SetConfigFile("/tmp/config.yaml")
SetConfigName("default")
assert.Empty(t, v.getConfigFile())
}