Create a struct with all of Hugo's config options

Primary motivation is documentation, but it will also hopefully simplify the code.

Also,

* Lower case the default output format names; this is in line with the custom ones (map keys) and how
it's treated all the places. This avoids doing `stringds.EqualFold` everywhere.

Closes #10896
Closes #10620
This commit is contained in:
Bjørn Erik Pedersen
2023-01-04 18:24:36 +01:00
parent 6aededf6b4
commit 241b21b0fd
337 changed files with 13377 additions and 14898 deletions

View File

@@ -18,6 +18,7 @@ import (
"testing"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/common/maps"
qt "github.com/frankban/quicktest"
)
@@ -71,13 +72,87 @@ func TestPageMatcher(t *testing.T) {
c.Run("Decode", func(c *qt.C) {
var v PageMatcher
c.Assert(DecodePageMatcher(map[string]any{"kind": "foo"}, &v), qt.Not(qt.IsNil))
c.Assert(DecodePageMatcher(map[string]any{"kind": "{foo,bar}"}, &v), qt.Not(qt.IsNil))
c.Assert(DecodePageMatcher(map[string]any{"kind": "taxonomy"}, &v), qt.IsNil)
c.Assert(DecodePageMatcher(map[string]any{"kind": "{taxonomy,foo}"}, &v), qt.IsNil)
c.Assert(DecodePageMatcher(map[string]any{"kind": "{taxonomy,term}"}, &v), qt.IsNil)
c.Assert(DecodePageMatcher(map[string]any{"kind": "*"}, &v), qt.IsNil)
c.Assert(DecodePageMatcher(map[string]any{"kind": "home", "path": filepath.FromSlash("/a/b/**")}, &v), qt.IsNil)
c.Assert(decodePageMatcher(map[string]any{"kind": "foo"}, &v), qt.Not(qt.IsNil))
c.Assert(decodePageMatcher(map[string]any{"kind": "{foo,bar}"}, &v), qt.Not(qt.IsNil))
c.Assert(decodePageMatcher(map[string]any{"kind": "taxonomy"}, &v), qt.IsNil)
c.Assert(decodePageMatcher(map[string]any{"kind": "{taxonomy,foo}"}, &v), qt.IsNil)
c.Assert(decodePageMatcher(map[string]any{"kind": "{taxonomy,term}"}, &v), qt.IsNil)
c.Assert(decodePageMatcher(map[string]any{"kind": "*"}, &v), qt.IsNil)
c.Assert(decodePageMatcher(map[string]any{"kind": "home", "path": filepath.FromSlash("/a/b/**")}, &v), qt.IsNil)
c.Assert(v, qt.Equals, PageMatcher{Kind: "home", Path: "/a/b/**"})
})
c.Run("mapToPageMatcherParamsConfig", func(c *qt.C) {
fn := func(m map[string]any) PageMatcherParamsConfig {
v, err := mapToPageMatcherParamsConfig(m)
c.Assert(err, qt.IsNil)
return v
}
// Legacy.
c.Assert(fn(map[string]any{"_target": map[string]any{"kind": "page"}, "foo": "bar"}), qt.DeepEquals, PageMatcherParamsConfig{
Params: maps.Params{
"foo": "bar",
},
Target: PageMatcher{Path: "", Kind: "page", Lang: "", Environment: ""},
})
// Current format.
c.Assert(fn(map[string]any{"target": map[string]any{"kind": "page"}, "params": map[string]any{"foo": "bar"}}), qt.DeepEquals, PageMatcherParamsConfig{
Params: maps.Params{
"foo": "bar",
},
Target: PageMatcher{Path: "", Kind: "page", Lang: "", Environment: ""},
})
})
}
func TestDecodeCascadeConfig(t *testing.T) {
c := qt.New(t)
in := []map[string]any{
{
"params": map[string]any{
"a": "av",
},
"target": map[string]any{
"kind": "page",
"Environment": "production",
},
},
{
"params": map[string]any{
"b": "bv",
},
"target": map[string]any{
"kind": "page",
},
},
}
got, err := DecodeCascadeConfig(in)
c.Assert(err, qt.IsNil)
c.Assert(got, qt.IsNotNil)
c.Assert(got.Config, qt.DeepEquals,
map[PageMatcher]maps.Params{
{Path: "", Kind: "page", Lang: "", Environment: ""}: {
"b": "bv",
},
{Path: "", Kind: "page", Lang: "", Environment: "production"}: {
"a": "av",
},
},
)
c.Assert(got.SourceStructure, qt.DeepEquals, []PageMatcherParamsConfig{
{
Params: maps.Params{"a": string("av")},
Target: PageMatcher{Kind: "page", Environment: "production"},
},
{Params: maps.Params{"b": string("bv")}, Target: PageMatcher{Kind: "page"}},
})
got, err = DecodeCascadeConfig(nil)
c.Assert(err, qt.IsNil)
c.Assert(got, qt.IsNotNil)
}