diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-30 11:53:38 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-30 15:59:43 +0200 |
commit | 9cdca1f9582597c3998e44cd16509ead362a90df (patch) | |
tree | a0bd56956bf0b67a5e3f9f6f7ff5125abf1d55ee | |
parent | 6462eecfbd7abc14fa62b33bb079ba424de7d765 (diff) |
Fail on invalid defaultContentLanguage
Fixes #11044
-rw-r--r-- | config/allconfig/alldecoders.go | 21 | ||||
-rw-r--r-- | hugolib/config_test.go | 28 |
2 files changed, 48 insertions, 1 deletions
diff --git a/config/allconfig/alldecoders.go b/config/allconfig/alldecoders.go index b798ef798..b45bd1e11 100644 --- a/config/allconfig/alldecoders.go +++ b/config/allconfig/alldecoders.go @@ -60,7 +60,14 @@ var allDecoderSetups = map[string]decodeWeight{ key: "", weight: -100, // Always first. decode: func(d decodeWeight, p decodeConfig) error { - return mapstructure.WeakDecode(p.p.Get(""), &p.c.RootConfig) + if err := mapstructure.WeakDecode(p.p.Get(""), &p.c.RootConfig); err != nil { + return err + } + + // This need to match with Lang which is always lower case. + p.c.RootConfig.DefaultContentLanguage = strings.ToLower(p.c.RootConfig.DefaultContentLanguage) + + return nil }, }, "imaging": { @@ -263,6 +270,18 @@ var allDecoderSetups = map[string]decodeWeight{ return err } + // Validate defaultContentLanguage. + var found bool + for lang := range p.c.Languages { + if lang == p.c.DefaultContentLanguage { + found = true + break + } + } + if !found { + return fmt.Errorf("config value %q for defaultContentLanguage does not match any language definition", p.c.DefaultContentLanguage) + } + return nil }, }, diff --git a/hugolib/config_test.go b/hugolib/config_test.go index efa2bf6b5..814be667f 100644 --- a/hugolib/config_test.go +++ b/hugolib/config_test.go @@ -1122,4 +1122,32 @@ Foo: {{ site.Params.foo }}| b.Assert(err.Error(), qt.Contains, "no languages") }) + // Issue 11044 + t.Run("invalid defaultContentLanguage", func(t *testing.T) { + + files := ` +-- hugo.toml -- +baseURL = "https://example.org" +defaultContentLanguage = "sv" + +[languages] +[languages.en] +languageCode = "en" +languageName = "English" +weight = 1 + + + + ` + b, err := NewIntegrationTestBuilder( + IntegrationTestConfig{ + T: t, + TxtarString: files, + }, + ).BuildE() + + b.Assert(err, qt.IsNotNil) + b.Assert(err.Error(), qt.Contains, "defaultContentLanguage does not match any language definition") + }) + } |