From ed847ed93d86d0e1c0993adfee787e7fa02e604b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 17 Feb 2017 20:52:50 +0100 Subject: [PATCH] hugolib: Test helper cleanup --- hugolib/alias_test.go | 12 +-- hugolib/case_insensitive_test.go | 12 +-- hugolib/embedded_shortcodes_test.go | 32 +++---- hugolib/hugo_sites_build_test.go | 144 ++++++++++++---------------- hugolib/node_as_page_test.go | 132 ++++++++++++------------- hugolib/pagination_test.go | 4 +- hugolib/robotstxt_test.go | 4 +- hugolib/rss_test.go | 8 +- hugolib/shortcode_test.go | 4 +- hugolib/site_test.go | 12 +-- hugolib/sitemap_test.go | 4 +- hugolib/template_engines_test.go | 4 +- hugolib/template_test.go | 18 ++-- hugolib/testhelpers_test.go | 32 +++++++ 14 files changed, 214 insertions(+), 208 deletions(-) diff --git a/hugolib/alias_test.go b/hugolib/alias_test.go index 653156495..6aa0ca3dd 100644 --- a/hugolib/alias_test.go +++ b/hugolib/alias_test.go @@ -36,7 +36,7 @@ func TestAlias(t *testing.T) { var ( cfg, fs = newTestCfg() - th = testHelper{cfg} + th = testHelper{cfg, fs, t} ) writeSource(t, fs, filepath.Join("content", "page.md"), pageWithAlias) @@ -45,9 +45,9 @@ func TestAlias(t *testing.T) { buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) // the real page - th.assertFileContent(t, fs, filepath.Join("public", "page", "index.html"), false, "For some moments the old man") + th.assertFileContent(filepath.Join("public", "page", "index.html"), false, "For some moments the old man") // the alias redirector - th.assertFileContent(t, fs, filepath.Join("public", "foo", "bar", "index.html"), false, "`) + th.assertFileContent("public/index.html", true, ``) } else { // should have redirect back to root - th.assertFileContent(t, fs, "public/fr/index.html", true, ``) + th.assertFileContent("public/fr/index.html", true, ``) } - th.assertFileContent(t, fs, "public/fr/index.html", defaultInSubDir, "Home", "Bonjour") - th.assertFileContent(t, fs, "public/en/index.html", defaultInSubDir, "Home", "Hello") + th.assertFileContent("public/fr/index.html", defaultInSubDir, "Home", "Bonjour") + th.assertFileContent("public/en/index.html", defaultInSubDir, "Home", "Hello") // Check list pages - th.assertFileContent(t, fs, "public/fr/sect/index.html", defaultInSubDir, "List", "Bonjour") - th.assertFileContent(t, fs, "public/en/sect/index.html", defaultInSubDir, "List", "Hello") - th.assertFileContent(t, fs, "public/fr/plaques/frtag1/index.html", defaultInSubDir, "List", "Bonjour") - th.assertFileContent(t, fs, "public/en/tags/tag1/index.html", defaultInSubDir, "List", "Hello") + th.assertFileContent("public/fr/sect/index.html", defaultInSubDir, "List", "Bonjour") + th.assertFileContent("public/en/sect/index.html", defaultInSubDir, "List", "Hello") + th.assertFileContent("public/fr/plaques/frtag1/index.html", defaultInSubDir, "List", "Bonjour") + th.assertFileContent("public/en/tags/tag1/index.html", defaultInSubDir, "List", "Hello") // Check sitemaps // Sitemaps behaves different: In a multilanguage setup there will always be a index file and // one sitemap in each lang folder. - th.assertFileContent(t, fs, "public/sitemap.xml", true, + th.assertFileContent("public/sitemap.xml", true, "http://example.com/blog/en/sitemap.xml", "http://example.com/blog/fr/sitemap.xml") if defaultInSubDir { - th.assertFileContent(t, fs, "public/fr/sitemap.xml", true, "http://example.com/blog/fr/") + th.assertFileContent("public/fr/sitemap.xml", true, "http://example.com/blog/fr/") } else { - th.assertFileContent(t, fs, "public/fr/sitemap.xml", true, "http://example.com/blog/") + th.assertFileContent("public/fr/sitemap.xml", true, "http://example.com/blog/") } - th.assertFileContent(t, fs, "public/en/sitemap.xml", true, "http://example.com/blog/en/") + th.assertFileContent("public/en/sitemap.xml", true, "http://example.com/blog/en/") // Check rss - th.assertFileContent(t, fs, "public/fr/index.xml", defaultInSubDir, `Content!", "# Pages: 4", @@ -90,7 +90,7 @@ func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) { "GetPage: Section1 ", ) - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "sect1", "regular1"), false, "Single Title: Page 01", "Content Page 01") + th.assertFileContent(expectedFilePath(ugly, "public", "sect1", "regular1"), false, "Single Title: Page 01", "Content Page 01") nodes := sites.findAllPagesByKindNotIn(KindPage) @@ -116,24 +116,24 @@ func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) { require.True(t, first.IsPage()) // Check Home paginator - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "page", "2"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "page", "2"), false, "Pag: Page 02") // Check Sections - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "sect1"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "sect1"), false, "Section Title: Section", "Section1 Content!", "Date: 2009-01-04", "Lastmod: 2009-01-05", ) - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "sect2"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "sect2"), false, "Section Title: Section", "Section2 Content!", "Date: 2009-01-06", "Lastmod: 2009-01-07", ) // Check Sections paginator - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "sect1", "page", "2"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "sect1", "page", "2"), false, "Pag: Page 02") sections := sites.findAllPagesByKind(KindSection) @@ -141,13 +141,13 @@ func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) { require.Len(t, sections, 2) // Check taxonomy lists - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "categories", "hugo"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "categories", "hugo"), false, "Taxonomy Title: Taxonomy Hugo", "Taxonomy Hugo Content!", "Date: 2009-01-08", "Lastmod: 2009-01-09", ) - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "categories", "hugo-rocks"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "categories", "hugo-rocks"), false, "Taxonomy Title: Taxonomy Hugo Rocks", ) @@ -157,7 +157,7 @@ func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) { require.NotNil(t, web) require.Len(t, web.Data["Pages"].(Pages), 4) - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "categories", "web"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "categories", "web"), false, "Taxonomy Title: Taxonomy Web", "Taxonomy Web Content!", "Date: 2009-01-10", @@ -165,12 +165,12 @@ func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) { ) // Check taxonomy list paginator - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "categories", "hugo", "page", "2"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "categories", "hugo", "page", "2"), false, "Taxonomy Title: Taxonomy Hugo", "Pag: Page 02") // Check taxonomy terms - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "categories"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "categories"), false, "Taxonomy Terms Title: Taxonomy Term Categories", "Taxonomy Term Categories Content!", "k/v: hugo", "Date: 2009-01-14", "Lastmod: 2009-01-15", @@ -179,11 +179,11 @@ func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) { // There are no pages to paginate over in the taxonomy terms. // RSS - th.assertFileContent(t, fs, filepath.Join("public", "customrss.xml"), false, "Recent content in Home Sweet Home! on Hugo Rocks", "Content!") - th.assertFileContent(t, fs, filepath.Join("public", "de", "index.html"), true, + th.assertFileContent(filepath.Join("public", "de", "index.html"), true, "Index Title: Home Sweet Home!", "Content!") // Taxonomy list - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "nn", "categories", "hugo"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "nn", "categories", "hugo"), true, "Taxonomy Title: Hugo") - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "en", "categories", "hugo"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "en", "categories", "hugo"), true, "Taxonomy Title: Taxonomy Hugo") // Taxonomy terms - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "nn", "categories"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "nn", "categories"), true, "Taxonomy Terms Title: Categories") - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "en", "categories"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "en", "categories"), true, "Taxonomy Terms Title: Taxonomy Term Categories") // Sections - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "nn", "sect1"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "nn", "sect1"), true, "Section Title: Sect1s") - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "nn", "sect2"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "nn", "sect2"), true, "Section Title: Sect2s") - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "en", "sect1"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "en", "sect1"), true, "Section Title: Section1") - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "en", "sect2"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "en", "sect2"), true, "Section Title: Section2") // Regular pages - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "en", "sect1", "regular1"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "en", "sect1", "regular1"), true, "Single Title: Page 01") - th.assertFileContent(t, fs, expectedFilePath(ugly, "public", "nn", "sect1", "regular2"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "nn", "sect1", "regular2"), true, "Single Title: Page 02") // RSS - th.assertFileContent(t, fs, filepath.Join("public", "nn", "customrss.xml"), true, "Hugo på norsk", "http://auth/bub/sect/doc1/", // Home page diff --git a/hugolib/template_engines_test.go b/hugolib/template_engines_test.go index e70104366..86e7f606a 100644 --- a/hugolib/template_engines_test.go +++ b/hugolib/template_engines_test.go @@ -84,9 +84,9 @@ p writeSource(t, fs, filepath.Join("layouts", "_default", fmt.Sprintf("single.%s", suffix)), templ) s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) - th := testHelper{s.Cfg} + th := testHelper{s.Cfg, s.Fs, t} - th.assertFileContent(t, fs, filepath.Join("public", "p", "index.html"), true, + th.assertFileContent(filepath.Join("public", "p", "index.html"), true, "Page Title: My Title", "My Content", "Hello World", diff --git a/hugolib/template_test.go b/hugolib/template_test.go index f9e14c8f9..5a6ce2f01 100644 --- a/hugolib/template_test.go +++ b/hugolib/template_test.go @@ -48,7 +48,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base: sect") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base: sect") }, }, { @@ -59,7 +59,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(t, fs, filepath.Join("public", "index.html"), false, "Base: index") + th.assertFileContent(filepath.Join("public", "index.html"), false, "Base: index") }, }, { @@ -70,7 +70,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base: list") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base: list") }, }, { @@ -81,7 +81,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base: list") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base: list") }, }, { @@ -94,7 +94,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base: sect") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base: sect") }, }, { @@ -106,7 +106,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base Theme: sect") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base Theme: sect") }, }, { @@ -119,7 +119,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base: list") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base: list") }, }, { @@ -131,13 +131,13 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(t, fs, filepath.Join("public", "sect", "index.html"), false, "Base Theme: list") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base Theme: list") }, }, } { cfg, fs = newTestCfg() - th = testHelper{cfg} + th = testHelper{cfg, fs, t} writeSource(t, fs, filepath.Join("content", "sect", "page.md"), `--- title: Template test diff --git a/hugolib/testhelpers_test.go b/hugolib/testhelpers_test.go index f0fcd9530..3c931ca48 100644 --- a/hugolib/testhelpers_test.go +++ b/hugolib/testhelpers_test.go @@ -4,7 +4,14 @@ import ( "path/filepath" "testing" + "regexp" + + "github.com/spf13/hugo/config" "github.com/spf13/hugo/deps" + + "fmt" + "strings" + "github.com/spf13/hugo/helpers" "github.com/spf13/hugo/source" "github.com/spf13/hugo/tpl" @@ -20,6 +27,31 @@ import ( "github.com/stretchr/testify/require" ) +type testHelper struct { + Cfg config.Provider + Fs *hugofs.Fs + T testing.TB +} + +func (th testHelper) assertFileContent(filename string, defaultInSubDir bool, matches ...string) { + filename = th.replaceDefaultContentLanguageValue(filename, defaultInSubDir) + content := readDestination(th.T, th.Fs, filename) + for _, match := range matches { + match = th.replaceDefaultContentLanguageValue(match, defaultInSubDir) + require.True(th.T, strings.Contains(content, match), fmt.Sprintf("File no match for\n%q in\n%q:\n%s", strings.Replace(match, "%", "%%", -1), filename, strings.Replace(content, "%", "%%", -1))) + } +} + +func (th testHelper) assertFileContentRegexp(filename string, defaultInSubDir bool, matches ...string) { + filename = th.replaceDefaultContentLanguageValue(filename, defaultInSubDir) + content := readDestination(th.T, th.Fs, filename) + for _, match := range matches { + match = th.replaceDefaultContentLanguageValue(match, defaultInSubDir) + r := regexp.MustCompile(match) + require.True(th.T, r.MatchString(content), fmt.Sprintf("File no match for\n%q in\n%q:\n%s", strings.Replace(match, "%", "%%", -1), filename, strings.Replace(content, "%", "%%", -1))) + } +} + func newTestPathSpec(fs *hugofs.Fs, v *viper.Viper) *helpers.PathSpec { l := helpers.NewDefaultLanguage(v) return helpers.NewPathSpec(fs, l)