mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
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:
parent
64dc6f6810
commit
5619c0edbe
2 changed files with 7 additions and 0 deletions
1
viper.go
1
viper.go
|
@ -1214,6 +1214,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 = ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -901,3 +901,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())
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue