Skip to content

Commit

Permalink
Avoid panic in invalid language config
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 30, 2023
1 parent 9b5e7bf commit 12c7517
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
13 changes: 9 additions & 4 deletions config/allconfig/alldecoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,17 @@ var allDecoderSetups = map[string]decodeWeight{
if len(m) == 1 {
// In v0.112.4 we moved this to the language config, but it's very commmon for mono language sites to have this at the top level.
var first maps.Params
var ok bool
for _, v := range m {
first = v.(maps.Params)
break
first, ok = v.(maps.Params)
if ok {
break
}
}
if _, found := first["languagecode"]; !found {
first["languagecode"] = p.p.GetString("languagecode")
if first != nil {
if _, found := first["languagecode"]; !found {
first["languagecode"] = p.p.GetString("languagecode")
}
}
}
p.c.Languages, err = langs.DecodeConfig(m)
Expand Down
56 changes: 45 additions & 11 deletions hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,25 +1067,59 @@ LanguageCode: {{ .Site.LanguageCode }}|{{ site.Language.LanguageCode }}|

}

// Issue 11047
func TestConfigYamlNil(t *testing.T) {
func TestConfigMiscPanics(t *testing.T) {
t.Parallel()

files := `
// Issue 11047,
t.Run("empty params", func(t *testing.T) {

files := `
-- hugo.yaml --
params:
-- layouts/index.html --
Foo: {{ site.Params.foo }}|
`
b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()

b.AssertFileContent("public/index.html", "Foo: |")
})

// Issue 11046
t.Run("invalid language setup", func(t *testing.T) {

files := `
-- hugo.toml --
baseURL = "https://example.org"
languageCode = "en-us"
title = "Blog of me"
defaultContentLanguage = "en"
[languages]
[en]
lang = "en"
languageName = "English"
weight = 1
-- layouts/index.html --
Foo: {{ site.Params.foo }}|
`
b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()
`
b, err := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).BuildE()

b.AssertFileContent("public/index.html", "Foo: |")
b.Assert(err, qt.IsNotNil)
b.Assert(err.Error(), qt.Contains, "no languages")
})

}
5 changes: 5 additions & 0 deletions langs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package langs

import (
"errors"

"github.com/gohugoio/hugo/common/maps"
"github.com/mitchellh/mapstructure"
)
Expand Down Expand Up @@ -46,5 +48,8 @@ func DecodeConfig(m map[string]any) (map[string]LanguageConfig, error) {
if err := mapstructure.WeakDecode(m, &langs); err != nil {
return nil, err
}
if len(langs) == 0 {
return nil, errors.New("no languages configured")
}
return langs, nil
}

0 comments on commit 12c7517

Please sign in to comment.