mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-27 22:09:53 +02:00
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:
@@ -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)
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user