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 1a0fb68
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 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")

}
20 changes: 8 additions & 12 deletions hugolib/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,19 @@ 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}
)

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{})
b := newTestSitesBuilder(t)
b.WithSimpleConfig().WithContent("page.md", pageWithAlias)
b.CreateSites().Build(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) {
Expand Down
34 changes: 26 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

0 comments on commit 1a0fb68

Please sign in to comment.