diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-02-05 10:20:06 +0700 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-02-17 17:15:26 +0100 |
commit | 93ca7c9e958e34469a337e4efcc7c75774ec50fd (patch) | |
tree | 5dfa296cfe74fd5ef8f0d41ea4078704f453aa04 /hugolib/testhelpers_test.go | |
parent | e34af6ee30f70f5780a281e2fd8f4ed9b487ee61 (diff) |
all: Refactor to nonglobal Viper, i18n etc.
This is a final rewrite that removes all the global state in Hugo, which also enables
the use if `t.Parallel` in tests.
Updates #2701
Fixes #3016
Diffstat (limited to 'hugolib/testhelpers_test.go')
-rw-r--r-- | hugolib/testhelpers_test.go | 66 |
1 files changed, 60 insertions, 6 deletions
diff --git a/hugolib/testhelpers_test.go b/hugolib/testhelpers_test.go index 1d775aca8..33e78e121 100644 --- a/hugolib/testhelpers_test.go +++ b/hugolib/testhelpers_test.go @@ -6,20 +6,64 @@ import ( "github.com/spf13/hugo/deps" "github.com/spf13/hugo/helpers" - "github.com/spf13/hugo/hugofs" "github.com/spf13/hugo/source" "github.com/spf13/hugo/tplapi" "github.com/spf13/viper" + "io/ioutil" + "os" + + "log" + + "github.com/spf13/hugo/hugofs" + jww "github.com/spf13/jwalterweatherman" "github.com/stretchr/testify/require" ) -func newTestDepsConfig() deps.DepsCfg { - return deps.DepsCfg{Fs: hugofs.NewMem()} +func newTestPathSpec(fs *hugofs.Fs, v *viper.Viper) *helpers.PathSpec { + l := helpers.NewDefaultLanguage(v) + return helpers.NewPathSpec(fs, l) } -func newTestPathSpec() *helpers.PathSpec { - return helpers.NewPathSpec(hugofs.NewMem(), viper.GetViper()) +func newTestCfg() (*viper.Viper, *hugofs.Fs) { + + v := viper.New() + fs := hugofs.NewMem(v) + + v.SetFs(fs.Source) + + loadDefaultSettingsFor(v) + + // Default is false, but true is easier to use as default in tests + v.Set("defaultContentLanguageInSubdir", true) + + return v, fs + +} + +// newTestSite creates a new site in the English language with in-memory Fs. +// The site will have a template system loaded and ready to use. +// Note: This is only used in single site tests. +func newTestSite(t testing.TB, configKeyValues ...interface{}) *Site { + + cfg, fs := newTestCfg() + + for i := 0; i < len(configKeyValues); i += 2 { + cfg.Set(configKeyValues[i].(string), configKeyValues[i+1]) + } + + d := deps.DepsCfg{Language: helpers.NewLanguage("en", cfg), Fs: fs} + + s, err := NewSiteForCfg(d) + + if err != nil { + t.Fatalf("Failed to create Site: %s", err) + } + return s +} + +func newDebugLogger() *jww.Notepad { + return jww.NewNotepad(jww.LevelDebug, jww.LevelError, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime) } func createWithTemplateFromNameValues(additionalTemplates ...string) func(templ tplapi.Template) error { @@ -36,11 +80,21 @@ func createWithTemplateFromNameValues(additionalTemplates ...string) func(templ } func buildSingleSite(t *testing.T, depsCfg deps.DepsCfg, buildCfg BuildCfg) *Site { - h, err := NewHugoSitesFromConfiguration(depsCfg) + return buildSingleSiteExpected(t, false, depsCfg, buildCfg) +} + +func buildSingleSiteExpected(t *testing.T, expectBuildError bool, depsCfg deps.DepsCfg, buildCfg BuildCfg) *Site { + h, err := NewHugoSites(depsCfg) require.NoError(t, err) require.Len(t, h.Sites, 1) + if expectBuildError { + require.Error(t, h.Build(buildCfg)) + return nil + + } + require.NoError(t, h.Build(buildCfg)) return h.Sites[0] |