mirror of
https://github.com/spf13/viper
synced 2025-05-06 12:17:18 +00:00
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:
parent
c1ccc378a0
commit
3baf1aeb9e
2 changed files with 7 additions and 0 deletions
1
viper.go
1
viper.go
|
@ -1208,6 +1208,7 @@ func SetConfigName(in string) { v.SetConfigName(in) }
|
|||
func (v *Viper) SetConfigName(in string) {
|
||||
if in != "" {
|
||||
v.configName = in
|
||||
v.configFile = ""
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -883,3 +883,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())
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue