Revert "refactor and clean up site tests"

This reverts commit 99e250917d.
This commit is contained in:
Bjørn Erik Pedersen 2015-06-21 15:01:06 +02:00
parent f25ce7fefa
commit 3eb301b57a
2 changed files with 129 additions and 104 deletions

View file

@ -1,80 +0,0 @@
package hugolib
import (
"testing"
"github.com/spf13/afero"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
"github.com/spf13/hugo/tpl"
"github.com/spf13/viper"
)
func siteFromByteSources(bs []source.ByteSource) *Site {
viper.SetDefault("baseurl", "http://auth/bub")
hugofs.DestinationFS = new(afero.MemMapFs)
s := &Site{
Source: &source.InMemorySource{ByteSource: bs},
}
s.initializeSiteInfo()
return s
}
func buildSiteFromByteSources(bs []source.ByteSource, t *testing.T) *Site {
s := siteFromByteSources(bs)
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)
}
return s
}
func setHugoDefaultTaxonomies() {
taxonomies := make(map[string]string)
taxonomies["tag"] = "tags"
taxonomies["category"] = "categories"
viper.Set("taxonomies", taxonomies)
}
func createAndRenderPages(t *testing.T, s *Site) {
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.RenderPages(); err != nil {
t.Fatalf("Unable to render pages. %s", err)
}
}
func testRenderPages(t *testing.T, s *Site) {
if err := s.RenderPages(); err != nil {
t.Fatalf("Unable to render pages. %s", err)
}
}
func templatePrep(s *Site) {
s.Tmpl = tpl.New()
s.Tmpl.LoadTemplates(s.absLayoutDir())
if s.hasTheme() {
s.Tmpl.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme")
}
}
func pageMust(p *Page, err error) *Page {
if err != nil {
panic(err)
}
return p
}

View file

