Skip to content

Commit

Permalink
Revert "refactor and clean up site tests"
Browse files Browse the repository at this point in the history
This reverts commit 99e2509.
  • Loading branch information
bep committed Jun 21, 2015
1 parent f25ce7f commit 3eb301b
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 104 deletions.
80 changes: 0 additions & 80 deletions hugolib/helpers_for_test.go

This file was deleted.

153 changes: 129 additions & 24 deletions hugolib/site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source"
"github.com/spf13/hugo/target"
"github.com/spf13/hugo/tpl"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -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) {
p, _ := NewPageFrom(strings.NewReader(PAGE_SIMPLE_TITLE), "content/a/file.md")
p.Convert()
Expand Down Expand Up @@ -197,37 +227,53 @@ 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*")},
}

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 := buildSiteFromByteSources(sources, t)
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 = buildSiteFromByteSources(sources, t)
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 = buildSiteFromByteSources(sources, t)
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 = buildSiteFromByteSources(sources, t)
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)
}

// Issue #957
Expand Down Expand Up @@ -339,8 +385,12 @@ func doTest404ShouldAlwaysHaveUglyUrls(t *testing.T, uglyURLs bool) {
{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)

must(s.addTemplate("index.html", "Home Sweet Home"))
Expand All @@ -351,8 +401,7 @@ func doTest404ShouldAlwaysHaveUglyUrls(t *testing.T, uglyURLs bool) {
must(s.addTemplate("rss.xml", "<root>RSS</root>"))
must(s.addTemplate("sitemap.xml", "<root>SITEMAP</root>"))

testRenderPages(t, s)

createAndRenderPages(t, s)
s.RenderHomePage()
s.RenderSitemap()

Expand Down Expand Up @@ -478,6 +527,7 @@ func TestSkipRender(t *testing.T) {
viper.Reset()
defer viper.Reset()

hugofs.DestinationFS = new(afero.MemMapFs)
sources := []source.ByteSource{
{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>")},
Expand All @@ -492,17 +542,20 @@ func TestSkipRender(t *testing.T) {
viper.Set("DefaultExtension", "html")
viper.Set("verbose", true)
viper.Set("CanonifyURLs", true)
viper.Set("UglyURLs", true)

s := buildSiteFromByteSources(sources, t)
viper.Set("baseurl", "http://auth/bub")
s := &Site{
Source: &source.InMemorySource{ByteSource: sources},
Targets: targetList{Page: &target.PagePub{UglyURLs: true}},
}

s.initializeSiteInfo()
templatePrep(s)

must(s.addTemplate("_default/single.html", "{{.Content}}"))
must(s.addTemplate("head", "<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 {
doc string
Expand Down Expand Up @@ -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("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} {
s := buildSiteFromByteSources(sources, t)
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)

s.initializeSiteInfo()
templatePrep(s)
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 {
t.Fatalf("Unable to render pages. %s", err)
}
Expand Down Expand Up @@ -636,7 +697,21 @@ func TestOrderedPages(t *testing.T) {
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 {
t.Errorf("Pages in unexpected order. First should be '%d', got '%d'", 2, s.Sections["sect"][0].Weight)
Expand Down Expand Up @@ -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")
if err != nil {
Expand Down Expand Up @@ -862,15 +951,31 @@ func TestWeightedTaxonomies(t *testing.T) {
viper.Reset()
defer viper.Reset()

hugofs.DestinationFS = new(afero.MemMapFs)
sources := []source.ByteSource{
{filepath.FromSlash("sect/doc1.md"), PAGE_WITH_WEIGHTED_TAXONOMIES_1},
{filepath.FromSlash("sect/doc2.md"), PAGE_WITH_WEIGHTED_TAXONOMIES_2},
{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" {
t.Errorf("Pages in unexpected order, 'foo' expected first, got '%v'", s.Taxonomies["tags"]["a"][0].Page.Title)
Expand Down

0 comments on commit 3eb301b

Please sign in to comment.