diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-30 11:48:17 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-30 15:59:43 +0200 |
commit | 6462eecfbd7abc14fa62b33bb079ba424de7d765 (patch) | |
tree | dbcff5292acb8920c5d5a9f3bdf356bdf33a7c3a | |
parent | a7d6b1413f7dd7fdda30b12d577b90f4bb0487ff (diff) |
Avoid panic in invalid language config
Fixes #11046
-rw-r--r-- | config/allconfig/alldecoders.go | 13 | ||||
-rw-r--r-- | hugolib/config_test.go | 56 | ||||
-rw-r--r-- | langs/config.go | 5 |
3 files changed, 59 insertions, 15 deletions
diff --git a/config/allconfig/alldecoders.go b/config/allconfig/alldecoders.go index c8944bd2d..b798ef798 100644 --- a/config/allconfig/alldecoders.go +++ b/config/allconfig/alldecoders.go @@ -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) diff --git a/hugolib/config_test.go b/hugolib/config_test.go index edb6b793e..efa2bf6b5 100644 --- a/hugolib/config_test.go +++ b/hugolib/config_test.go @@ -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") + }) } diff --git a/langs/config.go b/langs/config.go index 15c0bedbc..f60ea94a4 100644 --- a/langs/config.go +++ b/langs/config.go @@ -14,6 +14,8 @@ package langs import ( + "errors" + "github.com/gohugoio/hugo/common/maps" "github.com/mitchellh/mapstructure" ) @@ -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 } |