From 07ab7ae3d2e64dafb8f6dab937986d8e366d1fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 17 Feb 2017 21:14:52 +0100 Subject: [PATCH] hugolib: More test helper cleanup --- hugolib/alias_test.go | 8 +- hugolib/case_insensitive_test.go | 8 +- hugolib/embedded_shortcodes_test.go | 16 ++-- hugolib/hugo_sites_build_test.go | 116 +++++++++++++--------------- hugolib/node_as_page_test.go | 116 ++++++++++++++-------------- hugolib/pagination_test.go | 2 +- hugolib/robotstxt_test.go | 2 +- hugolib/rss_test.go | 6 +- hugolib/shortcode_test.go | 2 +- hugolib/site_test.go | 6 +- hugolib/sitemap_test.go | 2 +- hugolib/template_engines_test.go | 2 +- hugolib/template_test.go | 16 ++-- hugolib/testhelpers_test.go | 31 ++++++-- 14 files changed, 171 insertions(+), 162 deletions(-) diff --git a/hugolib/alias_test.go b/hugolib/alias_test.go index 6aa0ca3dd..cd8ef3496 100644 --- a/hugolib/alias_test.go +++ b/hugolib/alias_test.go @@ -45,9 +45,9 @@ func TestAlias(t *testing.T) { buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) // the real page - th.assertFileContent(filepath.Join("public", "page", "index.html"), false, "For some moments the old man") + th.assertFileContent(filepath.Join("public", "page", "index.html"), "For some moments the old man") // the alias redirector - th.assertFileContent(filepath.Join("public", "foo", "bar", "index.html"), false, "`) + th.assertFileContentStraight("public/index.html", ``) } else { // should have redirect back to root - th.assertFileContent("public/fr/index.html", true, ``) + th.assertFileContentStraight("public/fr/index.html", ``) } - th.assertFileContent("public/fr/index.html", defaultInSubDir, "Home", "Bonjour") - th.assertFileContent("public/en/index.html", defaultInSubDir, "Home", "Hello") + th.assertFileContent("public/fr/index.html", "Home", "Bonjour") + th.assertFileContent("public/en/index.html", "Home", "Hello") // Check list pages - 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") + th.assertFileContent("public/fr/sect/index.html", "List", "Bonjour") + th.assertFileContent("public/en/sect/index.html", "List", "Hello") + th.assertFileContent("public/fr/plaques/frtag1/index.html", "List", "Bonjour") + th.assertFileContent("public/en/tags/tag1/index.html", "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("public/sitemap.xml", true, + th.assertFileContentStraight("public/sitemap.xml", "http://example.com/blog/en/sitemap.xml", "http://example.com/blog/fr/sitemap.xml") if defaultInSubDir { - th.assertFileContent("public/fr/sitemap.xml", true, "http://example.com/blog/fr/") + th.assertFileContentStraight("public/fr/sitemap.xml", "http://example.com/blog/fr/") } else { - th.assertFileContent("public/fr/sitemap.xml", true, "http://example.com/blog/") + th.assertFileContentStraight("public/fr/sitemap.xml", "http://example.com/blog/") } - th.assertFileContent("public/en/sitemap.xml", true, "http://example.com/blog/en/") + th.assertFileContent("public/en/sitemap.xml", "http://example.com/blog/en/") // Check rss - th.assertFileContent("public/fr/index.xml", defaultInSubDir, `Content!", "# Pages: 4", @@ -90,7 +90,7 @@ func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) { "GetPage: Section1 ", ) - th.assertFileContent(expectedFilePath(ugly, "public", "sect1", "regular1"), false, "Single Title: Page 01", "Content Page 01") + th.assertFileContent(expectedFilePath(ugly, "public", "sect1", "regular1"), "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(expectedFilePath(ugly, "public", "page", "2"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "page", "2"), "Pag: Page 02") // Check Sections - th.assertFileContent(expectedFilePath(ugly, "public", "sect1"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "sect1"), "Section Title: Section", "Section1 Content!", "Date: 2009-01-04", "Lastmod: 2009-01-05", ) - th.assertFileContent(expectedFilePath(ugly, "public", "sect2"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "sect2"), "Section Title: Section", "Section2 Content!", "Date: 2009-01-06", "Lastmod: 2009-01-07", ) // Check Sections paginator - th.assertFileContent(expectedFilePath(ugly, "public", "sect1", "page", "2"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "sect1", "page", "2"), "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(expectedFilePath(ugly, "public", "categories", "hugo"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "categories", "hugo"), "Taxonomy Title: Taxonomy Hugo", "Taxonomy Hugo Content!", "Date: 2009-01-08", "Lastmod: 2009-01-09", ) - th.assertFileContent(expectedFilePath(ugly, "public", "categories", "hugo-rocks"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "categories", "hugo-rocks"), "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(expectedFilePath(ugly, "public", "categories", "web"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "categories", "web"), "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(expectedFilePath(ugly, "public", "categories", "hugo", "page", "2"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "categories", "hugo", "page", "2"), "Taxonomy Title: Taxonomy Hugo", "Pag: Page 02") // Check taxonomy terms - th.assertFileContent(expectedFilePath(ugly, "public", "categories"), false, + th.assertFileContent(expectedFilePath(ugly, "public", "categories"), "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(filepath.Join("public", "customrss.xml"), false, "Recent content in Home Sweet Home! on Hugo Rocks", "Content!") - th.assertFileContent(filepath.Join("public", "de", "index.html"), true, + th.assertFileContent(filepath.Join("public", "de", "index.html"), "Index Title: Home Sweet Home!", "Content!") // Taxonomy list - th.assertFileContent(expectedFilePath(ugly, "public", "nn", "categories", "hugo"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "nn", "categories", "hugo"), "Taxonomy Title: Hugo") - th.assertFileContent(expectedFilePath(ugly, "public", "en", "categories", "hugo"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "en", "categories", "hugo"), "Taxonomy Title: Taxonomy Hugo") // Taxonomy terms - th.assertFileContent(expectedFilePath(ugly, "public", "nn", "categories"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "nn", "categories"), "Taxonomy Terms Title: Categories") - th.assertFileContent(expectedFilePath(ugly, "public", "en", "categories"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "en", "categories"), "Taxonomy Terms Title: Taxonomy Term Categories") // Sections - th.assertFileContent(expectedFilePath(ugly, "public", "nn", "sect1"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "nn", "sect1"), "Section Title: Sect1s") - th.assertFileContent(expectedFilePath(ugly, "public", "nn", "sect2"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "nn", "sect2"), "Section Title: Sect2s") - th.assertFileContent(expectedFilePath(ugly, "public", "en", "sect1"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "en", "sect1"), "Section Title: Section1") - th.assertFileContent(expectedFilePath(ugly, "public", "en", "sect2"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "en", "sect2"), "Section Title: Section2") // Regular pages - th.assertFileContent(expectedFilePath(ugly, "public", "en", "sect1", "regular1"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "en", "sect1", "regular1"), "Single Title: Page 01") - th.assertFileContent(expectedFilePath(ugly, "public", "nn", "sect1", "regular2"), true, + th.assertFileContent(expectedFilePath(ugly, "public", "nn", "sect1", "regular2"), "Single Title: Page 02") // RSS - th.assertFileContent(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 86e7f606a..1e6112729 100644 --- a/hugolib/template_engines_test.go +++ b/hugolib/template_engines_test.go @@ -86,7 +86,7 @@ p s := buildSingleSite(t, deps.DepsCfg{Fs: fs, Cfg: cfg}, BuildCfg{}) th := testHelper{s.Cfg, s.Fs, t} - th.assertFileContent(filepath.Join("public", "p", "index.html"), true, + th.assertFileContent(filepath.Join("public", "p", "index.html"), "Page Title: My Title", "My Content", "Hello World", diff --git a/hugolib/template_test.go b/hugolib/template_test.go index 5a6ce2f01..d69345817 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(filepath.Join("public", "sect", "index.html"), false, "Base: sect") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base: sect") }, }, { @@ -59,7 +59,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(filepath.Join("public", "index.html"), false, "Base: index") + th.assertFileContent(filepath.Join("public", "index.html"), "Base: index") }, }, { @@ -70,7 +70,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base: list") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base: list") }, }, { @@ -81,7 +81,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base: list") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base: list") }, }, { @@ -94,7 +94,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base: sect") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base: sect") }, }, { @@ -106,7 +106,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base Theme: sect") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base Theme: sect") }, }, { @@ -119,7 +119,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base: list") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base: list") }, }, { @@ -131,7 +131,7 @@ func TestBaseGoTemplate(t *testing.T) { }, func(t *testing.T) { - th.assertFileContent(filepath.Join("public", "sect", "index.html"), false, "Base Theme: list") + th.assertFileContent(filepath.Join("public", "sect", "index.html"), "Base Theme: list") }, }, } { diff --git a/hugolib/testhelpers_test.go b/hugolib/testhelpers_test.go index 3c931ca48..668b46b1d 100644 --- a/hugolib/testhelpers_test.go +++ b/hugolib/testhelpers_test.go @@ -33,25 +33,44 @@ type testHelper struct { T testing.TB } -func (th testHelper) assertFileContent(filename string, defaultInSubDir bool, matches ...string) { - filename = th.replaceDefaultContentLanguageValue(filename, defaultInSubDir) +func (th testHelper) assertFileContent(filename string, matches ...string) { + filename = th.replaceDefaultContentLanguageValue(filename) content := readDestination(th.T, th.Fs, filename) for _, match := range matches { - match = th.replaceDefaultContentLanguageValue(match, defaultInSubDir) + match = th.replaceDefaultContentLanguageValue(match) 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) +// TODO(bep) better name for this. It does no magic replacements depending on defaultontentLanguageInSubDir. +func (th testHelper) assertFileContentStraight(filename string, matches ...string) { 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, matches ...string) { + filename = th.replaceDefaultContentLanguageValue(filename) + content := readDestination(th.T, th.Fs, filename) + for _, match := range matches { + match = th.replaceDefaultContentLanguageValue(match) 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 (th testHelper) replaceDefaultContentLanguageValue(value string) string { + defaultInSubDir := th.Cfg.GetBool("defaultContentLanguageInSubDir") + replace := th.Cfg.GetString("defaultContentLanguage") + "/" + + if !defaultInSubDir { + value = strings.Replace(value, replace, "", 1) + + } + return value +} + func newTestPathSpec(fs *hugofs.Fs, v *viper.Viper) *helpers.PathSpec { l := helpers.NewDefaultLanguage(v) return helpers.NewPathSpec(fs, l)