Spring test cleaning, take 2

This commit is contained in:
Bjørn Erik Pedersen 2018-03-17 19:24:02 +01:00
parent debd3663dd
commit da88015776
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
6 changed files with 172 additions and 159 deletions

View file

@ -14,30 +14,19 @@
package hugolib package hugolib
import ( import (
"path/filepath"
"testing" "testing"
"github.com/gohugoio/hugo/deps"
) )
func Test404(t *testing.T) { func Test404(t *testing.T) {
t.Parallel() 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>")
writeSource(t, fs, filepath.Join("layouts", "404.html"), "<html><body>Not Found!</body></html>") b.Build(BuildCfg{})
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 // 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. // right now.
th.assertFileContent("public/404.html", "Not Found") b.AssertFileContent("public/404.html", "Not Found")
} }

View file

@ -18,7 +18,6 @@ import (
"runtime" "runtime"
"testing" "testing"
"github.com/gohugoio/hugo/deps"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -42,73 +41,59 @@ const aliasTemplate = "<html><body>ALIASTEMPLATE</body></html>"
func TestAlias(t *testing.T) { func TestAlias(t *testing.T) {
t.Parallel() t.Parallel()
assert := require.New(t)
var ( b := newTestSitesBuilder(t)
cfg, fs = newTestCfg() b.WithSimpleConfigFile().WithContent("page.md", pageWithAlias)
th = testHelper{cfg, fs, t} b.CreateSites().Build(BuildCfg{})
)
writeSource(t, fs, filepath.Join("content", "page.md"), pageWithAlias) assert.Equal(1, len(b.H.Sites))
writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), basicTemplate) 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 // 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 // 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) { func TestAliasMultipleOutputFormats(t *testing.T) {
t.Parallel() t.Parallel()
var ( assert := require.New(t)
cfg, fs = newTestCfg()
th = testHelper{cfg, fs, t}
)
writeSource(t, fs, filepath.Join("content", "page.md"), pageWithAliasMultipleOutputs) b := newTestSitesBuilder(t)
writeSource(t, fs, filepath.Join("layouts", "_default", "single.html"), basicTemplate) b.WithSimpleConfigFile().WithContent("page.md", pageWithAliasMultipleOutputs)
writeSource(t, fs, filepath.Join("layouts", "_default", "single.amp.html"), basicTemplate)
writeSource(t, fs, filepath.Join("layouts", "_default", "single.json"), basicTemplate)
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 // the real pages
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")
th.assertFileContent(filepath.Join("public", "amp", "page", "index.html"), "For some moments the old man") b.AssertFileContent("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.json", "For some moments the old man")
// the alias redirectors // the alias redirectors
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; ")
th.assertFileContent(filepath.Join("public", "foo", "bar", "amp", "index.html"), "<meta http-equiv=\"refresh\" content=\"0; ") b.AssertFileContent("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"))) assert.False(b.CheckExists("public/foo/bar/index.json"))
} }
func TestAliasTemplate(t *testing.T) { func TestAliasTemplate(t *testing.T) {
t.Parallel() t.Parallel()
var ( b := newTestSitesBuilder(t)
cfg, fs = newTestCfg() b.WithSimpleConfigFile().WithContent("page.md", pageWithAlias).WithTemplatesAdded("alias.html", aliasTemplate)
th = testHelper{cfg, fs, t}
)
writeSource(t, fs, filepath.Join("content", "page.md"), pageWithAlias) b.CreateSites().Build(BuildCfg{})
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)
require.NoError(t, sites.Build(BuildCfg{}))
// the real page // 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 // 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) { func TestTargetPathHTMLRedirectAlias(t *testing.T) {

View file

@ -157,7 +157,7 @@ func TestMultiSitesWithTwoLanguages(t *testing.T) {
t.Parallel() t.Parallel()
assert := require.New(t) assert := require.New(t)
b := newTestSitesBuilder(t).WithConfig("toml", ` b := newTestSitesBuilder(t).WithConfigFile("toml", `
defaultContentLanguage = "nn" defaultContentLanguage = "nn"

View file

@ -48,7 +48,7 @@ languageName = "Nynorsk"
` `
b := newMultiSiteTestDefaultBuilder(t).WithConfig("toml", configTemplate) b := newMultiSiteTestDefaultBuilder(t).WithConfigFile("toml", configTemplate)
b.CreateSites().Build(BuildCfg{}) b.CreateSites().Build(BuildCfg{})
b.AssertFileContent("public/en/sect/doc1-slug/index.html", "Hello") b.AssertFileContent("public/en/sect/doc1-slug/index.html", "Hello")

View file

@ -14,10 +14,9 @@
package hugolib package hugolib
import ( import (
"path/filepath"
"testing" "testing"
"github.com/gohugoio/hugo/deps" "github.com/spf13/viper"
) )
const robotTxtTemplate = `User-agent: Googlebot const robotTxtTemplate = `User-agent: Googlebot
@ -28,19 +27,16 @@ const robotTxtTemplate = `User-agent: Googlebot
func TestRobotsTXTOutput(t *testing.T) { func TestRobotsTXTOutput(t *testing.T) {
t.Parallel() t.Parallel()
var (
cfg, fs = newTestCfg()
th = testHelper{cfg, fs, t}
)
cfg := viper.New()
cfg.Set("baseURL", "http://auth/bub/") cfg.Set("baseURL", "http://auth/bub/")
cfg.Set("enableRobotsTXT", true) cfg.Set("enableRobotsTXT", true)
writeSource(t, fs, filepath.Join("layouts", "robots.txt"), robotTxtTemplate) b := newTestSitesBuilder(t).WithViper(cfg)
writeSourcesToSource(t, "content", fs, weightedSources...) 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")
} }

View file

@ -45,11 +45,18 @@ type sitesBuilder struct {
// Default toml // Default toml
configFormat string configFormat string
// We will add some default if not set. // Base data/content
templatesAdded bool contentFilePairs []string
i18nAdded bool templateFilePairs []string
dataAdded bool i18nFilePairs []string
contentAdded bool 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 { 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) templ, err := template.New("test").Parse(configTemplate)
if err != nil { if err != nil {
s.T.Fatal("Template parse failed:", err) s.Fatalf("Template parse failed: %s", err)
} }
var b bytes.Buffer var b bytes.Buffer
templ.Execute(&b, data) 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) writeSource(s.T, s.Fs, "config."+format, conf)
s.configFormat = format s.configFormat = format
return s return s
} }
func (s *sitesBuilder) WithSimpleConfigFile() *sitesBuilder {
var config = `
baseURL = "http://example.com/"
`
return s.WithConfigFile("toml", config)
}
func (s *sitesBuilder) WithDefaultMultiSiteConfig() *sitesBuilder { func (s *sitesBuilder) WithDefaultMultiSiteConfig() *sitesBuilder {
var defaultMultiSiteConfig = ` var defaultMultiSiteConfig = `
baseURL = "http://example.com/blog" baseURL = "http://example.com/blog"
@ -142,67 +162,83 @@ paginatePath = "side"
lag = "lag" lag = "lag"
` `
return s.WithConfig("toml", defaultMultiSiteConfig) return s.WithConfigFile("toml", defaultMultiSiteConfig)
} }
func (s *sitesBuilder) WithContent(filenameContent ...string) *sitesBuilder { func (s *sitesBuilder) WithContent(filenameContent ...string) *sitesBuilder {
s.contentAdded = true s.contentFilePairs = append(s.contentFilePairs, filenameContent...)
return s.WithContentAdded(filenameContent...) return s
} }
func (s *sitesBuilder) WithContentAdded(filenameContent ...string) *sitesBuilder { func (s *sitesBuilder) WithContentAdded(filenameContent ...string) *sitesBuilder {
if len(filenameContent)%2 != 0 { s.contentFilePairsAdded = append(s.contentFilePairsAdded, filenameContent...)
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)
}
return s return s
} }
func (s *sitesBuilder) WithTemplates(filenameContent ...string) *sitesBuilder { func (s *sitesBuilder) WithTemplates(filenameContent ...string) *sitesBuilder {
if len(filenameContent)%2 != 0 { s.templateFilePairs = append(s.templateFilePairs, filenameContent...)
s.Fatalf("expect filenameContent in pairs") return s
}
s.templatesAdded = true
return s.WithTemplatesAdded(filenameContent...)
} }
func (s *sitesBuilder) WithTemplatesAdded(filenameContent ...string) *sitesBuilder { 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 { for i := 0; i < len(filenameContent); i += 2 {
filename, content := filenameContent[i], filenameContent[i+1] 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 return s
} }
func (s *sitesBuilder) CreateSites() *sitesBuilder { func (s *sitesBuilder) CreateSites() *sitesBuilder {
if !s.templatesAdded { s.addDefaults()
s.addDefaultTemplates() s.writeFilePairs("content", s.contentFilePairs)
} s.writeFilePairs("content", s.contentFilePairsAdded)
if !s.i18nAdded { s.writeFilePairs("layouts", s.templateFilePairs)
s.addDefaultI18n() s.writeFilePairs("layouts", s.templateFilePairsAdded)
} s.writeFilePairs("data", s.dataFilePairs)
if !s.dataAdded { s.writeFilePairs("data", s.dataFilePairsAdded)
s.addDefaultData() s.writeFilePairs("i18n", s.i18nFilePairs)
} s.writeFilePairs("i18n", s.i18nFilePairsAdded)
if !s.contentAdded {
s.addDefaultContent()
}
if s.Cfg == nil { if s.Cfg == nil {
cfg, err := LoadConfig(s.Fs.Source, "", "config."+s.configFormat) cfg, err := LoadConfig(s.Fs.Source, "", "config."+s.configFormat)
if err != nil { if err != nil {
s.T.Fatalf("Failed to load config: %s", err) s.Fatalf("Failed to load config: %s", err)
} }
s.Cfg = cfg s.Cfg = cfg
} }
sites, err := NewHugoSites(deps.DepsCfg{Fs: s.Fs, Cfg: s.Cfg, Running: s.running}) sites, err := NewHugoSites(deps.DepsCfg{Fs: s.Fs, Cfg: s.Cfg, Running: s.running})
if err != nil { if err != nil {
s.T.Fatalf("Failed to create sites: %s", err) s.Fatalf("Failed to create sites: %s", err)
} }
s.H = sites s.H = sites
@ -211,61 +247,20 @@ func (s *sitesBuilder) CreateSites() *sitesBuilder {
func (s *sitesBuilder) Build(cfg BuildCfg) *sitesBuilder { func (s *sitesBuilder) Build(cfg BuildCfg) *sitesBuilder {
if s.H == nil { if s.H == nil {
s.T.Fatal("Need to run builder.CreateSites first") s.CreateSites()
} }
err := s.H.Build(cfg) err := s.H.Build(cfg)
if err != nil { if err != nil {
s.T.Fatalf("Build failed: %s", err) s.Fatalf("Build failed: %s", err)
} }
return s return s
} }
func (s *sitesBuilder) addDefaultTemplates() { func (s *sitesBuilder) addDefaults() {
fs := s.Fs
t := s.T
// Layouts var (
contentTemplate = `---
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 := `---
title: doc1 title: doc1
weight: 1 weight: 1
tags: tags:
@ -280,10 +275,54 @@ date: "2018-02-28"
{{< lingo >}} {{< lingo >}}
` `
writeSource(t, fs, filepath.FromSlash("content/sect/doc1.en.md"), contentTemplate) defaultContent = []string{
writeSource(t, fs, filepath.FromSlash("content/sect/doc1.fr.md"), contentTemplate) "content/sect/doc1.en.md", contentTemplate,
writeSource(t, fs, filepath.FromSlash("content/sect/doc1.nb.md"), contentTemplate) "content/sect/doc1.fr.md", contentTemplate,
writeSource(t, fs, filepath.FromSlash("content/sect/doc1.nn.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{}) { 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 { type testHelper struct {
Cfg config.Provider Cfg config.Provider
Fs *hugofs.Fs Fs *hugofs.Fs