Changing config name should also reset file name cache

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-06-08 16:05:26 +02:00
parent c1ccc378a0
commit 3baf1aeb9e
2 changed files with 7 additions and 0 deletions

View file

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

View file

@ -883,3 +883,9 @@ func TestUnmarshalingWithAliases(t *testing.T) {
assert.Equal(t, &C, &config{Id: 1, FirstName: "Steve", Surname: "Owen"}) 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())
}