Skip to content
This repository was archived by the owner on Dec 15, 2020. It is now read-only.

Commit 2ae50bd

Browse files
authored
fix: SetRawConfig resets cache (#17)
1 parent 443a9dd commit 2ae50bd

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

viper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,7 @@ func (v *Viper) SetRawConfig(config map[string]interface{}) {
15521552
insensitiviseMap(config)
15531553
v.config = config
15541554
v.configChangedAt = time.Now()
1555+
v.cache.Clear()
15551556
}
15561557

15571558
// MergeInConfig merges a new configuration with an existing config.

viper_test.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,9 +2553,24 @@ bar:
25532553
}
25542554

25552555
func TestSetRawConfig(t *testing.T) {
2556-
Reset()
2557-
SetRawConfig(map[string]interface{}{"foo": "bar"})
2558-
assert.Equal(t, "bar", Get("foo"))
2556+
t.Run("case=setting the config map works", func(t *testing.T) {
2557+
v := New()
2558+
v.SetRawConfig(map[string]interface{}{"foo": "bar"})
2559+
assert.Equal(t, "bar", v.Get("foo"))
2560+
})
2561+
2562+
t.Run("case=resets cache", func(t *testing.T) {
2563+
key := "foo"
2564+
v := New()
2565+
// make sure the value is in the cache
2566+
for !v.cache.Set(key, "bar", 0) {
2567+
}
2568+
for _, ok := v.cache.Get(key); !ok; _, ok = v.cache.Get(key) {
2569+
}
2570+
2571+
v.SetRawConfig(map[string]interface{}{key: "not bar"})
2572+
assert.Equal(t, "not bar", v.Get(key))
2573+
})
25592574
}
25602575

25612576
func BenchmarkGetBool(b *testing.B) {

0 commit comments

Comments
 (0)