diff --git a/viper.go b/viper.go index 6330961aa..c08f8667d 100644 --- a/viper.go +++ b/viper.go @@ -1552,6 +1552,7 @@ func (v *Viper) SetRawConfig(config map[string]interface{}) { insensitiviseMap(config) v.config = config v.configChangedAt = time.Now() + v.cache.Clear() } // MergeInConfig merges a new configuration with an existing config. diff --git a/viper_test.go b/viper_test.go index 1202651c4..225bb5253 100644 --- a/viper_test.go +++ b/viper_test.go @@ -2553,9 +2553,24 @@ bar: } func TestSetRawConfig(t *testing.T) { - Reset() - SetRawConfig(map[string]interface{}{"foo": "bar"}) - assert.Equal(t, "bar", Get("foo")) + t.Run("case=setting the config map works", func(t *testing.T) { + v := New() + v.SetRawConfig(map[string]interface{}{"foo": "bar"}) + assert.Equal(t, "bar", v.Get("foo")) + }) + + t.Run("case=resets cache", func(t *testing.T) { + key := "foo" + v := New() + // make sure the value is in the cache + for !v.cache.Set(key, "bar", 0) { + } + for _, ok := v.cache.Get(key); !ok; _, ok = v.cache.Get(key) { + } + + v.SetRawConfig(map[string]interface{}{key: "not bar"}) + assert.Equal(t, "not bar", v.Get(key)) + }) } func BenchmarkGetBool(b *testing.B) {