diff --git a/hugolib/404_test.go b/hugolib/404_test.go index bbaed61d757..5ea98be62b2 100644 --- a/hugolib/404_test.go +++ b/hugolib/404_test.go @@ -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"), "Not Found!") - 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", "Not Found!") + 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") } diff --git a/hugolib/alias_test.go b/hugolib/alias_test.go index abbda5f355f..d20409512fa 100644 --- a/hugolib/alias_test.go +++ b/hugolib/alias_test.go @@ -18,7 +18,6 @@ import ( "runtime" "testing" - "github.com/gohugoio/hugo/deps" "github.com/stretchr/testify/require" ) @@ -42,73 +41,59 @@ const aliasTemplate = "ALIASTEMPLATE" 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"), "}} ` - 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