shithub: hugo

Download patch

ref: da88015776645cc68b96e8b94030c95905df53ae
parent: debd3663dd0a4dacf7dff01e2e602e58a4fa8ee7
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Mar 17 15:24:02 EDT 2018

Spring test cleaning, take 2

--- a/hugolib/404_test.go
+++ b/hugolib/404_test.go
@@ -14,30 +14,19 @@
 package hugolib
 
 import (
-	"path/filepath"
-
 	"testing"
-
-	"github.com/gohugoio/hugo/deps"
 )
 
 func Test404(t *testing.T) {
 	t.Parallel()
-	var (
-		cfg, fs = newTestCfg()
-		th      = testHelper{cfg, fs, t}
-	)
 
-	cfg.Set("baseURL", "http://auth/bub/")
+	b := newTestSitesBuilder(t)
+	b.WithSimpleConfigFile().WithTemplatesAdded("404.html", "<html><body>Not Found!</body></html>")
+	b.Build(BuildCfg{})
 
-	writeSource(t, fs, filepath.Join("layouts", "404.html"), "<html><body>Not Found!</body></html>")
-	writeSource(t, fs, filepath.Join("content", "page.md"), "A page")
-
-	buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
-
 	// Note: We currently have only 1 404 page. One might think that we should have
-	// multiple, to follow the Custom Output scheme, but I don't see how that wold work
+	// multiple, to follow the Custom Output scheme, but I don't see how that would work
 	// right now.
-	th.assertFileContent("public/404.html", "Not Found")
+	b.AssertFileContent("public/404.html", "Not Found")
 
 }
--- a/hugolib/alias_test.go
+++ b/hugolib/alias_test.go
@@ -18,7 +18,6 @@
 	"runtime"
 	"testing"
 
-	"github.com/gohugoio/hugo/deps"
 	"github.com/stretchr/testify/require"
 )
 
@@ -42,73 +41,59 @@
 
 func TestAlias(t *testing.T) {
 	t.Parallel()
+	assert := require.New(t)
 
-	var (
-		cfg, fs = newTestCfg()
-		th      = testHelper{cfg, fs, t}
-	)
+	b := newTestSitesBuilder(t)
+	b.WithSimpleConfigFile().WithContent("page.md", pageWithAlias)
+	b.CreateSites().Build(BuildCfg{})
 
-	writeSource(t, fs, filepath.Join("content", "page.md"), pageWithAlias)
-	writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), basicTemplate)
+	assert.Equal(1, len(b.H.Sites))
+	require.Len(t, b.H.Sites[0].RegularPages, 1)
 
-	s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
-
-	require.Len(t, s.rawAllPages, 1)
-
 	// the real page
-	th.assertFileContent(filepath.Join("public", "page", "index.html"), "For some moments the old man")
+	b.AssertFileContent("public/page/index.html", "For some moments the old man")
 	// the alias redirector
-	th.assertFileContent(filepath.Join("public", "foo", "bar", "index.html"), "<meta http-equiv=\"refresh\" content=\"0; ")
+	b.AssertFileContent("public/foo/bar/index.html", "<meta http-equiv=\"refresh\" content=\"0; ")
 }
 
 func TestAliasMultipleOutputFormats(t *testing.T) {
 	t.Parallel()
 
-	var (
-		cfg, fs = newTestCfg()
-		th      = testHelper{cfg, fs, t}
-	)
+	assert := require.New(t)
 
-	writeSource(t, fs, filepath.Join("content", "page.md"), pageWithAliasMultipleOutputs)
-	writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), basicTemplate)
-	writeSource(t, fs, filepath.Join("layouts", "_default", "single.amp.html"), basicTemplate)
-	writeSource(t, fs, filepath.Join("layouts", "_default", "single.json"), basicTemplate)
+	b := newTestSitesBuilder(t)
+	b.WithSimpleConfigFile().WithContent("page.md", pageWithAliasMultipleOutputs)
 
-	buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+	b.WithTemplates(
+		"_default/single.html", basicTemplate,
+		"_default/single.amp.html", basicTemplate,
+		"_default/single.json", basicTemplate)
 
+	b.CreateSites().Build(BuildCfg{})
+
 	// the real pages
-	th.assertFileContent(filepath.Join("public", "page", "index.html"), "For some moments the old man")
-	th.assertFileContent(filepath.Join("public", "amp", "page", "index.html"), "For some moments the old man")
-	th.assertFileContent(filepath.Join("public", "page", "index.json"), "For some moments the old man")
+	b.AssertFileContent("public/page/index.html", "For some moments the old man")
+	b.AssertFileContent("public/amp/page/index.html", "For some moments the old man")
+	b.AssertFileContent("public/page/index.json", "For some moments the old man")
 
 	// the alias redirectors
