Skip to content

Commit

Permalink
Spring test cleaning, take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Mar 18, 2018
1 parent debd366 commit da88015
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 159 deletions.
21 changes: 5 additions & 16 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.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")

}
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.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) {
Expand Down
2 changes: 1 addition & 1 deletion hugolib/hugo_sites_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion hugolib/hugo_sites_multihost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
16 changes: 6 additions & 10 deletions hugolib/robotstxt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
package hugolib

import (
"path/filepath"
"testing"

"github.com/gohugoio/hugo/deps"
"github.com/spf13/viper"
)

const robotTxtTemplate = `User-agent: Googlebot
Expand All @@ -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")

}
Loading

0 comments on commit da88015

Please sign in to comment.