Spring test cleaning, take 2
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 17 Mar 2018 18:24:02 +0000 (19:24 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 18 Mar 2018 08:54:32 +0000 (09:54 +0100)
hugolib/404_test.go
hugolib/alias_test.go
hugolib/hugo_sites_build_test.go
hugolib/hugo_sites_multihost_test.go
hugolib/robotstxt_test.go
hugolib/testhelpers_test.go

index bbaed61d75705c4fb4fd48ae90a2a98ab3f20f22..5ea98be62b29dd991997a9c005093ad5d7d93969 100644 (file)
 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/")
-
-       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{})
+       b := newTestSitesBuilder(t)
+       b.WithSimpleConfigFile().WithTemplatesAdded("404.html", "<html><body>Not Found!</body></html>")
+       b.Build(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")
 
 }
index abbda5f355f4ebfbcdb3f5e6d3d9a2057fc0920b..d20409512fa568e77b75b33d283a4ef0bdbc035d 100644 (file)
@@ -18,7 +18,6 @@ import (
        "runtime"
        "testing"
 
-       "github.com/gohugoio/hugo/deps"
        "github.com/stretchr/testify/require"
 )
 
@@ -42,73 +41,59 @@ const aliasTemplate = "<html><body>ALIASTEMPLATE</body></html>"
 
 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)
-
-       s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
-
-       require.Len(t, s.rawAllPages, 1)
+       assert.Equal(1, len(b.H.Sites))
+       require.Len(t, b.H.Sites[0].RegularPages, 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)
+
+       b := newTestSitesBuilder(t)
+       b.WithSimpleConfigFile().WithContent("page.md", pageWithAliasMultipleOutputs)
 
-       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.WithTemplates(
+               "_default/single.html", basicTemplate,
+               "_default/single.amp.html", basicTemplate,
+               "_default/single.json", basicTemplate)
 
-       buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{})
+       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}
-       )
-
-       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)
-
-       sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})
-
-       require.NoError(t, err)
+       b := newTestSitesBuilder(t)
+       b.WithSimpleConfigFile().WithContent("page.md", pageWithAlias).WithTemplatesAdded("alias.html", aliasTemplate)
 
-       require.NoError(t, sites.Build(BuildCfg{}))
+       b.CreateSites().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) {
index c201f5bf2afba31a69efbf979d9e6cd1dfcb44ed..5e4f171dafb1f286f6e154a1b98c669ed6bd7279 100644 (file)
@@ -157,7 +157,7 @@ func TestMultiSitesWithTwoLanguages(t *testing.T) {
        t.Parallel()
 
        assert := require.New(t)
-       b := newTestSitesBuilder(t).WithConfig("toml", `
+       b := newTestSitesBuilder(t).WithConfigFile("toml", `
 
 defaultContentLanguage = "nn"
 
index ff69f95ae969059e42fc83262eafea3f5ab43945..7dc2d8e1c003d7fbd8b2f0409abb03989827b215 100644 (file)
@@ -48,7 +48,7 @@ languageName = "Nynorsk"
 
 `
 
-       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")
index 03332cbcea59a57719bfc59a7c723058cc30a057..e924cb8dc2dd9f377ca810c219f61d023448d5e0 100644 (file)
 package hugolib
 
 import (
-       "path/filepath"
        "testing"
 
-       "github.com/gohugoio/hugo/deps"
+       "github.com/spf13/viper"
 )
 
 const robotTxtTemplate = `User-agent: Googlebot
@@ -28,19 +27,16 @@ const robotTxtTemplate = `User-agent: Googlebot
 
 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")
 
 }
index e044f61dbf5b0e86a17178c44ed50b61c31e8151..b6394f6d8a0a50379526d0f70dbe707722c73e71 100644 (file)
@@ -45,11 +45,18 @@ type sitesBuilder struct {
        // 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 @@ func (s *sitesBuilder) WithConfigTemplate(data interface{}, format, configTempla
 
        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) WithViper(v *viper.Viper) *sitesBuilder {
+       loadDefaultSettingsFor(v)
+       s.Cfg = v
+       return s
 }
 
-func (s *sitesBuilder) WithConfig(format, conf string) *sitesBuilder {
+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,67 +162,83 @@ paginatePath = "side"
 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
        }
 
        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) CreateSites() *sitesBuilder {
 
 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
-
-       // 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
+func (s *sitesBuilder) addDefaults() {
 
-       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 @@ date: "2018-02-28"
 {{< 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{}) {
@@ -316,6 +355,10 @@ func (s *sitesBuilder) AssertFileContentRe(filename string, matches ...string) {
        }
 }
 
+func (s *sitesBuilder) CheckExists(filename string) bool {
+       return destinationExists(s.Fs, filepath.Clean(filename))
+}
+
 type testHelper struct {
        Cfg config.Provider
        Fs  *hugofs.Fs