-	th.assertFileContent(filepath.Join("public", "foo", "bar", "index.html"), "<meta http-equiv=\"refresh\" content=\"0; ")
-	th.assertFileContent(filepath.Join("public", "foo", "bar", "amp", "index.html"), "<meta http-equiv=\"refresh\" content=\"0; ")
-	require.False(t, destinationExists(th.Fs, filepath.Join("public", "foo", "bar", "index.json")))
+	b.AssertFileContent("public/foo/bar/index.html", "<meta http-equiv=\"refresh\" content=\"0; ")
+	b.AssertFileContent("public/foo/bar/amp/index.html", "<meta http-equiv=\"refresh\" content=\"0; ")
+	assert.False(b.CheckExists("public/foo/bar/index.json"))
 }
 
 func TestAliasTemplate(t *testing.T) {
 	t.Parallel()
 
-	var (
-		cfg, fs = newTestCfg()
-		th      = testHelper{cfg, fs, t}
-	)
+	b := newTestSitesBuilder(t)
+	b.WithSimpleConfigFile().WithContent("page.md", pageWithAlias).WithTemplatesAdded("alias.html", aliasTemplate)
 
-	writeSource(t, fs, filepath.Join("content", "page.md"), pageWithAlias)
-	writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), basicTemplate)
-	writeSource(t, fs, filepath.Join("layouts", "alias.html"), aliasTemplate)
+	b.CreateSites().Build(BuildCfg{})
 
-	sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})
-
-	require.NoError(t, err)
-
-	require.NoError(t, sites.Build(BuildCfg{}))
-
 	// the real page
-	th.assertFileContent(filepath.Join("public", "page", "index.html"), "For some moments the old man")
+	b.AssertFileContent("public/page/index.html", "For some moments the old man")
 	// the alias redirector