@ -15,6 +15,7 @@ import (
"github.com/spf13/hugo/hugofs" "github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source" "github.com/spf13/hugo/source"
"github.com/spf13/hugo/target" "github.com/spf13/hugo/target"
"github.com/spf13/hugo/tpl"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -49,6 +50,35 @@ more text
` `
) )
func createAndRenderPages(t *testing.T, s *Site) {
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.RenderPages(); err != nil {
t.Fatalf("Unable to render pages. %s", err)
}
}
func templatePrep(s *Site) {
s.Tmpl = tpl.New()
s.Tmpl.LoadTemplates(s.absLayoutDir())
if s.hasTheme() {
s.Tmpl.LoadTemplatesWithPrefix(s.absThemeDir()+"/layouts", "theme")
}
}
func pageMust(p *Page, err error) *Page {
if err != nil {
panic(err)
}
return p
}
func TestDegenerateRenderThingMissingTemplate(t *testing.T) { func TestDegenerateRenderThingMissingTemplate(t *testing.T) {
p, _ := NewPageFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md") p, _ := NewPageFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
p.Convert() p.Convert()
@ -197,16 +227,30 @@ func TestDraftAndFutureRender(t *testing.T) {
{filepath.FromSlash("sect/doc4.md"), []byte("---\ntitle: doc4\ndraft: false\npublishdate: \"2012-05-29\"\n---\n# doc4\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 // Testing Defaults.. Only draft:true and publishDate in the past should be rendered
s := buildSiteFromByteSources(sources, t) s := siteSetup()
if len(s.Pages) != 1 { if len(s.Pages) != 1 {
t.Fatal("Draft or Future dated content published unexpectedly") t.Fatal("Draft or Future dated content published unexpectedly")
} }
// only publishDate in the past should be rendered // only publishDate in the past should be rendered
viper.Set("BuildDrafts", true) viper.Set("BuildDrafts", true)
s = siteSetup()
s = buildSiteFromByteSources(sources, t)
if len(s.Pages) != 2 { if len(s.Pages) != 2 {
t.Fatal("Future Dated Posts published unexpectedly") t.Fatal("Future Dated Posts published unexpectedly")
} }
@ -214,8 +258,7 @@ func TestDraftAndFutureRender(t *testing.T) {
// drafts should not be rendered, but all dates should // drafts should not be rendered, but all dates should
viper.Set("BuildDrafts", false) viper.Set("BuildDrafts", false)
viper.Set("BuildFuture", true) viper.Set("BuildFuture", true)
s = siteSetup()
s = buildSiteFromByteSources(sources, t)
if len(s.Pages) != 2 { if len(s.Pages) != 2 {
t.Fatal("Draft posts published unexpectedly") t.Fatal("Draft posts published unexpectedly")
} }
@ -223,11 +266,14 @@ func TestDraftAndFutureRender(t *testing.T) {
// all 4 should be included // all 4 should be included
viper.Set("BuildDrafts", true) viper.Set("BuildDrafts", true)
viper.Set("BuildFuture", true) viper.Set("BuildFuture", true)
s = siteSetup()
s = buildSiteFromByteSources(sources, t)
if len(s.Pages) != 4 { if len(s.Pages) != 4 {
t.Fatal("Drafts or Future posts not included as expected") t.Fatal("Drafts or Future posts not included as expected")
} }
//setting defaults back
viper.Set("BuildDrafts", false)
viper.Set("BuildFuture", false)
} }
// Issue #957 // Issue #957
@ -339,8 +385,12 @@ func doTest404ShouldAlwaysHaveUglyUrls(t *testing.T, uglyURLs bool) {
{filepath.FromSlash("sect/doc1.html"), []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, {filepath.FromSlash("sect/doc1.html"), []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")},
} }
s := buildSiteFromByteSources(sources, t) s := &Site{
Source: &source.InMemorySource{ByteSource: sources},
Targets: targetList{Page: &target.PagePub{UglyURLs: uglyURLs}},
}
s.initializeSiteInfo()
templatePrep(s) templatePrep(s)
must(s.addTemplate("index.html", "Home Sweet Home")) must(s.addTemplate("index.html", "Home Sweet Home"))
@ -351,8 +401,7 @@ func doTest404ShouldAlwaysHaveUglyUrls(t *testing.T, uglyURLs bool) {
must(s.addTemplate("rss.xml", "<root>RSS</root>")) must(s.addTemplate("rss.xml", "<root>RSS</root>"))
must(s.addTemplate("sitemap.xml", "<root>SITEMAP</root>")) must(s.addTemplate("sitemap.xml", "<root>SITEMAP</root>"))
testRenderPages(t, s) createAndRenderPages(t, s)
s.RenderHomePage() s.RenderHomePage()
s.RenderSitemap() s.RenderSitemap()
@ -478,6 +527,7 @@ func TestSkipRender(t *testing.T) {
viper.Reset() viper.Reset()
defer viper.Reset() defer viper.Reset()
hugofs.DestinationFS = new(afero.MemMapFs)
sources := []source.ByteSource{ sources := []source.ByteSource{
{filepath.FromSlash("sect/doc1.html"), []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, {filepath.FromSlash("sect/doc1.html"), []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")},
{filepath.FromSlash("sect/doc2.html"), []byte("<!doctype html><html><body>more content</body></html>")}, {filepath.FromSlash("sect/doc2.html"), []byte("<!doctype html><html><body>more content</body></html>")},
@ -492,17 +542,20 @@ func TestSkipRender(t *testing.T) {
viper.Set("DefaultExtension", "html") viper.Set("DefaultExtension", "html")
viper.Set("verbose", true) viper.Set("verbose", true)
viper.Set("CanonifyURLs", true) viper.Set("CanonifyURLs", true)
viper.Set("UglyURLs", true) viper.Set("baseurl", "http://auth/bub")
s := &Site{
s := buildSiteFromByteSources(sources, t) Source: &source.InMemorySource{ByteSource: sources},
Targets: targetList{Page: &target.PagePub{UglyURLs: true}},
}
s.initializeSiteInfo()
templatePrep(s) templatePrep(s)
must(s.addTemplate("_default/single.html", "{{.Content}}")) must(s.addTemplate("_default/single.html", "{{.Content}}"))
must(s.addTemplate("head", "<head><script src=\"script.js\"></script></head>")) must(s.addTemplate("head", "<head><script src=\"script.js\"></script></head>"))
must(s.addTemplate("head_abs", "<head><script src=\"/script.js\"></script></head>")) must(s.addTemplate("head_abs", "<head><script src=\"/script.js\"></script></head>"))
testRenderPages(t, s) createAndRenderPages(t, s)
tests := []struct { tests := []struct {
doc string doc string
@ -543,18 +596,26 @@ func TestAbsUrlify(t *testing.T) {
{filepath.FromSlash("sect/doc1.html"), []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")}, {filepath.FromSlash("sect/doc1.html"), []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")},
{filepath.FromSlash("content/blue/doc2.html"), []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")}, {filepath.FromSlash("content/blue/doc2.html"), []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")},
} }
viper.Set("UglyURLs", true)
for _, canonify := range []bool{true, false} { for _, canonify := range []bool{true, false} {
s := buildSiteFromByteSources(sources, t)
viper.Set("CanonifyURLs", canonify) viper.Set("CanonifyURLs", canonify)
viper.Set("BaseURL", "http://auth/bub")
s := &Site{
Source: &source.InMemorySource{ByteSource: sources},
Targets: targetList{Page: &target.PagePub{UglyURLs: true}},
}
t.Logf("Rendering with BaseURL %q and CanonifyURLs set %v", viper.GetString("baseURL"), canonify) t.Logf("Rendering with BaseURL %q and CanonifyURLs set %v", viper.GetString("baseURL"), canonify)
s.initializeSiteInfo()
templatePrep(s) templatePrep(s)
must(s.addTemplate("blue/single.html", TEMPLATE_WITH_URL_ABS)) must(s.addTemplate("blue/single.html", TEMPLATE_WITH_URL_ABS))
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.RenderPages(); err != nil { if err := s.RenderPages(); err != nil {
t.Fatalf("Unable to render pages. %s", err) t.Fatalf("Unable to render pages. %s", err)
} }
@ -636,7 +697,21 @@ func TestOrderedPages(t *testing.T) {
viper.Reset() viper.Reset()
defer viper.Reset() defer viper.Reset()
s := buildSiteFromByteSources(WEIGHTED_SOURCES, t) hugofs.DestinationFS = new(afero.MemMapFs)
viper.Set("baseurl", "http://auth/bub")
s := &Site{
Source: &source.InMemorySource{ByteSource: WEIGHTED_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.Sections["sect"][0].Weight != 2 || s.Sections["sect"][3].Weight != 6 { 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) t.Errorf("Pages in unexpected order. First should be '%d', got '%d'", 2, s.Sections["sect"][0].Weight)
@ -696,7 +771,21 @@ func TestGroupedPages(t *testing.T) {
} }
}() }()
s := buildSiteFromByteSources(GROUPED_SOURCES, t) hugofs.DestinationFS = new(afero.MemMapFs)
viper.Set("baseurl", "http://auth/bub")
s := &Site{
Source: &source.InMemorySource{ByteSource: GROUPED_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)
}
rbysection, err := s.Pages.GroupBy("Section", "desc") rbysection, err := s.Pages.GroupBy("Section", "desc")
if err != nil { if err != nil {
@ -862,15 +951,31 @@ func TestWeightedTaxonomies(t *testing.T) {
viper.Reset() viper.Reset()
defer viper.Reset() defer viper.Reset()
hugofs.DestinationFS = new(afero.MemMapFs)
sources := []source.ByteSource{ sources := []source.ByteSource{
{filepath.FromSlash("sect/doc1.md"), PAGE_WITH_WEIGHTED_TAXONOMIES_1}, {filepath.FromSlash("sect/doc1.md"), PAGE_WITH_WEIGHTED_TAXONOMIES_1},
{filepath.FromSlash("sect/doc2.md"), PAGE_WITH_WEIGHTED_TAXONOMIES_2}, {filepath.FromSlash("sect/doc2.md"), PAGE_WITH_WEIGHTED_TAXONOMIES_2},
{filepath.FromSlash("sect/doc3.md"), PAGE_WITH_WEIGHTED_TAXONOMIES_3}, {filepath.FromSlash("sect/doc3.md"), PAGE_WITH_WEIGHTED_TAXONOMIES_3},
} }
taxonomies := make(map[string]string)
setHugoDefaultTaxonomies() taxonomies["tag"] = "tags"
taxonomies["category"] = "categories"
s := buildSiteFromByteSources(sources, t) 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" { 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) t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Taxonomies["tags"]["a"][0].Page.Title)