diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-01-04 18:24:36 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-05-16 18:01:29 +0200 |
commit | 241b21b0fd34d91fccb2ce69874110dceae6f926 (patch) | |
tree | d4e0118eac7e9c42f065815447a70805f8d6ad3e /tpl/collections | |
parent | 6aededf6b42011c3039f5f66487a89a8dd65e0e7 (diff) |
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
Diffstat (limited to 'tpl/collections')
-rw-r--r-- | tpl/collections/append_test.go | 6 | ||||
-rw-r--r-- | tpl/collections/apply_test.go | 13 | ||||
-rw-r--r-- | tpl/collections/collections.go | 6 | ||||
-rw-r--r-- | tpl/collections/collections_test.go | 71 | ||||
-rw-r--r-- | tpl/collections/complement_test.go | 6 | ||||
-rw-r--r-- | tpl/collections/index.go | 2 | ||||
-rw-r--r-- | tpl/collections/index_test.go | 5 | ||||
-rw-r--r-- | tpl/collections/merge_test.go | 7 | ||||
-rw-r--r-- | tpl/collections/sort.go | 6 | ||||
-rw-r--r-- | tpl/collections/sort_test.go | 8 | ||||
-rw-r--r-- | tpl/collections/symdiff_test.go | 6 | ||||
-rw-r--r-- | tpl/collections/where.go | 4 | ||||
-rw-r--r-- | tpl/collections/where_test.go | 8 |
13 files changed, 48 insertions, 100 deletions
diff --git a/tpl/collections/append_test.go b/tpl/collections/append_test.go index 232781522..78cdcdd84 100644 --- a/tpl/collections/append_test.go +++ b/tpl/collections/append_test.go @@ -18,17 +18,13 @@ import ( "testing" qt "github.com/frankban/quicktest" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/deps" - "github.com/gohugoio/hugo/langs" ) // Also see tests in common/collection. func TestAppend(t *testing.T) { t.Parallel() c := qt.New(t) - - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { start any diff --git a/tpl/collections/apply_test.go b/tpl/collections/apply_test.go index 2c7783fd9..aa39923b7 100644 --- a/tpl/collections/apply_test.go +++ b/tpl/collections/apply_test.go @@ -21,10 +21,9 @@ import ( "testing" qt "github.com/frankban/quicktest" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/deps" - "github.com/gohugoio/hugo/langs" + "github.com/gohugoio/hugo/config/testconfig" "github.com/gohugoio/hugo/output" + "github.com/gohugoio/hugo/output/layouts" "github.com/gohugoio/hugo/tpl" ) @@ -46,7 +45,7 @@ func (templateFinder) LookupVariants(name string) []tpl.Template { return nil } -func (templateFinder) LookupLayout(d output.LayoutDescriptor, f output.Format) (tpl.Template, bool, error) { +func (templateFinder) LookupLayout(d layouts.LayoutDescriptor, f output.Format) (tpl.Template, bool, error) { return nil, false, nil } @@ -69,8 +68,10 @@ func (templateFinder) GetFunc(name string) (reflect.Value, bool) { func TestApply(t *testing.T) { t.Parallel() c := qt.New(t) - d := &deps.Deps{Language: langs.NewDefaultLanguage(config.New())} - d.SetTmpl(new(templateFinder)) + d := testconfig.GetTestDeps(nil, nil) + d.SetTempl(&tpl.TemplateHandlers{ + Tmpl: new(templateFinder), + }) ns := New(d) strings := []any{"a\n", "b\n"} diff --git a/tpl/collections/collections.go b/tpl/collections/collections.go index 994d5f1b4..35a87394a 100644 --- a/tpl/collections/collections.go +++ b/tpl/collections/collections.go @@ -43,11 +43,11 @@ func init() { // New returns a new instance of the collections-namespaced template functions. func New(deps *deps.Deps) *Namespace { - if deps.Language == nil { + language := deps.Conf.Language() + if language == nil { panic("language must be set") } - - loc := langs.GetLocation(deps.Language) + loc := langs.GetLocation(language) return &Namespace{ loc: loc, diff --git a/tpl/collections/collections_test.go b/tpl/collections/collections_test.go index fd78da6d4..86192c480 100644 --- a/tpl/collections/collections_test.go +++ b/tpl/collections/collections_test.go @@ -23,15 +23,9 @@ import ( "time" "github.com/gohugoio/hugo/common/maps" + "github.com/gohugoio/hugo/config/testconfig" qt "github.com/frankban/quicktest" - "github.com/gohugoio/hugo/common/loggers" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/deps" - "github.com/gohugoio/hugo/helpers" - "github.com/gohugoio/hugo/hugofs" - "github.com/gohugoio/hugo/langs" - "github.com/spf13/afero" ) type tstNoStringer struct{} @@ -40,7 +34,7 @@ func TestAfter(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { index any @@ -97,7 +91,7 @@ func (g *tstGrouper2) Group(key any, items any) (any, error) { func TestGroup(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { key any @@ -133,9 +127,7 @@ func TestDelimit(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{ - Language: langs.NewDefaultLanguage(config.New()), - }) + ns := newNs() for i, test := range []struct { seq any @@ -187,7 +179,7 @@ func TestDelimit(t *testing.T) { func TestDictionary(t *testing.T) { c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { values []any @@ -226,7 +218,7 @@ func TestDictionary(t *testing.T) { func TestReverse(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() s := []string{"a", "b", "c"} reversed, err := ns.Reverse(s) @@ -245,7 +237,7 @@ func TestEchoParam(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { a any @@ -277,7 +269,7 @@ func TestFirst(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { limit any @@ -314,8 +306,7 @@ func TestFirst(t *testing.T) { func TestIn(t *testing.T) { t.Parallel() c := qt.New(t) - - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { l1 any @@ -391,7 +382,7 @@ func TestIntersect(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { l1, l2 any @@ -481,7 +472,7 @@ func TestIntersect(t *testing.T) { func TestIsSet(t *testing.T) { t.Parallel() c := qt.New(t) - ns := newTestNs() + ns := newNs() for i, test := range []struct { a any @@ -518,7 +509,7 @@ func TestLast(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { limit any @@ -557,7 +548,7 @@ func TestLast(t *testing.T) { func TestQuerify(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { params []any @@ -591,7 +582,7 @@ func TestQuerify(t *testing.T) { } func BenchmarkQuerify(b *testing.B) { - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() params := []any{"a", "b", "c", "d", "f", " &"} b.ResetTimer() @@ -604,7 +595,7 @@ func BenchmarkQuerify(b *testing.B) { } func BenchmarkQuerifySlice(b *testing.B) { - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() params := []string{"a", "b", "c", "d", "f", " &"} b.ResetTimer() @@ -619,7 +610,7 @@ func BenchmarkQuerifySlice(b *testing.B) { func TestSeq(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { args []any @@ -663,7 +654,7 @@ func TestSeq(t *testing.T) { func TestShuffle(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { seq any @@ -703,7 +694,7 @@ func TestShuffle(t *testing.T) { func TestShuffleRandomising(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() // Note that this test can fail with false negative result if the shuffle // of the sequence happens to be the same as the original sequence. However @@ -734,7 +725,7 @@ func TestShuffleRandomising(t *testing.T) { func TestSlice(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { args []any @@ -758,7 +749,7 @@ func TestUnion(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { l1 any @@ -847,7 +838,7 @@ func TestUnion(t *testing.T) { func TestUniq(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() for i, test := range []struct { l any expect any @@ -971,22 +962,6 @@ func ToTstXIs(slice any) []TstXI { return tis } -func newDeps(cfg config.Provider) *deps.Deps { - l := langs.NewLanguage("en", cfg) - l.Set("i18nDir", "i18n") - cs, err := helpers.NewContentSpec(l, loggers.NewErrorLogger(), afero.NewMemMapFs(), nil) - if err != nil { - panic(err) - } - return &deps.Deps{ - Language: l, - Cfg: cfg, - Fs: hugofs.NewMem(l), - ContentSpec: cs, - Log: loggers.NewErrorLogger(), - } -} - -func newTestNs() *Namespace { - return New(newDeps(config.NewWithTestDefaults())) +func newNs() *Namespace { + return New(testconfig.GetTestDeps(nil, nil)) } diff --git a/tpl/collections/complement_test.go b/tpl/collections/complement_test.go index 6c13ab5c4..761a2451c 100644 --- a/tpl/collections/complement_test.go +++ b/tpl/collections/complement_test.go @@ -17,10 +17,6 @@ import ( "reflect" "testing" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/deps" - "github.com/gohugoio/hugo/langs" - qt "github.com/frankban/quicktest" ) @@ -36,7 +32,7 @@ func TestComplement(t *testing.T) { c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() s1 := []TstX{{A: "a"}, {A: "b"}, {A: "d"}, {A: "e"}} s2 := []TstX{{A: "b"}, {A: "e"}} diff --git a/tpl/collections/index.go b/tpl/collections/index.go index e4362fdc3..df932f7c6 100644 --- a/tpl/collections/index.go +++ b/tpl/collections/index.go @@ -64,7 +64,7 @@ func (ns *Namespace) doIndex(item any, args ...any) (any, error) { lowerm, ok := item.(maps.Params) if ok { - return lowerm.Get(cast.ToStringSlice(indices)...), nil + return lowerm.GetNested(cast.ToStringSlice(indices)...), nil } for _, i := range indices { diff --git a/tpl/collections/index_test.go b/tpl/collections/index_test.go index 7c917c443..0c5a58756 100644 --- a/tpl/collections/index_test.go +++ b/tpl/collections/index_test.go @@ -18,17 +18,14 @@ import ( "testing" "github.com/gohugoio/hugo/common/maps" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/langs" qt "github.com/frankban/quicktest" - "github.com/gohugoio/hugo/deps" ) func TestIndex(t *testing.T) { t.Parallel() c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() var ( emptyInterface any diff --git a/tpl/collections/merge_test.go b/tpl/collections/merge_test.go index 4dbc30741..7809152d4 100644 --- a/tpl/collections/merge_test.go +++ b/tpl/collections/merge_test.go @@ -19,9 +19,6 @@ import ( "testing" "github.com/gohugoio/hugo/common/maps" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/deps" - "github.com/gohugoio/hugo/langs" "github.com/gohugoio/hugo/parser" "github.com/gohugoio/hugo/parser/metadecoders" @@ -29,7 +26,7 @@ import ( ) func TestMerge(t *testing.T) { - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() simpleMap := map[string]any{"a": 1, "b": 2} @@ -164,7 +161,7 @@ func TestMerge(t *testing.T) { func TestMergeDataFormats(t *testing.T) { c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() toml1 := ` V1 = "v1_1" diff --git a/tpl/collections/sort.go b/tpl/collections/sort.go index 9a1928b00..83029b310 100644 --- a/tpl/collections/sort.go +++ b/tpl/collections/sort.go @@ -46,7 +46,7 @@ func (ns *Namespace) Sort(l any, args ...any) (any, error) { return nil, errors.New("can't sort " + reflect.ValueOf(l).Type().String()) } - collator := langs.GetCollator(ns.deps.Language) + collator := langs.GetCollator(ns.deps.Conf.Language()) // Create a list of pairs that will be used to do the sort p := pairList{Collator: collator, sortComp: ns.sortComp, SortAsc: true, SliceType: sliceType} @@ -87,7 +87,7 @@ func (ns *Namespace) Sort(l any, args ...any) (any, error) { } // Special handling of lower cased maps. if params, ok := v.Interface().(maps.Params); ok { - v = reflect.ValueOf(params.Get(path[i+1:]...)) + v = reflect.ValueOf(params.GetNested(path[i+1:]...)) break } } @@ -117,7 +117,7 @@ func (ns *Namespace) Sort(l any, args ...any) (any, error) { } // Special handling of lower cased maps. if params, ok := v.Interface().(maps.Params); ok { - v = reflect.ValueOf(params.Get(path[i+1:]...)) + v = reflect.ValueOf(params.GetNested(path[i+1:]...)) break } } diff --git a/tpl/collections/sort_test.go b/tpl/collections/sort_test.go index a4adccf51..da9c75d04 100644 --- a/tpl/collections/sort_test.go +++ b/tpl/collections/sort_test.go @@ -19,10 +19,6 @@ import ( "testing" "github.com/gohugoio/hugo/common/maps" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/langs" - - "github.com/gohugoio/hugo/deps" ) type stringsSlice []string @@ -30,9 +26,7 @@ type stringsSlice []string func TestSort(t *testing.T) { t.Parallel() - ns := New(&deps.Deps{ - Language: langs.NewDefaultLanguage(config.New()), - }) + ns := newNs() type ts struct { MyInt int diff --git a/tpl/collections/symdiff_test.go b/tpl/collections/symdiff_test.go index e5494d5a0..548f91b6c 100644 --- a/tpl/collections/symdiff_test.go +++ b/tpl/collections/symdiff_test.go @@ -17,10 +17,6 @@ import ( "reflect" "testing" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/deps" - "github.com/gohugoio/hugo/langs" - qt "github.com/frankban/quicktest" ) @@ -29,7 +25,7 @@ func TestSymDiff(t *testing.T) { c := qt.New(t) - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() s1 := []TstX{{A: "a"}, {A: "b"}} s2 := []TstX{{A: "a"}, {A: "e"}} diff --git a/tpl/collections/where.go b/tpl/collections/where.go index df29baf13..b20c290fa 100644 --- a/tpl/collections/where.go +++ b/tpl/collections/where.go @@ -380,7 +380,7 @@ func (ns *Namespace) checkWhereArray(seqv, kv, mv reflect.Value, path []string, if kv.Kind() == reflect.String { if params, ok := rvv.Interface().(maps.Params); ok { - vvv = reflect.ValueOf(params.Get(path...)) + vvv = reflect.ValueOf(params.GetNested(path...)) } else { vvv = rvv for i, elemName := range path { @@ -394,7 +394,7 @@ func (ns *Namespace) checkWhereArray(seqv, kv, mv reflect.Value, path []string, if i < len(path)-1 && vvv.IsValid() { if params, ok := vvv.Interface().(maps.Params); ok { // The current path element is the map itself, .Params. - vvv = reflect.ValueOf(params.Get(path[i+1:]...)) + vvv = reflect.ValueOf(params.GetNested(path[i+1:]...)) break } } diff --git a/tpl/collections/where_test.go b/tpl/collections/where_test.go index 9a65de3d5..e5ae85e88 100644 --- a/tpl/collections/where_test.go +++ b/tpl/collections/where_test.go @@ -22,16 +22,12 @@ import ( "time" "github.com/gohugoio/hugo/common/maps" - "github.com/gohugoio/hugo/config" - "github.com/gohugoio/hugo/langs" - - "github.com/gohugoio/hugo/deps" ) func TestWhere(t *testing.T) { t.Parallel() - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() type Mid struct { Tst TstX @@ -685,7 +681,7 @@ func TestWhere(t *testing.T) { func TestCheckCondition(t *testing.T) { t.Parallel() - ns := New(&deps.Deps{Language: langs.NewDefaultLanguage(config.New())}) + ns := newNs() type expect struct { result bool |