-	th.assertFileContent(filepath.Join("public", "foo", "bar", "index.html"), "ALIASTEMPLATE")
+	b.AssertFileContent("public/foo/bar/index.html", "ALIASTEMPLATE")
 }
 
 func TestTargetPathHTMLRedirectAlias(t *testing.T) {
--- a/hugolib/hugo_sites_build_test.go
+++ b/hugolib/hugo_sites_build_test.go
@@ -157,7 +157,7 @@
 	t.Parallel()
 
 	assert := require.New(t)
-	b := newTestSitesBuilder(t).WithConfig("toml", `
+	b := newTestSitesBuilder(t).WithConfigFile("toml", `
 
 defaultContentLanguage = "nn"
 
--- a/hugolib/hugo_sites_multihost_test.go
+++ b/hugolib/hugo_sites_multihost_test.go
@@ -48,7 +48,7 @@
 
 `
 
-	b := newMultiSiteTestDefaultBuilder(t).WithConfig("toml", configTemplate)
+	b := newMultiSiteTestDefaultBuilder(t).WithConfigFile("toml", configTemplate)
 	b.CreateSites().Build(BuildCfg{})
 
 	b.AssertFileContent("public/en/sect/doc1-slug/index.html", "Hello")
--- a/hugolib/robotstxt_test.go
+++ b/hugolib/robotstxt_test.go
@@ -14,10 +14,9 @@
 package hugolib
 
 import (
-	"path/filepath"
 	"testing"
 
-	"github.com/gohugoio/hugo/deps"
+	"github.com/spf13/viper"
 )
 
 const robotTxtTemplate = `User-agent: Googlebot
@@ -28,19 +27,16 @@
 
 func TestRobotsTXTOutput(t *testing.T) {
 	t.Parallel()
-	var (
-		cfg, fs = newTestCfg()
-		th      = testHelper{cfg, fs, t}
-	)
 
+	cfg := viper.New()
 	cfg.Set("baseURL", "http://auth/bub/")
 	cfg.Set("enableRobotsTXT", true)
 
-	writeSource(t, fs, filepath.Join("layouts", "robots.txt"), robotTxtTemplate)
-	writeSourcesToSource(t, "content", fs, weightedSources...)
+	b := newTestSitesBuilder(t).WithViper(cfg)
+	b.WithTemplatesAdded("layouts/robots.txt", robotTxtTemplate)
 
-	buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+	b.Build(BuildCfg{})
 
-	th.assertFileContent("public/robots.txt", "User-agent: Googlebot")
+	b.AssertFileContent("public/robots.txt", "User-agent: Googlebot")
 
 }
--- a/hugolib/testhelpers_test.go
+++ b/hugolib/testhelpers_test.go
@@ -45,11 +45,18 @@
 	// Default toml
 	configFormat string
 
-	// We will add some default if not set.
-	templatesAdded bool
-	i18nAdded      bool
-	dataAdded      bool
-	contentAdded   bool
+	// Base data/content
+	contentFilePairs  []string
+	templateFilePairs []string
+	i18nFilePairs     []string
+	dataFilePairs     []string
+
+	// Additional data/content.
+	// As in "use the base, but add these on top".
+	contentFilePairsAdded  []string
+	templateFilePairsAdded []string
+	i18nFilePairsAdded     []string
+	dataFilePairsAdded     []string
 }
 
 func newTestSitesBuilder(t testing.TB) *sitesBuilder {
@@ -71,19 +78,32 @@
 
 	templ, err := template.New("test").Parse(configTemplate)
 	if err != nil {
-		s.T.Fatal("Template parse failed:", err)
+		s.Fatalf("Template parse failed: %s", err)
 	}
 	var b bytes.Buffer
 	templ.Execute(&b, data)
-	return s.WithConfig(format, b.String())
+	return s.WithConfigFile(format, b.String())
 }
 
-func (s *sitesBuilder) WithConfig(format, conf string) *sitesBuilder {
+func (s *sitesBuilder) WithViper(v *viper.Viper) *sitesBuilder {
+	loadDefaultSettingsFor(v)
+	s.Cfg = v
+	return s
+}
+
+func (s *sitesBuilder) WithConfigFile(format, conf string) *sitesBuilder {
 	writeSource(s.T, s.Fs, "config."+format, conf)
 	s.configFormat = format
 	return s
 }
 
+func (s *sitesBuilder) WithSimpleConfigFile() *sitesBuilder {
+	var config = `
+baseURL = "http://example.com/"
+`
+	return s.WithConfigFile("toml", config)
+}
+
 func (s *sitesBuilder) WithDefaultMultiSiteConfig() *sitesBuilder {
 	var defaultMultiSiteConfig = `
 baseURL = "http://example.com/blog"
@@ -142,60 +162,76 @@
 lag = "lag"
 `
 
-	return s.WithConfig("toml", defaultMultiSiteConfig)
+	return s.WithConfigFile("toml", defaultMultiSiteConfig)
 
 }
 
 func (s *sitesBuilder) WithContent(filenameContent ...string) *sitesBuilder {
-	s.contentAdded = true
-	return s.WithContentAdded(filenameContent...)
+	s.contentFilePairs = append(s.contentFilePairs, filenameContent...)
+	return s
 }
 
 func (s *sitesBuilder) WithContentAdded(filenameContent ...string) *sitesBuilder {
-	if len(filenameContent)%2 != 0 {
-		s.Fatalf("expect filenameContent in pairs")
-	}
-	for i := 0; i < len(filenameContent); i += 2 {
-		filename, content := filenameContent[i], filenameContent[i+1]
-		writeSource(s.T, s.Fs, filepath.Join("content", filename), content)
-	}
+	s.contentFilePairsAdded = append(s.contentFilePairsAdded, filenameContent...)
 	return s
 }
 
 func (s *sitesBuilder) WithTemplates(filenameContent ...string) *sitesBuilder {
-	if len(filenameContent)%2 != 0 {
-		s.Fatalf("expect filenameContent in pairs")
-	}
-	s.templatesAdded = true
-	return s.WithTemplatesAdded(filenameContent...)
+	s.templateFilePairs = append(s.templateFilePairs, filenameContent...)
+	return s
 }
 
 func (s *sitesBuilder) WithTemplatesAdded(filenameContent ...string) *sitesBuilder {
+	s.templateFilePairsAdded = append(s.templateFilePairsAdded, filenameContent...)
+	return s
+}
+
+func (s *sitesBuilder) WithData(filenameContent ...string) *sitesBuilder {
+	s.dataFilePairs = append(s.dataFilePairs, filenameContent...)
+	return s
+}
+
+func (s *sitesBuilder) WithDataAdded(filenameContent ...string) *sitesBuilder {
+	s.dataFilePairsAdded = append(s.dataFilePairsAdded, filenameContent...)
+	return s
+}
+
+func (s *sitesBuilder) WithI18n(filenameContent ...string) *sitesBuilder {
+	s.i18nFilePairs = append(s.i18nFilePairs, filenameContent...)
+	return s
+}
+
+func (s *sitesBuilder) WithI18nAdded(filenameContent ...string) *sitesBuilder {
+	s.i18nFilePairsAdded = append(s.i18nFilePairsAdded, filenameContent...)
+	return s
+}
+
+func (s *sitesBuilder) writeFilePairs(folder string, filenameContent []string) *sitesBuilder {
+	if len(filenameContent)%2 != 0 {
+		s.Fatalf("expect filenameContent for %q in pairs (%d)", folder, len(filenameContent))
+	}
 	for i := 0; i < len(filenameContent); i += 2 {
 		filename, content := filenameContent[i], filenameContent[i+1]
-		writeSource(s.T, s.Fs, filepath.Join("layouts", filename), content)
+		writeSource(s.T, s.Fs, filepath.Join(folder, filename), content)
 	}
 	return s
 }
 
 func (s *sitesBuilder) CreateSites() *sitesBuilder {
-	if !s.templatesAdded {
-		s.addDefaultTemplates()
-	}
-	if !s.i18nAdded {
-		s.addDefaultI18n()
-	}
-	if !s.dataAdded {
-		s.addDefaultData()
-	}
-	if !s.contentAdded {
-		s.addDefaultContent()
-	}
+	s.addDefaults()
+	s.writeFilePairs("content", s.contentFilePairs)
+	s.writeFilePairs("content", s.contentFilePairsAdded)
+	s.writeFilePairs("layouts", s.templateFilePairs)
+	s.writeFilePairs("layouts", s.templateFilePairsAdded)
+	s.writeFilePairs("data", s.dataFilePairs)
+	s.writeFilePairs("data", s.dataFilePairsAdded)
+	s.writeFilePairs("i18n", s.i18nFilePairs)
+	s.writeFilePairs("i18n", s.i18nFilePairsAdded)
 
 	if s.Cfg == nil {
 		cfg, err := LoadConfig(s.Fs.Source, "", "config."+s.configFormat)
 		if err != nil {
-			s.T.Fatalf("Failed to load config: %s", err)
+			s.Fatalf("Failed to load config: %s", err)
 		}
 		s.Cfg = cfg
 	}
@@ -202,7 +238,7 @@
 
 	sites, err := NewHugoSites(deps.DepsCfg{Fs: s.Fs, Cfg: s.Cfg, Running: s.running})
 	if err != nil {
-		s.T.Fatalf("Failed to create sites: %s", err)
+		s.Fatalf("Failed to create sites: %s", err)
 	}
 	s.H = sites
 
@@ -211,61 +247,20 @@
 
 func (s *sitesBuilder) Build(cfg BuildCfg) *sitesBuilder {
 	if s.H == nil {
-		s.T.Fatal("Need to run builder.CreateSites first")
+		s.CreateSites()
 	}
 	err := s.H.Build(cfg)
 	if err != nil {
-		s.T.Fatalf("Build failed: %s", err)
+		s.Fatalf("Build failed: %s", err)
 	}
 
 	return s
 }
 
-func (s *sitesBuilder) addDefaultTemplates() {
-	fs := s.Fs
-	t := s.T
+func (s *sitesBuilder) addDefaults() {
 
-	// Layouts
-
-	writeSource(t, fs, filepath.Join("layouts", "_default/single.html"), "Single: {{ .Title }}|{{ i18n \"hello\" }}|{{.Lang}}|{{ .Content }}")
-	writeSource(t, fs, filepath.Join("layouts", "_default/list.html"), "{{ $p := .Paginator }}List Page {{ $p.PageNumber }}: {{ .Title }}|{{ i18n \"hello\" }}|{{ .Permalink }}|Pager: {{ template \"_internal/pagination.html\" . }}")
-	writeSource(t, fs, filepath.Join("layouts", "index.html"), "{{ $p := .Paginator }}Default Home Page {{ $p.PageNumber }}: {{ .Title }}|{{ .IsHome }}|{{ i18n \"hello\" }}|{{ .Permalink }}|{{  .Site.Data.hugo.slogan }}")
-	writeSource(t, fs, filepath.Join("layouts", "index.fr.html"), "{{ $p := .Paginator }}French Home Page {{ $p.PageNumber }}: {{ .Title }}|{{ .IsHome }}|{{ i18n \"hello\" }}|{{ .Permalink }}|{{  .Site.Data.hugo.slogan }}")
-
-	// Shortcodes
-	writeSource(t, fs, filepath.Join("layouts", "shortcodes", "shortcode.html"), "Shortcode: {{ i18n \"hello\" }}")
-	// A shortcode in multiple languages
-	writeSource(t, fs, filepath.Join("layouts", "shortcodes", "lingo.html"), "LingoDefault")
-	writeSource(t, fs, filepath.Join("layouts", "shortcodes", "lingo.fr.html"), "LingoFrench")
-}
-
-func (s *sitesBuilder) addDefaultI18n() {
-	fs := s.Fs
-	t := s.T
-
-	writeSource(t, fs, filepath.Join("i18n", "en.yaml"), `
-hello:
-  other: "Hello"
-`)
-	writeSource(t, fs, filepath.Join("i18n", "fr.yaml"), `
-hello:
-  other: "Bonjour"
-`)
-
-}
-
-func (s *sitesBuilder) addDefaultData() {
-	fs := s.Fs
-	t := s.T
-
-	writeSource(t, fs, filepath.FromSlash("data/hugo.toml"), "slogan = \"Hugo Rocks!\"")
-}
-
-func (s *sitesBuilder) addDefaultContent() {
-	fs := s.Fs
-	t := s.T
-
-	contentTemplate := `---
+	var (
+		contentTemplate = `---
 title: doc1
 weight: 1
 tags:
@@ -280,10 +275,54 @@
 {{< lingo >}}
 `
 
-	writeSource(t, fs, filepath.FromSlash("content/sect/doc1.en.md"), contentTemplate)
-	writeSource(t, fs, filepath.FromSlash("content/sect/doc1.fr.md"), contentTemplate)
-	writeSource(t, fs, filepath.FromSlash("content/sect/doc1.nb.md"), contentTemplate)
-	writeSource(t, fs, filepath.FromSlash("content/sect/doc1.nn.md"), contentTemplate)
+		defaultContent = []string{
+			"content/sect/doc1.en.md", contentTemplate,
+			"content/sect/doc1.fr.md", contentTemplate,
+			"content/sect/doc1.nb.md", contentTemplate,
+			"content/sect/doc1.nn.md", contentTemplate,
+		}
+
+		defaultTemplates = []string{
+			"_default/single.html", "Single: {{ .Title }}|{{ i18n \"hello\" }}|{{.Lang}}|{{ .Content }}",
+			"_default/list.html", "{{ $p := .Paginator }}List Page {{ $p.PageNumber }}: {{ .Title }}|{{ i18n \"hello\" }}|{{ .Permalink }}|Pager: {{ template \"_internal/pagination.html\" . }}",
+			"index.html", "{{ $p := .Paginator }}Default Home Page {{ $p.PageNumber }}: {{ .Title }}|{{ .IsHome }}|{{ i18n \"hello\" }}|{{ .Permalink }}|{{  .Site.Data.hugo.slogan }}",
+			"index.fr.html", "{{ $p := .Paginator }}French Home Page {{ $p.PageNumber }}: {{ .Title }}|{{ .IsHome }}|{{ i18n \"hello\" }}|{{ .Permalink }}|{{  .Site.Data.hugo.slogan }}",
+
+			// Shortcodes
+			"shortcodes/shortcode.html", "Shortcode: {{ i18n \"hello\" }}",
+			// A shortcode in multiple languages
+			"shortcodes/lingo.html", "LingoDefault",
+			"shortcodes/lingo.fr.html", "LingoFrench",
+		}
+
+		defaultI18n = []string{
+			"en.yaml", `
+hello:
+  other: "Hello"
+`,
+			"fr.yaml", `
+hello:
+  other: "Bonjour"
+`,
+		}
+
+		defaultData = []string{
+			"hugo.toml", "slogan = \"Hugo Rocks!\"",
+		}
+	)
+
+	if len(s.contentFilePairs) == 0 {
+		s.writeFilePairs("content", defaultContent)
+	}
+	if len(s.templateFilePairs) == 0 {
+		s.writeFilePairs("layouts", defaultTemplates)
+	}
+	if len(s.dataFilePairs) == 0 {
+		s.writeFilePairs("data", defaultData)
+	}
+	if len(s.i18nFilePairs) == 0 {
+		s.writeFilePairs("i18n", defaultI18n)
+	}
 }
 
 func (s *sitesBuilder) Fatalf(format string, args ...interface{}) {
@@ -314,6 +353,10 @@
 			s.Fatalf("No match for %q in content for %s\n%q", match, filename, content)
 		}
 	}
+}
+
+func (s *sitesBuilder) CheckExists(filename string) bool {
+	return destinationExists(s.Fs, filepath.Clean(filename))
 }
 
 type testHelper struct {