Fail on invalid defaultContentLanguage

Fixes #11044
This commit is contained in:
Bjørn Erik Pedersen 2023-05-30 11:53:38 +02:00
parent 6462eecfbd
commit 9cdca1f958
2 changed files with 48 additions and 1 deletions

View file

@ -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
},
},

View file

@ -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")
})
}