Skip to content

Commit

Permalink
Spring test cleaning #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Mar 17, 2018
1 parent debd366 commit 6768406
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 66 deletions.
19 changes: 4 additions & 15 deletions hugolib/404_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/")

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.WithSimpleConfig().WithTemplatesAdded("404.html", "<html><body>Not Found!</body></html>")
b.CreateSites().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
// right now.
th.assertFileContent("public/404.html", "Not Found")
b.AssertFileContent("public/404.html", "Not Found")

}
71 changes: 28 additions & 43 deletions hugolib/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"runtime"
"testing"

"github.com/gohugoio/hugo/deps"
"github.com/stretchr/testify/require"
)

Expand All @@ -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.WithSimpleConfig().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.WithSimpleConfig().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.WithSimpleConfig().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) {
Expand Down
38 changes: 30 additions & 8 deletions hugolib/testhelpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type sitesBuilder struct {
// Default toml
configFormat string

additionalContent []string
additionalTemplates []string

// We will add some default if not set.
templatesAdded bool
i18nAdded bool
Expand Down Expand Up @@ -84,6 +87,13 @@ func (s *sitesBuilder) WithConfig(format, conf string) *sitesBuilder {
return s
}

func (s *sitesBuilder) WithSimpleConfig() *sitesBuilder {
var config = `
baseURL = "http://example.com/"
`
return s.WithConfig("toml", config)
}

func (s *sitesBuilder) WithDefaultMultiSiteConfig() *sitesBuilder {
var defaultMultiSiteConfig = `
baseURL = "http://example.com/blog"
Expand Down Expand Up @@ -146,12 +156,13 @@ lag = "lag"

}

func (s *sitesBuilder) WithContent(filenameContent ...string) *sitesBuilder {
s.contentAdded = true
return s.WithContentAdded(filenameContent...)
func (s *sitesBuilder) WithContentAdded(filenameContent ...string) *sitesBuilder {
s.additionalContent = append(s.additionalContent, filenameContent...)
return s
}

func (s *sitesBuilder) WithContentAdded(filenameContent ...string) *sitesBuilder {
func (s *sitesBuilder) WithContent(filenameContent ...string) *sitesBuilder {
s.contentAdded = true
if len(filenameContent)%2 != 0 {
s.Fatalf("expect filenameContent in pairs")
}
Expand All @@ -162,15 +173,16 @@ func (s *sitesBuilder) WithContentAdded(filenameContent ...string) *sitesBuilder
return s
}

func (s *sitesBuilder) WithTemplatesAdded(filenameContent ...string) *sitesBuilder {
s.additionalTemplates = append(s.additionalTemplates, 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...)
}

func (s *sitesBuilder) WithTemplatesAdded(filenameContent ...string) *sitesBuilder {
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)
Expand All @@ -191,6 +203,12 @@ func (s *sitesBuilder) CreateSites() *sitesBuilder {
if !s.contentAdded {
s.addDefaultContent()
}
if len(s.additionalContent) > 0 {
s.WithContent(s.additionalContent...)
}
if len(s.additionalTemplates) > 0 {
s.WithTemplates(s.additionalTemplates...)
}

if s.Cfg == nil {
cfg, err := LoadConfig(s.Fs.Source, "", "config."+s.configFormat)
Expand Down Expand Up @@ -316,6 +334,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
Expand Down

0 comments on commit 6768406

Please sign in to comment.