mirror of
https://github.com/spf13/viper
synced 2025-05-05 19:57:18 +00:00
Add test case that demonstrates the fixed behavior
This commit is contained in:
parent
7d76daf975
commit
a53d9a5d71
1 changed files with 47 additions and 0 deletions
|
@ -49,6 +49,24 @@ eyes : brown
|
||||||
beard: true
|
beard: true
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
var yamlExampleWithURLKey = []byte(`Hacker: true
|
||||||
|
name: steve
|
||||||
|
hobbies:
|
||||||
|
- skateboarding
|
||||||
|
- snowboarding
|
||||||
|
- go
|
||||||
|
clothing:
|
||||||
|
jacket: leather
|
||||||
|
trousers: denim
|
||||||
|
pants:
|
||||||
|
size: large
|
||||||
|
http://example.com:
|
||||||
|
review
|
||||||
|
age: 35
|
||||||
|
eyes : brown
|
||||||
|
beard: true
|
||||||
|
`)
|
||||||
|
|
||||||
var yamlExampleWithExtras = []byte(`Existing: true
|
var yamlExampleWithExtras = []byte(`Existing: true
|
||||||
Bogus: true
|
Bogus: true
|
||||||
`)
|
`)
|
||||||
|
@ -154,6 +172,10 @@ func initYAML(v *Viper) {
|
||||||
initConfig(v, "yaml", string(yamlExample))
|
initConfig(v, "yaml", string(yamlExample))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initYAMLWithURLKey(v *Viper) {
|
||||||
|
initConfig(v, "yaml", string(yamlExampleWithURLKey))
|
||||||
|
}
|
||||||
|
|
||||||
func initJSON(v *Viper) {
|
func initJSON(v *Viper) {
|
||||||
v.SetConfigType("json")
|
v.SetConfigType("json")
|
||||||
r := bytes.NewReader(jsonExample)
|
r := bytes.NewReader(jsonExample)
|
||||||
|
@ -1894,3 +1916,28 @@ func TestIsKnown(t *testing.T) {
|
||||||
|
|
||||||
assert.False(t, v.IsKnown("unknown"))
|
assert.False(t, v.IsKnown("unknown"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func copyMap(m map[string]interface{}) map[string]interface{} {
|
||||||
|
res := make(map[string]interface{})
|
||||||
|
for k, v := range m {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAssignToMapThenGet(t *testing.T) {
|
||||||
|
v := New()
|
||||||
|
initYAMLWithURLKey(v)
|
||||||
|
|
||||||
|
// NOTE: copyMap is necessary here for the test to demonstrate the actual issue
|
||||||
|
// Without copying this map, this block of code will modify the v.defaults layer
|
||||||
|
// which hides the bug that v.Get should return the v.override value
|
||||||
|
clothingElem := copyMap(v.Get("clothing").(map[string]interface{}))
|
||||||
|
clothingElem["http://example.com"] = "recommendation"
|
||||||
|
v.Set("clothing", clothingElem)
|
||||||
|
|
||||||
|
// demonstart that v.Get returns the overriden value, despite having a url in the
|
||||||
|
// key path, and the key not being set directly, but rather being set as a map key
|
||||||
|
expected := `recommendation`
|
||||||
|
require.Equal(t, expected, v.Get("clothing.http://example.com"))
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue