Misc config loading fixes

The main motivation behind this is simplicity and correctnes, but the new small config library is also faster:

```
BenchmarkDefaultConfigProvider/Viper-16         	  252418	      4546 ns/op	    2720 B/op	      30 allocs/op
BenchmarkDefaultConfigProvider/Custom-16        	  450756	      2651 ns/op	    1008 B/op	       6 allocs/op
```

Fixes #8633
Fixes #8618
Fixes #8630
Updates #8591
Closes #6680
Closes #5192
This commit is contained in:
Bjørn Erik Pedersen
2021-06-09 10:58:18 +02:00
parent a886dd53b8
commit d392893cd7
107 changed files with 2159 additions and 1060 deletions

View File

@@ -15,18 +15,18 @@ package cast
import (
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/docshelper"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/tpl/internal"
"github.com/spf13/viper"
)
// This file provides documentation support and is randomly put into this package.
func init() {
docsProvider := func() docshelper.DocProvider {
d := &deps.Deps{
Cfg: viper.New(),
Cfg: config.New(),
Log: loggers.NewErrorLogger(),
BuildStartListeners: &deps.Listeners{},
Site: page.NewDummyHugoSite(newTestConfig()),
@@ -46,8 +46,8 @@ func init() {
docshelper.AddDocProviderFunc(docsProvider)
}
func newTestConfig() *viper.Viper {
v := viper.New()
func newTestConfig() config.Provider {
v := config.New()
v.Set("contentDir", "content")
return v
}

View File

@@ -32,7 +32,7 @@ import (
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/langs"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
type tstNoStringer struct{}
@@ -986,7 +986,7 @@ func newDeps(cfg config.Provider) *deps.Deps {
}
func newTestNs() *Namespace {
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
return New(newDeps(v))
}

View File

@@ -17,10 +17,10 @@ import (
"testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/htesting/hqt"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/tpl/internal"
"github.com/spf13/viper"
)
func TestInit(t *testing.T) {
@@ -28,7 +28,7 @@ func TestInit(t *testing.T) {
var found bool
var ns *internal.TemplateFuncsNamespace
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
langs.LoadLanguageSettings(v, nil)

View File

@@ -34,12 +34,12 @@ import (
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/langs"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
func TestScpGetLocal(t *testing.T) {
t.Parallel()
v := viper.New()
v := config.New()
fs := hugofs.NewMem(v)
ps := helpers.FilePathSeparator
@@ -144,7 +144,7 @@ func TestScpGetRemoteParallel(t *testing.T) {
c.Assert(err, qt.IsNil)
for _, ignoreCache := range []bool{false} {
cfg := viper.New()
cfg := config.New()
cfg.Set("ignoreCache", ignoreCache)
cfg.Set("contentDir", "content")
@@ -223,7 +223,7 @@ func newDeps(cfg config.Provider) *deps.Deps {
}
func newTestNs() *Namespace {
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
return New(newDeps(v))
}

View File

@@ -20,6 +20,7 @@ import (
"errors"
"html/template"
"github.com/gohugoio/hugo/common/maps"
"github.com/spf13/cast"
)
@@ -71,7 +72,7 @@ func (ns *Namespace) Jsonify(args ...interface{}) (template.HTML, error) {
case 2:
var opts map[string]string
opts, err = cast.ToStringMapStringE(args[0])
opts, err = maps.ToStringMapStringE(args[0])
if err != nil {
break
}

View File

@@ -16,20 +16,21 @@ package hugo
import (
"testing"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/htesting/hqt"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/tpl/internal"
"github.com/spf13/viper"
)
func TestInit(t *testing.T) {
c := qt.New(t)
var found bool
var ns *internal.TemplateFuncsNamespace
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
s := page.NewDummyHugoSite(v)

View File

@@ -22,11 +22,11 @@ import (
"testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs"
"github.com/spf13/afero"
"github.com/spf13/cast"
"github.com/spf13/viper"
)
type tstNoStringer struct{}
@@ -82,7 +82,7 @@ func TestNSConfig(t *testing.T) {
t.Parallel()
c := qt.New(t)
v := viper.New()
v := config.New()
v.Set("workingDir", "/a/b")
ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})

View File

@@ -17,11 +17,12 @@ import (
"path/filepath"
"testing"
"github.com/gohugoio/hugo/config"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugofs"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
func TestReadFile(t *testing.T) {
@@ -30,7 +31,7 @@ func TestReadFile(t *testing.T) {
workingDir := "/home/hugo"
v := viper.New()
v := config.New()
v.Set("workingDir", workingDir)
// f := newTestFuncsterWithViper(v)
@@ -68,7 +69,7 @@ func TestFileExists(t *testing.T) {
workingDir := "/home/hugo"
v := viper.New()
v := config.New()
v.Set("workingDir", workingDir)
ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})
@@ -103,7 +104,7 @@ func TestStat(t *testing.T) {
c := qt.New(t)
workingDir := "/home/hugo"
v := viper.New()
v := config.New()
v.Set("workingDir", workingDir)
ns := New(&deps.Deps{Fs: hugofs.NewMem(v)})

View File

@@ -18,11 +18,11 @@ import (
"testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/deps"
"github.com/spf13/viper"
)
var ns = New(&deps.Deps{Cfg: viper.New()})
var ns = New(&deps.Deps{Cfg: config.New()})
type tstNoStringer struct{}

View File

@@ -282,7 +282,7 @@ func (ns *Namespace) ToCSS(args ...interface{}) (resource.Resource, error) {
}
if m != nil {
maps.ToLower(m)
maps.PrepareParams(m)
if t, found := m["transpiler"]; found {
switch t {
case transpilerDart, transpilerLibSass:

View File

@@ -16,12 +16,13 @@ package site
import (
"testing"
"github.com/gohugoio/hugo/config"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/htesting/hqt"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/tpl/internal"
"github.com/spf13/viper"
)
func TestInit(t *testing.T) {
@@ -29,7 +30,7 @@ func TestInit(t *testing.T) {
var found bool
var ns *internal.TemplateFuncsNamespace
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
s := page.NewDummyHugoSite(v)

View File

@@ -16,12 +16,13 @@ package strings
import (
"testing"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/htesting/hqt"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/tpl/internal"
"github.com/spf13/viper"
)
func TestInit(t *testing.T) {
@@ -30,7 +31,7 @@ func TestInit(t *testing.T) {
var ns *internal.TemplateFuncsNamespace
for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
ns = nsf(&deps.Deps{Cfg: viper.New()})
ns = nsf(&deps.Deps{Cfg: config.New()})
if ns.Name == name {
found = true
break

View File

@@ -17,14 +17,14 @@ import (
"html/template"
"testing"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/deps"
qt "github.com/frankban/quicktest"
"github.com/spf13/cast"
"github.com/spf13/viper"
)
var ns = New(&deps.Deps{Cfg: viper.New()})
var ns = New(&deps.Deps{Cfg: config.New()})
type tstNoStringer struct{}

View File

@@ -37,13 +37,13 @@ import (
"github.com/gohugoio/hugo/tpl/internal"
"github.com/gohugoio/hugo/tpl/partials"
"github.com/spf13/afero"
"github.com/spf13/viper"
)
var logger = loggers.NewErrorLogger()
func newTestConfig() config.Provider {
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
v.Set("dataDir", "data")
v.Set("i18nDir", "i18n")
@@ -206,7 +206,7 @@ func BenchmarkPartialCached(b *testing.B) {
func doBenchmarkPartial(b *testing.B, f func(ns *partials.Namespace) error) {
c := qt.New(b)
config := newDepsConfig(viper.New())
config := newDepsConfig(config.New())
config.WithTemplate = func(templ tpl.TemplateManager) error {
err := templ.AddTemplate("partials/bench1", `{{ shuffle (seq 1 10) }}`)
if err != nil {

View File

@@ -16,16 +16,16 @@ package transform
import (
"testing"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/htesting"
qt "github.com/frankban/quicktest"
"github.com/spf13/viper"
)
func TestRemarshal(t *testing.T) {
t.Parallel()
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
ns := New(newDeps(v))
c := qt.New(t)
@@ -112,7 +112,7 @@ title: Test Metadata
func TestRemarshalComments(t *testing.T) {
t.Parallel()
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
ns := New(newDeps(v))
@@ -158,7 +158,7 @@ func TestTestRemarshalError(t *testing.T) {
t.Parallel()
c := qt.New(t)
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
ns := New(newDeps(v))
@@ -172,7 +172,7 @@ func TestTestRemarshalError(t *testing.T) {
func TestTestRemarshalMapInput(t *testing.T) {
t.Parallel()
c := qt.New(t)
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
ns := New(newDeps(v))

View File

@@ -26,7 +26,7 @@ import (
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/langs"
"github.com/spf13/viper"
)
type tstNoStringer struct{}
@@ -35,7 +35,7 @@ func TestEmojify(t *testing.T) {
t.Parallel()
c := qt.New(t)
v := viper.New()
v := config.New()
ns := New(newDeps(v))
for _, test := range []struct {
@@ -64,7 +64,7 @@ func TestHighlight(t *testing.T) {
t.Parallel()
c := qt.New(t)
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
ns := New(newDeps(v))
@@ -96,7 +96,7 @@ func TestHTMLEscape(t *testing.T) {
t.Parallel()
c := qt.New(t)
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
ns := New(newDeps(v))
@@ -126,7 +126,7 @@ func TestHTMLUnescape(t *testing.T) {
t.Parallel()
c := qt.New(t)
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
ns := New(newDeps(v))
@@ -156,7 +156,7 @@ func TestMarkdownify(t *testing.T) {
t.Parallel()
c := qt.New(t)
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
ns := New(newDeps(v))
@@ -185,7 +185,7 @@ func TestMarkdownify(t *testing.T) {
func TestMarkdownifyBlocksOfText(t *testing.T) {
t.Parallel()
c := qt.New(t)
v := viper.New()
v := config.New()
v.Set("contentDir", "content")
ns := New(newDeps(v))
@@ -211,7 +211,7 @@ func TestPlainify(t *testing.T) {
t.Parallel()
c := qt.New(t)
v := viper.New()
v := config.New()
ns := New(newDeps(v))
for _, test := range []struct {

View File

@@ -19,13 +19,14 @@ import (
"strings"
"testing"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/resources/resource"
"github.com/gohugoio/hugo/media"
qt "github.com/frankban/quicktest"
"github.com/spf13/viper"
)
const (
@@ -79,7 +80,7 @@ func (t testContentResource) Key() string {
}
func TestUnmarshal(t *testing.T) {
v := viper.New()
v := config.New()
ns := New(newDeps(v))
c := qt.New(t)
@@ -173,7 +174,7 @@ a;b;c`, mime: media.CSVType}, map[string]interface{}{"DElimiter": ";", "Comment"
}
func BenchmarkUnmarshalString(b *testing.B) {
v := viper.New()
v := config.New()
ns := New(newDeps(v))
const numJsons = 100
@@ -196,7 +197,7 @@ func BenchmarkUnmarshalString(b *testing.B) {
}
func BenchmarkUnmarshalResource(b *testing.B) {
v := viper.New()
v := config.New()
ns := New(newDeps(v))
const numJsons = 100

View File

@@ -16,11 +16,12 @@ package urls
import (
"testing"
"github.com/gohugoio/hugo/config"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/htesting/hqt"
"github.com/gohugoio/hugo/tpl/internal"
"github.com/spf13/viper"
)
func TestInit(t *testing.T) {
@@ -29,7 +30,7 @@ func TestInit(t *testing.T) {
var ns *internal.TemplateFuncsNamespace
for _, nsf := range internal.TemplateFuncsNamespaceRegistry {
ns = nsf(&deps.Deps{Cfg: viper.New()})
ns = nsf(&deps.Deps{Cfg: config.New()})
if ns.Name == name {
found = true
break

View File

@@ -17,14 +17,15 @@ import (
"net/url"
"testing"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/htesting/hqt"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/deps"
"github.com/spf13/viper"
)
var ns = New(&deps.Deps{Cfg: viper.New()})
var ns = New(&deps.Deps{Cfg: config.New()})
type tstNoStringer struct{}