config: Fix handling of invalid OS env config overrides

Fixes #8709
This commit is contained in:
Bjørn Erik Pedersen 2021-06-29 09:38:05 +02:00
parent 8290720107
commit 49fedbc51c
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 93 additions and 55 deletions

View file

@ -169,6 +169,9 @@ func (c *defaultConfigProvider) Set(k string, v interface{}) {
} }
key, m := c.getNestedKeyAndMap(k, true) key, m := c.getNestedKeyAndMap(k, true)
if m == nil {
return
}
if existing, found := m[key]; found { if existing, found := m[key]; found {
if p1, ok := existing.(maps.Params); ok { if p1, ok := existing.(maps.Params); ok {
@ -289,6 +292,9 @@ func (c *defaultConfigProvider) Merge(k string, v interface{}) {
} }
key, m := c.getNestedKeyAndMap(k, true) key, m := c.getNestedKeyAndMap(k, true)
if m == nil {
return
}
if existing, found := m[key]; found { if existing, found := m[key]; found {
if p1, ok := existing.(maps.Params); ok { if p1, ok := existing.(maps.Params); ok {
@ -422,7 +428,12 @@ func (c *defaultConfigProvider) getNestedKeyAndMap(key string, create bool) (str
return "", nil return "", nil
} }
} }
current = next.(maps.Params) var ok bool
current, ok = next.(maps.Params)
if !ok {
// E.g. a string, not a map that we can store values in.
return "", nil
}
} }
return parts[len(parts)-1], current return parts[len(parts)-1], current
} }

View file

@ -576,6 +576,7 @@ anchor = "smart"
quality = 75 quality = 75
` `
newB := func(t testing.TB) *sitesBuilder {
b := newTestSitesBuilder(t).WithConfigFile("toml", baseConfig) b := newTestSitesBuilder(t).WithConfigFile("toml", baseConfig)
b.WithSourceFile("themes/mytheme/config.toml", ` b.WithSourceFile("themes/mytheme/config.toml", `
@ -595,6 +596,13 @@ theme_param="themevalue2"
`) `)
return b
}
c.Run("Variations", func(c *qt.C) {
b := newB(c)
b.WithEnviron( b.WithEnviron(
"HUGO_ENVIRONMENT", "test", "HUGO_ENVIRONMENT", "test",
"HUGO_NEW", "new", // key not in config.toml "HUGO_NEW", "new", // key not in config.toml
@ -655,4 +663,23 @@ theme_param="themevalue2"
c.Assert(ofBase.MediaType, qt.Equals, media.TextType) c.Assert(ofBase.MediaType, qt.Equals, media.TextType)
c.Assert(ofTheme.MediaType, qt.Equals, media.TextType) c.Assert(ofTheme.MediaType, qt.Equals, media.TextType)
})
// Issue #8709
c.Run("Set in string", func(c *qt.C) {
b := newB(c)
b.WithEnviron(
// imaging.anchor is a string, and it's not possible
// to set a child attribute.
"HUGO_IMAGING_ANCHOR_FOO", "top",
)
b.Build(BuildCfg{})
cfg := b.H.Cfg
c.Assert(cfg.Get("imaging.anchor"), qt.Equals, "smart")
})
} }