Skip to content

Commit

Permalink
Fail on invalid defaultContentLanguage
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 30, 2023
1 parent 12c7517 commit d4b09cc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
21 changes: 20 additions & 1 deletion config/allconfig/alldecoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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
},
},
Expand Down
28 changes: 28 additions & 0 deletions hugolib/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

}

0 comments on commit d4b09cc

Please sign in to comment.