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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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 {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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