func TestDefaultHandler(t *testing.T) { testCommonResetState() hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc1.html"), Content: []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, {Name: filepath.FromSlash("sect/doc2.html"), Content: []byte("<!doctype html><html><body>more content</body></html>")}, {Name: filepath.FromSlash("sect/doc3.md"), Content: []byte("# doc3\n*some* content")}, {Name: filepath.FromSlash("sect/doc4.md"), Content: []byte("---\ntitle: doc4\n---\n# doc4\n*some content*")}, {Name: filepath.FromSlash("sect/doc3/img1.png"), Content: []byte("‰PNG ��� IHDR����������:~›U��� IDATWcø��ZMoñ����IEND®B`‚")}, {Name: filepath.FromSlash("sect/img2.gif"), Content: []byte("GIF89a��€��ÿÿÿ���,�������D�;")}, {Name: filepath.FromSlash("sect/img2.spf"), Content: []byte("****FAKE-FILETYPE****")}, {Name: filepath.FromSlash("doc7.html"), Content: []byte("<html><body>doc7 content</body></html>")}, {Name: filepath.FromSlash("sect/doc8.html"), Content: []byte("---\nmarkup: md\n---\n# title\nsome *content*")}, } viper.Set("DefaultExtension", "html") viper.Set("verbose", true) s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: true, PublishDir: "public"}}, Language: helpers.NewLanguage("en"), } if err := buildAndRenderSite(s, "_default/single.html", "{{.Content}}", "head", "<head><script src=\"script.js\"></script></head>", "head_abs", "<head><script src=\"/script.js\"></script></head>"); err != nil { t.Fatalf("Failed to render site: %s", err) } tests := []struct { doc string expected string }{ {filepath.FromSlash("public/sect/doc1.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, {filepath.FromSlash("public/sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"}, {filepath.FromSlash("public/sect/doc3.html"), "\n\n<h1 id=\"doc3\">doc3</h1>\n\n<p><em>some</em> content</p>\n"}, {filepath.FromSlash("public/sect/doc3/img1.png"), string([]byte("‰PNG ��� IHDR����������:~›U��� IDATWcø��ZMoñ����IEND®B`‚"))}, {filepath.FromSlash("public/sect/img2.gif"), string([]byte("GIF89a��€��ÿÿÿ���,�������D�;"))}, {filepath.FromSlash("public/sect/img2.spf"), string([]byte("****FAKE-FILETYPE****"))}, {filepath.FromSlash("public/doc7.html"), "<html><body>doc7 content</body></html>"}, {filepath.FromSlash("public/sect/doc8.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, } for _, test := range tests { file, err := hugofs.Destination().Open(test.doc) if err != nil { t.Fatalf("Did not find %s in target.", test.doc) } content := helpers.ReaderToString(file) if content != test.expected { t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) } } }
func TestRobotsTXTOutput(t *testing.T) { testCommonResetState() hugofs.InitMemFs() viper.Set("baseurl", "http://auth/bub/") viper.Set("enableRobotsTXT", true) s := &Site{ Source: &source.InMemorySource{ByteSource: weightedSources}, Language: helpers.NewDefaultLanguage(), } if err := buildAndRenderSite(s, "robots.txt", robotTxtTemplate); err != nil { t.Fatalf("Failed to build site: %s", err) } robotsFile, err := hugofs.Destination().Open("public/robots.txt") if err != nil { t.Fatalf("Unable to locate: robots.txt") } robots := helpers.ReaderToBytes(robotsFile) if !bytes.HasPrefix(robots, []byte("User-agent: Googlebot")) { t.Errorf("Robots file should start with 'User-agentL Googlebot'. %s", robots) } }
func TestDraftAndFutureRender(t *testing.T) { viper.Reset() defer viper.Reset() hugofs.InitMemFs() sources := []source.ByteSource{ {filepath.FromSlash("sect/doc1.md"), []byte("---\ntitle: doc1\ndraft: true\npublishdate: \"2414-05-29\"\n---\n# doc1\n*some content*")}, {filepath.FromSlash("sect/doc2.md"), []byte("---\ntitle: doc2\ndraft: true\npublishdate: \"2012-05-29\"\n---\n# doc2\n*some content*")}, {filepath.FromSlash("sect/doc3.md"), []byte("---\ntitle: doc3\ndraft: false\npublishdate: \"2414-05-29\"\n---\n# doc3\n*some content*")}, {filepath.FromSlash("sect/doc4.md"), []byte("---\ntitle: doc4\ndraft: false\npublishdate: \"2012-05-29\"\n---\n# doc4\n*some content*")}, } siteSetup := func() *Site { s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, } s.initializeSiteInfo() if err := s.createPages(); err != nil { t.Fatalf("Unable to create pages: %s", err) } return s } viper.Set("baseurl", "http://auth/bub") // Testing Defaults.. Only draft:true and publishDate in the past should be rendered s := siteSetup() if len(s.Pages) != 1 { t.Fatal("Draft or Future dated content published unexpectedly") } // only publishDate in the past should be rendered viper.Set("BuildDrafts", true) s = siteSetup() if len(s.Pages) != 2 { t.Fatal("Future Dated Posts published unexpectedly") } // drafts should not be rendered, but all dates should viper.Set("BuildDrafts", false) viper.Set("BuildFuture", true) s = siteSetup() if len(s.Pages) != 2 { t.Fatal("Draft posts published unexpectedly") } // all 4 should be included viper.Set("BuildDrafts", true) viper.Set("BuildFuture", true) s = siteSetup() if len(s.Pages) != 4 { t.Fatal("Drafts or Future posts not included as expected") } //setting defaults back viper.Set("BuildDrafts", false) viper.Set("BuildFuture", false) }
func TestOrderedPages(t *testing.T) { viper.Reset() defer viper.Reset() hugofs.InitMemFs() viper.Set("baseurl", "http://auth/bub") s := &Site{ Source: &source.InMemorySource{ByteSource: weightedSources}, } s.initializeSiteInfo() if err := s.createPages(); err != nil { t.Fatalf("Unable to create pages: %s", err) } if err := s.buildSiteMeta(); err != nil { t.Fatalf("Unable to build site metadata: %s", err) } if s.Sections["sect"][0].Weight != 2 || s.Sections["sect"][3].Weight != 6 { t.Errorf("Pages in unexpected order. First should be '%d', got '%d'", 2, s.Sections["sect"][0].Weight) } if s.Sections["sect"][1].Page.Title != "Three" || s.Sections["sect"][2].Page.Title != "Four" { t.Errorf("Pages in unexpected order. Second should be '%s', got '%s'", "Three", s.Sections["sect"][1].Page.Title) } bydate := s.Pages.ByDate() if bydate[0].Title != "One" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bydate[0].Title) } rev := bydate.Reverse() if rev[0].Title != "Three" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Three", rev[0].Title) } bypubdate := s.Pages.ByPublishDate() if bypubdate[0].Title != "One" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bypubdate[0].Title) } rbypubdate := bypubdate.Reverse() if rbypubdate[0].Title != "Three" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Three", rbypubdate[0].Title) } bylength := s.Pages.ByLength() if bylength[0].Title != "One" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bylength[0].Title) } rbylength := bylength.Reverse() if rbylength[0].Title != "Four" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Four", rbylength[0].Title) } }
func TestDoNewSite_noerror_base_exists_but_empty(t *testing.T) { basepath := filepath.Join(os.TempDir(), "blog") hugofs.InitMemFs() hugofs.Source().MkdirAll(basepath, 777) err := doNewSite(basepath, false) assert.Nil(t, err) }
func TestPageCount(t *testing.T) { testCommonResetState() hugofs.InitMemFs() viper.Set("uglyURLs", false) viper.Set("paginate", 10) s := &Site{ Source: &source.InMemorySource{ByteSource: urlFakeSource}, Language: helpers.NewDefaultLanguage(), } if err := buildAndRenderSite(s, "indexes/blue.html", indexTemplate); err != nil { t.Fatalf("Failed to build site: %s", err) } _, err := hugofs.Destination().Open("public/blue") if err != nil { t.Errorf("No indexed rendered.") } for _, s := range []string{ "public/sd1/foo/index.html", "public/sd2/index.html", "public/sd3/index.html", "public/sd4.html", } { if _, err := hugofs.Destination().Open(filepath.FromSlash(s)); err != nil { t.Errorf("No alias rendered: %s", s) } } }
func TestDraftAndFutureRender(t *testing.T) { testCommonResetState() hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc1.md"), Content: []byte("---\ntitle: doc1\ndraft: true\npublishdate: \"2414-05-29\"\n---\n# doc1\n*some content*")}, {Name: filepath.FromSlash("sect/doc2.md"), Content: []byte("---\ntitle: doc2\ndraft: true\npublishdate: \"2012-05-29\"\n---\n# doc2\n*some content*")}, {Name: filepath.FromSlash("sect/doc3.md"), Content: []byte("---\ntitle: doc3\ndraft: false\npublishdate: \"2414-05-29\"\n---\n# doc3\n*some content*")}, {Name: filepath.FromSlash("sect/doc4.md"), Content: []byte("---\ntitle: doc4\ndraft: false\npublishdate: \"2012-05-29\"\n---\n# doc4\n*some content*")}, } siteSetup := func(t *testing.T) *Site { s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, Language: helpers.NewDefaultLanguage(), } if err := buildSiteSkipRender(s); err != nil { t.Fatalf("Failed to build site: %s", err) } return s } viper.Set("baseURL", "http://auth/bub") // Testing Defaults.. Only draft:true and publishDate in the past should be rendered s := siteSetup(t) if len(s.AllPages) != 1 { t.Fatal("Draft or Future dated content published unexpectedly") } // only publishDate in the past should be rendered viper.Set("buildDrafts", true) s = siteSetup(t) if len(s.AllPages) != 2 { t.Fatal("Future Dated Posts published unexpectedly") } // drafts should not be rendered, but all dates should viper.Set("buildDrafts", false) viper.Set("buildFuture", true) s = siteSetup(t) if len(s.AllPages) != 2 { t.Fatal("Draft posts published unexpectedly") } // all 4 should be included viper.Set("buildDrafts", true) viper.Set("buildFuture", true) s = siteSetup(t) if len(s.AllPages) != 4 { t.Fatal("Drafts or Future posts not included as expected") } //setting defaults back viper.Set("buildDrafts", false) viper.Set("buildFuture", false) }
func TestSkipRender(t *testing.T) { testCommonResetState() hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc1.html"), Content: []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, {Name: filepath.FromSlash("sect/doc2.html"), Content: []byte("<!doctype html><html><body>more content</body></html>")}, {Name: filepath.FromSlash("sect/doc3.md"), Content: []byte("# doc3\n*some* content")}, {Name: filepath.FromSlash("sect/doc4.md"), Content: []byte("---\ntitle: doc4\n---\n# doc4\n*some content*")}, {Name: filepath.FromSlash("sect/doc5.html"), Content: []byte("<!doctype html><html>{{ template \"head\" }}<body>body5</body></html>")}, {Name: filepath.FromSlash("sect/doc6.html"), Content: []byte("<!doctype html><html>{{ template \"head_abs\" }}<body>body5</body></html>")}, {Name: filepath.FromSlash("doc7.html"), Content: []byte("<html><body>doc7 content</body></html>")}, {Name: filepath.FromSlash("sect/doc8.html"), Content: []byte("---\nmarkup: md\n---\n# title\nsome *content*")}, } viper.Set("defaultExtension", "html") viper.Set("verbose", true) viper.Set("canonifyURLs", true) viper.Set("baseURL", "http://auth/bub") s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: true}}, Language: helpers.NewDefaultLanguage(), } if err := buildAndRenderSite(s, "_default/single.html", "{{.Content}}", "head", "<head><script src=\"script.js\"></script></head>", "head_abs", "<head><script src=\"/script.js\"></script></head>"); err != nil { t.Fatalf("Failed to build site: %s", err) } tests := []struct { doc string expected string }{ {filepath.FromSlash("sect/doc1.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, {filepath.FromSlash("sect/doc2.html"), "<!doctype html><html><body>more content</body></html>"}, {filepath.FromSlash("sect/doc3.html"), "\n\n<h1 id=\"doc3\">doc3</h1>\n\n<p><em>some</em> content</p>\n"}, {filepath.FromSlash("sect/doc4.html"), "\n\n<h1 id=\"doc4\">doc4</h1>\n\n<p><em>some content</em></p>\n"}, {filepath.FromSlash("sect/doc5.html"), "<!doctype html><html><head><script src=\"script.js\"></script></head><body>body5</body></html>"}, {filepath.FromSlash("sect/doc6.html"), "<!doctype html><html><head><script src=\"http://auth/bub/script.js\"></script></head><body>body5</body></html>"}, {filepath.FromSlash("doc7.html"), "<html><body>doc7 content</body></html>"}, {filepath.FromSlash("sect/doc8.html"), "\n\n<h1 id=\"title\">title</h1>\n\n<p>some <em>content</em></p>\n"}, } for _, test := range tests { file, err := hugofs.Destination().Open(test.doc) if err != nil { t.Fatalf("Did not find %s in target.", test.doc) } content := helpers.ReaderToString(file) if content != test.expected { t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, content) } } }
// Issue #957 func TestCrossrefs(t *testing.T) { hugofs.InitMemFs() for _, uglyURLs := range []bool{true, false} { for _, relative := range []bool{true, false} { doTestCrossrefs(t, relative, uglyURLs) } } }
func TestDoNewSite(t *testing.T) { basepath := filepath.Join(os.TempDir(), "blog") hugofs.InitMemFs() err := doNewSite(basepath, false) assert.Nil(t, err) checkNewSiteInited(basepath, t) }
func TestDoNewSite_error_force_dir_inside_exists(t *testing.T) { basepath := filepath.Join(os.TempDir(), "blog") contentPath := filepath.Join(basepath, "content") hugofs.InitMemFs() hugofs.Source().MkdirAll(contentPath, 777) err := doNewSite(basepath, true) assert.NotNil(t, err) }
func createTestSite(pageSources []source.ByteSource) *Site { hugofs.InitMemFs() s := &Site{ Source: &source.InMemorySource{ByteSource: pageSources}, } return s }
func TestAbsURLify(t *testing.T) { testCommonResetState() viper.Set("defaultExtension", "html") hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc1.html"), Content: []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")}, {Name: filepath.FromSlash("blue/doc2.html"), Content: []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")}, } for _, baseURL := range []string{"http://auth/bub", "http://base", "//base"} { for _, canonify := range []bool{true, false} { viper.Set("canonifyURLs", canonify) viper.Set("baseURL", baseURL) s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, targets: targetList{page: &target.PagePub{UglyURLs: true}}, Language: helpers.NewDefaultLanguage(), } t.Logf("Rendering with baseURL %q and canonifyURLs set %v", viper.GetString("baseURL"), canonify) if err := buildAndRenderSite(s, "blue/single.html", templateWithURLAbs); err != nil { t.Fatalf("Failed to build site: %s", err) } tests := []struct { file, expected string }{ {"blue/doc2.html", "<a href=\"%s/foobar.jpg\">Going</a>"}, {"sect/doc1.html", "<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>"}, } for _, test := range tests { file, err := hugofs.Destination().Open(filepath.FromSlash(test.file)) if err != nil { t.Fatalf("Unable to locate rendered content: %s", test.file) } content := helpers.ReaderToString(file) expected := test.expected if strings.Contains(expected, "%s") { expected = fmt.Sprintf(expected, baseURL) } if !canonify { expected = strings.Replace(expected, baseURL, "", -1) } if content != expected { t.Errorf("AbsURLify with baseURL %q content expected:\n%q\ngot\n%q", baseURL, expected, content) } } } } }
func TestDoNewSite_error_base_exists(t *testing.T) { basepath := filepath.Join(os.TempDir(), "blog") hugofs.InitMemFs() hugofs.Source().MkdirAll(basepath, 777) hugofs.Source().Create(filepath.Join(basepath, "foo")) // Since the directory already exists and isn't empty, expect an error err := doNewSite(basepath, false) assert.NotNil(t, err) }
func TestDoNewSite_force_empty_dir(t *testing.T) { basepath := filepath.Join(os.TempDir(), "blog") hugofs.InitMemFs() hugofs.Source().MkdirAll(basepath, 777) err := doNewSite(basepath, true) assert.Nil(t, err) checkNewSiteInited(basepath, t) }
func createTestSite(pageSources []source.ByteSource) *Site { hugofs.InitMemFs() return &Site{ Source: &source.InMemorySource{ByteSource: pageSources}, Language: helpers.NewDefaultLanguage(), } }
func TestOrderedPages(t *testing.T) { testCommonResetState() hugofs.InitMemFs() viper.Set("baseURL", "http://auth/bub") s := &Site{ Source: &source.InMemorySource{ByteSource: weightedSources}, Language: helpers.NewDefaultLanguage(), } if err := buildSiteSkipRender(s); err != nil { t.Fatalf("Failed to process site: %s", err) } if s.Sections["sect"][0].Weight != 2 || s.Sections["sect"][3].Weight != 6 { t.Errorf("Pages in unexpected order. First should be '%d', got '%d'", 2, s.Sections["sect"][0].Weight) } if s.Sections["sect"][1].Page.Title != "Three" || s.Sections["sect"][2].Page.Title != "Four" { t.Errorf("Pages in unexpected order. Second should be '%s', got '%s'", "Three", s.Sections["sect"][1].Page.Title) } bydate := s.Pages.ByDate() if bydate[0].Title != "One" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bydate[0].Title) } rev := bydate.Reverse() if rev[0].Title != "Three" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Three", rev[0].Title) } bypubdate := s.Pages.ByPublishDate() if bypubdate[0].Title != "One" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bypubdate[0].Title) } rbypubdate := bypubdate.Reverse() if rbypubdate[0].Title != "Three" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Three", rbypubdate[0].Title) } bylength := s.Pages.ByLength() if bylength[0].Title != "One" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "One", bylength[0].Title) } rbylength := bylength.Reverse() if rbylength[0].Title != "Four" { t.Errorf("Pages in unexpected order. First should be '%s', got '%s'", "Four", rbylength[0].Title) } }
func testCommonResetState() { hugofs.InitMemFs() viper.Reset() viper.SetFs(hugofs.Source()) loadDefaultSettings() // Default is false, but true is easier to use as default in tests viper.Set("DefaultContentLanguageInSubdir", true) if err := hugofs.Source().Mkdir("content", 0755); err != nil { panic("Content folder creation failed.") } }
func TestPageCount(t *testing.T) { viper.Reset() defer viper.Reset() hugofs.InitMemFs() viper.Set("uglyurls", false) viper.Set("paginate", 10) s := &Site{ Source: &source.InMemorySource{ByteSource: urlFakeSource}, } s.initializeSiteInfo() s.prepTemplates("indexes/blue.html", indexTemplate) if err := s.createPages(); err != nil { t.Errorf("Unable to create pages: %s", err) } if err := s.buildSiteMeta(); err != nil { t.Errorf("Unable to build site metadata: %s", err) } if err := s.renderSectionLists(); err != nil { t.Errorf("Unable to render section lists: %s", err) } if err := s.renderAliases(); err != nil { t.Errorf("Unable to render site lists: %s", err) } _, err := hugofs.Destination().Open("blue") if err != nil { t.Errorf("No indexed rendered.") } //expected := ".." //if string(blueIndex) != expected { //t.Errorf("Index template does not match expected: %q, got: %q", expected, string(blueIndex)) //} for _, s := range []string{ "sd1/foo/index.html", "sd2/index.html", "sd3/index.html", "sd4.html", } { if _, err := hugofs.Destination().Open(filepath.FromSlash(s)); err != nil { t.Errorf("No alias rendered: %s", s) } } }
func TestRobotsTXTOutput(t *testing.T) { viper.Reset() defer viper.Reset() hugofs.InitMemFs() viper.Set("baseurl", "http://auth/bub/") viper.Set("enableRobotsTXT", true) s := &Site{ Source: &source.InMemorySource{ByteSource: weightedSources}, } s.initializeSiteInfo() s.prepTemplates("robots.txt", robotTxtTemplate) if err := s.createPages(); err != nil { t.Fatalf("Unable to create pages: %s", err) } if err := s.buildSiteMeta(); err != nil { t.Fatalf("Unable to build site metadata: %s", err) } if err := s.renderHomePage(); err != nil { t.Fatalf("Unable to RenderHomePage: %s", err) } if err := s.renderSitemap(); err != nil { t.Fatalf("Unable to RenderSitemap: %s", err) } if err := s.renderRobotsTXT(); err != nil { t.Fatalf("Unable to RenderRobotsTXT :%s", err) } robotsFile, err := hugofs.Destination().Open("robots.txt") if err != nil { t.Fatalf("Unable to locate: robots.txt") } robots := helpers.ReaderToBytes(robotsFile) if !bytes.HasPrefix(robots, []byte("User-agent: Googlebot")) { t.Errorf("Robots file should start with 'User-agentL Googlebot'. %s", robots) } }
func TestRenderThingOrDefault(t *testing.T) { tests := []struct { missing bool template string expected string }{ {true, templateTitle, HTML("simple template")}, {true, templateFunc, HTML("simple-template")}, {false, templateTitle, HTML("simple template")}, {false, templateFunc, HTML("simple-template")}, } hugofs.InitMemFs() for i, test := range tests { s := &Site{} p, err := NewPageFrom(strings.NewReader(pageSimpleTitle), "content/a/file.md") if err != nil { t.Fatalf("Error parsing buffer: %s", err) } templateName := fmt.Sprintf("default%d", i) s.prepTemplates(templateName, test.template) var err2 error if test.missing { err2 = s.renderAndWritePage("name", "out", p, "missing", templateName) } else { err2 = s.renderAndWritePage("name", "out", p, templateName, "missing_default") } if err2 != nil { t.Errorf("Unable to render html: %s", err) } file, err := hugofs.Destination().Open(filepath.FromSlash("out/index.html")) if err != nil { t.Errorf("Unable to open html: %s", err) } if helpers.ReaderToString(file) != test.expected { t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, helpers.ReaderToString(file)) } } }
func TestSitemapOutput(t *testing.T) { viper.Reset() defer viper.Reset() hugofs.InitMemFs() viper.Set("baseurl", "http://auth/bub/") s := &Site{ Source: &source.InMemorySource{ByteSource: weightedSources}, } s.initializeSiteInfo() s.prepTemplates("sitemap.xml", SITEMAP_TEMPLATE) if err := s.createPages(); err != nil { t.Fatalf("Unable to create pages: %s", err) } if err := s.buildSiteMeta(); err != nil { t.Fatalf("Unable to build site metadata: %s", err) } if err := s.renderHomePage(); err != nil { t.Fatalf("Unable to RenderHomePage: %s", err) } if err := s.renderSitemap(); err != nil { t.Fatalf("Unable to RenderSitemap: %s", err) } if err := s.renderRobotsTXT(); err != nil { t.Fatalf("Unable to RenderRobotsTXT :%s", err) } sitemapFile, err := hugofs.Destination().Open("sitemap.xml") if err != nil { t.Fatalf("Unable to locate: sitemap.xml") } sitemap := helpers.ReaderToBytes(sitemapFile) if !bytes.HasPrefix(sitemap, []byte("<?xml")) { t.Errorf("Sitemap file should start with <?xml. %s", sitemap) } }
func setupLinkingMockSite(t *testing.T) *Site { hugofs.InitMemFs() sources := []source.ByteSource{ {filepath.FromSlash("index.md"), []byte("")}, {filepath.FromSlash("rootfile.md"), []byte("")}, {filepath.FromSlash("root-image.png"), []byte("")}, {filepath.FromSlash("level2/2-root.md"), []byte("")}, {filepath.FromSlash("level2/index.md"), []byte("")}, {filepath.FromSlash("level2/common.md"), []byte("")}, // {filepath.FromSlash("level2b/2b-root.md"), []byte("")}, // {filepath.FromSlash("level2b/index.md"), []byte("")}, // {filepath.FromSlash("level2b/common.md"), []byte("")}, {filepath.FromSlash("level2/2-image.png"), []byte("")}, {filepath.FromSlash("level2/common.png"), []byte("")}, {filepath.FromSlash("level2/level3/3-root.md"), []byte("")}, {filepath.FromSlash("level2/level3/index.md"), []byte("")}, {filepath.FromSlash("level2/level3/common.md"), []byte("")}, {filepath.FromSlash("level2/level3/3-image.png"), []byte("")}, {filepath.FromSlash("level2/level3/common.png"), []byte("")}, } viper.Set("baseurl", "http://auth/") viper.Set("DefaultExtension", "html") viper.Set("UglyURLs", false) viper.Set("PluralizeListTitles", false) viper.Set("CanonifyURLs", false) viper.Set("blackfriday", map[string]interface{}{ "sourceRelativeLinksProjectFolder": "/docs"}) site := &Site{ Source: &source.InMemorySource{ByteSource: sources}, } site.initializeSiteInfo() if err := site.createPages(); err != nil { t.Fatalf("Unable to create pages: %s", err) } return site }
func setupLinkingMockSite(t *testing.T) *Site { hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("index.md"), Content: []byte("")}, {Name: filepath.FromSlash("rootfile.md"), Content: []byte("")}, {Name: filepath.FromSlash("root-image.png"), Content: []byte("")}, {Name: filepath.FromSlash("level2/2-root.md"), Content: []byte("")}, {Name: filepath.FromSlash("level2/index.md"), Content: []byte("")}, {Name: filepath.FromSlash("level2/common.md"), Content: []byte("")}, // {Name: filepath.FromSlash("level2b/2b-root.md"), Content: []byte("")}, // {Name: filepath.FromSlash("level2b/index.md"), Content: []byte("")}, // {Name: filepath.FromSlash("level2b/common.md"), Content: []byte("")}, {Name: filepath.FromSlash("level2/2-image.png"), Content: []byte("")}, {Name: filepath.FromSlash("level2/common.png"), Content: []byte("")}, {Name: filepath.FromSlash("level2/level3/3-root.md"), Content: []byte("")}, {Name: filepath.FromSlash("level2/level3/index.md"), Content: []byte("")}, {Name: filepath.FromSlash("level2/level3/common.md"), Content: []byte("")}, {Name: filepath.FromSlash("level2/level3/3-image.png"), Content: []byte("")}, {Name: filepath.FromSlash("level2/level3/common.png"), Content: []byte("")}, } viper.Set("baseurl", "http://auth/") viper.Set("DefaultExtension", "html") viper.Set("UglyURLs", false) viper.Set("PluralizeListTitles", false) viper.Set("CanonifyURLs", false) viper.Set("blackfriday", // TODO(bep) see https://github.com/spf13/viper/issues/261 map[string]interface{}{ strings.ToLower("sourceRelativeLinksProjectFolder"): "/docs"}) site := &Site{ Source: &source.InMemorySource{ByteSource: sources}, Language: helpers.NewDefaultLanguage(), } if err := buildSiteSkipRender(site); err != nil { t.Fatalf("Failed to build site: %s", err) } return site }
func TestWeightedTaxonomies(t *testing.T) { viper.Reset() defer viper.Reset() hugofs.InitMemFs() sources := []source.ByteSource{ {filepath.FromSlash("sect/doc1.md"), pageWithWeightedTaxonomies2}, {filepath.FromSlash("sect/doc2.md"), pageWithWeightedTaxonomies1}, {filepath.FromSlash("sect/doc3.md"), pageWithWeightedTaxonomies3}, } taxonomies := make(map[string]string) taxonomies["tag"] = "tags" taxonomies["category"] = "categories" viper.Set("baseurl", "http://auth/bub") viper.Set("taxonomies", taxonomies) s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, } s.initializeSiteInfo() if err := s.createPages(); err != nil { t.Fatalf("Unable to create pages: %s", err) } if err := s.buildSiteMeta(); err != nil { t.Fatalf("Unable to build site metadata: %s", err) } if s.Taxonomies["tags"]["a"][0].Page.Title != "foo" { t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Taxonomies["tags"]["a"][0].Page.Title) } if s.Taxonomies["categories"]["d"][0].Page.Title != "bar" { t.Errorf("Pages in unexpected order, 'bar' expected first, got '%v'", s.Taxonomies["categories"]["d"][0].Page.Title) } if s.Taxonomies["categories"]["e"][0].Page.Title != "bza" { t.Errorf("Pages in unexpected order, 'bza' expected first, got '%v'", s.Taxonomies["categories"]["e"][0].Page.Title) } }
func TestFutureExpirationRender(t *testing.T) { viper.Reset() defer viper.Reset() hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc3.md"), Content: []byte("---\ntitle: doc1\nexpirydate: \"2400-05-29\"\n---\n# doc1\n*some content*")}, {Name: filepath.FromSlash("sect/doc4.md"), Content: []byte("---\ntitle: doc2\nexpirydate: \"2000-05-29\"\n---\n# doc2\n*some content*")}, } siteSetup := func() *Site { s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, } s.initializeSiteInfo() if err := s.createPages(); err != nil { t.Fatalf("Unable to create pages: %s", err) } return s } viper.Set("baseurl", "http://auth/bub") s := siteSetup() if len(s.Pages) != 1 { if len(s.Pages) > 1 { t.Fatal("Expired content published unexpectedly") } if len(s.Pages) < 1 { t.Fatal("Valid content expired unexpectedly") } } if s.Pages[0].Title == "doc2" { t.Fatal("Expired content published unexpectedly") } }
func TestFutureExpirationRender(t *testing.T) { testCommonResetState() hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc3.md"), Content: []byte("---\ntitle: doc1\nexpirydate: \"2400-05-29\"\n---\n# doc1\n*some content*")}, {Name: filepath.FromSlash("sect/doc4.md"), Content: []byte("---\ntitle: doc2\nexpirydate: \"2000-05-29\"\n---\n# doc2\n*some content*")}, } siteSetup := func(t *testing.T) *Site { s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, Language: helpers.NewDefaultLanguage(), } if err := buildSiteSkipRender(s); err != nil { t.Fatalf("Failed to build site: %s", err) } return s } viper.Set("baseURL", "http://auth/bub") s := siteSetup(t) if len(s.AllPages) != 1 { if len(s.AllPages) > 1 { t.Fatal("Expired content published unexpectedly") } if len(s.AllPages) < 1 { t.Fatal("Valid content expired unexpectedly") } } if s.AllPages[0].Title == "doc2" { t.Fatal("Expired content published unexpectedly") } }
func TestRSSOutput(t *testing.T) { viper.Reset() defer viper.Reset() rssURI := "customrss.xml" viper.Set("baseurl", "http://auth/bub/") viper.Set("RSSUri", rssURI) hugofs.InitMemFs() s := &Site{ Source: &source.InMemorySource{ByteSource: weightedSources}, } s.initializeSiteInfo() s.prepTemplates("rss.xml", rssTemplate) if err := s.createPages(); err != nil { t.Fatalf("Unable to create pages: %s", err) } if err := s.buildSiteMeta(); err != nil { t.Fatalf("Unable to build site metadata: %s", err) } if err := s.renderHomePage(); err != nil { t.Fatalf("Unable to RenderHomePage: %s", err) } file, err := hugofs.Destination().Open(rssURI) if err != nil { t.Fatalf("Unable to locate: %s", rssURI) } rss := helpers.ReaderToBytes(file) if !bytes.HasPrefix(rss, []byte("<?xml")) { t.Errorf("rss feed should start with <?xml. %s", rss) } }
func TestWeightedTaxonomies(t *testing.T) { testCommonResetState() hugofs.InitMemFs() sources := []source.ByteSource{ {Name: filepath.FromSlash("sect/doc1.md"), Content: pageWithWeightedTaxonomies2}, {Name: filepath.FromSlash("sect/doc2.md"), Content: pageWithWeightedTaxonomies1}, {Name: filepath.FromSlash("sect/doc3.md"), Content: pageWithWeightedTaxonomies3}, } taxonomies := make(map[string]string) taxonomies["tag"] = "tags" taxonomies["category"] = "categories" viper.Set("baseURL", "http://auth/bub") viper.Set("taxonomies", taxonomies) s := &Site{ Source: &source.InMemorySource{ByteSource: sources}, Language: helpers.NewDefaultLanguage(), } if err := buildSiteSkipRender(s); err != nil { t.Fatalf("Failed to process site: %s", err) } if s.Taxonomies["tags"]["a"][0].Page.Title != "foo" { t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Taxonomies["tags"]["a"][0].Page.Title) } if s.Taxonomies["categories"]["d"][0].Page.Title != "bar" { t.Errorf("Pages in unexpected order, 'bar' expected first, got '%v'", s.Taxonomies["categories"]["d"][0].Page.Title) } if s.Taxonomies["categories"]["e"][0].Page.Title != "bza" { t.Errorf("Pages in unexpected order, 'bza' expected first, got '%v'", s.Taxonomies["categories"]["e"][0].Page.Title) } }
func TestGroupedPages(t *testing.T) { viper.Reset() defer viper.Reset() defer func() { if r := recover(); r != nil { fmt.Println("Recovered in f", r) } }() hugofs.InitMemFs() viper.Set("baseurl", "http://auth/bub") s := &Site{ Source: &source.InMemorySource{ByteSource: groupedSources}, } s.initializeSiteInfo() if err := s.createPages(); err != nil { t.Fatalf("Unable to create pages: %s", err) } if err := s.buildSiteMeta(); err != nil { t.Fatalf("Unable to build site metadata: %s", err) } rbysection, err := s.Pages.GroupBy("Section", "desc") if err != nil { t.Fatalf("Unable to make PageGroup array: %s", err) } if rbysection[0].Key != "sect3" { t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "sect3", rbysection[0].Key) } if rbysection[1].Key != "sect2" { t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "sect2", rbysection[1].Key) } if rbysection[2].Key != "sect1" { t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "sect1", rbysection[2].Key) } if rbysection[0].Pages[0].Title != "Four" { t.Errorf("PageGroup has an unexpected page. First group's pages should have '%s', got '%s'", "Four", rbysection[0].Pages[0].Title) } if len(rbysection[2].Pages) != 2 { t.Errorf("PageGroup has unexpected number of pages. Third group should have '%d' pages, got '%d' pages", 2, len(rbysection[2].Pages)) } bytype, err := s.Pages.GroupBy("Type", "asc") if err != nil { t.Fatalf("Unable to make PageGroup array: %s", err) } if bytype[0].Key != "sect1" { t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "sect1", bytype[0].Key) } if bytype[1].Key != "sect2" { t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "sect2", bytype[1].Key) } if bytype[2].Key != "sect3" { t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "sect3", bytype[2].Key) } if bytype[2].Pages[0].Title != "Four" { t.Errorf("PageGroup has an unexpected page. Third group's data should have '%s', got '%s'", "Four", bytype[0].Pages[0].Title) } if len(bytype[0].Pages) != 2 { t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(bytype[2].Pages)) } bydate, err := s.Pages.GroupByDate("2006-01", "asc") if err != nil { t.Fatalf("Unable to make PageGroup array: %s", err) } if bydate[0].Key != "0001-01" { t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "0001-01", bydate[0].Key) } if bydate[1].Key != "2012-01" { t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "2012-01", bydate[1].Key) } if bydate[2].Key != "2012-04" { t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "2012-04", bydate[2].Key) } if bydate[2].Pages[0].Title != "Three" { t.Errorf("PageGroup has an unexpected page. Third group's pages should have '%s', got '%s'", "Three", bydate[2].Pages[0].Title) } if len(bydate[0].Pages) != 2 { t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(bydate[2].Pages)) } bypubdate, err := s.Pages.GroupByPublishDate("2006") if err != nil { t.Fatalf("Unable to make PageGroup array: %s", err) } if bypubdate[0].Key != "2012" { t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "2012", bypubdate[0].Key) } if bypubdate[1].Key != "0001" { t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "0001", bypubdate[1].Key) } if bypubdate[0].Pages[0].Title != "Three" { t.Errorf("PageGroup has an unexpected page. Third group's pages should have '%s', got '%s'", "Three", bypubdate[0].Pages[0].Title) } if len(bypubdate[0].Pages) != 3 { t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 3, len(bypubdate[0].Pages)) } byparam, err := s.Pages.GroupByParam("my_param", "desc") if err != nil { t.Fatalf("Unable to make PageGroup array: %s", err) } if byparam[0].Key != "foo" { t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "foo", byparam[0].Key) } if byparam[1].Key != "baz" { t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "baz", byparam[1].Key) } if byparam[2].Key != "bar" { t.Errorf("PageGroup array in unexpected order. Third group key should be '%s', got '%s'", "bar", byparam[2].Key) } if byparam[2].Pages[0].Title != "Three" { t.Errorf("PageGroup has an unexpected page. Third group's pages should have '%s', got '%s'", "Three", byparam[2].Pages[0].Title) } if len(byparam[0].Pages) != 2 { t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(byparam[0].Pages)) } _, err = s.Pages.GroupByParam("not_exist") if err == nil { t.Errorf("GroupByParam didn't return an expected error") } byOnlyOneParam, err := s.Pages.GroupByParam("only_one") if err != nil { t.Fatalf("Unable to make PageGroup array: %s", err) } if len(byOnlyOneParam) != 1 { t.Errorf("PageGroup array has unexpected elements. Group length should be '%d', got '%d'", 1, len(byOnlyOneParam)) } if byOnlyOneParam[0].Key != "yes" { t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "yes", byOnlyOneParam[0].Key) } byParamDate, err := s.Pages.GroupByParamDate("my_date", "2006-01") if err != nil { t.Fatalf("Unable to make PageGroup array: %s", err) } if byParamDate[0].Key != "2010-05" { t.Errorf("PageGroup array in unexpected order. First group key should be '%s', got '%s'", "2010-05", byParamDate[0].Key) } if byParamDate[1].Key != "1979-05" { t.Errorf("PageGroup array in unexpected order. Second group key should be '%s', got '%s'", "1979-05", byParamDate[1].Key) } if byParamDate[1].Pages[0].Title != "One" { t.Errorf("PageGroup has an unexpected page. Second group's pages should have '%s', got '%s'", "One", byParamDate[1].Pages[0].Title) } if len(byParamDate[0].Pages) != 2 { t.Errorf("PageGroup has unexpected number of pages. First group should have '%d' pages, got '%d' pages", 2, len(byParamDate[2].Pages)) } }