Updating tests to use new Targets & Writers and switch to using Afero.MemMapFs for more accurate tests.

This commit is contained in:
spf13 2014-11-04 00:41:47 -05:00
parent 7b960ac121
commit 4dcf734acd
6 changed files with 81 additions and 75 deletions

View file

@ -5,8 +5,9 @@ import (
"testing" "testing"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/spf13/afero"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source" "github.com/spf13/hugo/source"
"github.com/spf13/hugo/target"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -283,11 +284,9 @@ func resetMenuTestState(state *testMenuState) {
} }
func createTestSite() *Site { func createTestSite() *Site {
files := make(map[string][]byte) hugofs.DestinationFS = new(afero.MemMapFs)
target := &target.InMemoryTarget{Files: files}
s := &Site{ s := &Site{
Target: target,
Source: &source.InMemorySource{ByteSource: MENU_PAGE_SOURCES}, Source: &source.InMemorySource{ByteSource: MENU_PAGE_SOURCES},
} }
return s return s

View file

@ -4,8 +4,10 @@ import (
"bytes" "bytes"
"testing" "testing"
"github.com/spf13/afero"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source" "github.com/spf13/hugo/source"
"github.com/spf13/hugo/target"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -31,11 +33,10 @@ const RSS_TEMPLATE = `<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom
</rss>` </rss>`
func TestRSSOutput(t *testing.T) { func TestRSSOutput(t *testing.T) {
files := make(map[string][]byte)
target := &target.InMemoryTarget{Files: files}
viper.Set("baseurl", "http://auth/bub/") viper.Set("baseurl", "http://auth/bub/")
hugofs.DestinationFS = new(afero.MemMapFs)
s := &Site{ s := &Site{
Target: target,
Source: &source.InMemorySource{ByteSource: WEIGHTED_SOURCES}, Source: &source.InMemorySource{ByteSource: WEIGHTED_SOURCES},
} }
s.initializeSiteInfo() s.initializeSiteInfo()
@ -55,12 +56,13 @@ func TestRSSOutput(t *testing.T) {
t.Fatalf("Unable to RenderHomePage: %s", err) t.Fatalf("Unable to RenderHomePage: %s", err)
} }
if _, ok := files[".xml"]; !ok { file, err := hugofs.DestinationFS.Open("rss.xml")
t.Errorf("Unable to locate: %s", ".xml")
t.Logf("%q", files) if err != nil {
t.Fatalf("Unable to locate: %s", "rss.xml")
} }
rss, _ := files[".xml"] rss := helpers.ReaderToBytes(file)
if !bytes.HasPrefix(rss, []byte("<?xml")) { if !bytes.HasPrefix(rss, []byte("<?xml")) {
t.Errorf("rss feed should start with <?xml. %s", rss) t.Errorf("rss feed should start with <?xml. %s", rss)
} }

View file

@ -76,9 +76,9 @@ func TestDegenerateNoTarget(t *testing.T) {
func TestFileTarget(t *testing.T) { func TestFileTarget(t *testing.T) {
s := &Site{ s := &Site{
Source: &source.InMemorySource{ByteSource: fakeSource}, Source: &source.InMemorySource{ByteSource: fakeSource},
Target: new(target.Filesystem),
Alias: new(target.HTMLRedirectAlias),
} }
s.AliasTarget()
s.PageTarget()
must(s.CreatePages()) must(s.CreatePages())
expected := "foo/bar/file.md (renderer: markdown)\n canonical => foo/bar/file/index.html\n\n" + expected := "foo/bar/file.md (renderer: markdown)\n canonical => foo/bar/file/index.html\n\n" +
"alias/test/file1.md (renderer: markdown)\n" + "alias/test/file1.md (renderer: markdown)\n" +
@ -90,12 +90,13 @@ func TestFileTarget(t *testing.T) {
checkShowPlanExpected(t, s, expected) checkShowPlanExpected(t, s, expected)
} }
func TestFileTargetUgly(t *testing.T) { func TestPageTargetUgly(t *testing.T) {
s := &Site{ s := &Site{
Target: &target.Filesystem{UglyUrls: true}, Targets: targetList{Page: &target.PagePub{UglyUrls: true}},
Source: &source.InMemorySource{ByteSource: fakeSource}, Source: &source.InMemorySource{ByteSource: fakeSource},
Alias: new(target.HTMLRedirectAlias),
} }
s.AliasTarget()
s.CreatePages() s.CreatePages()
expected := "foo/bar/file.md (renderer: markdown)\n canonical => foo/bar/file.html\n\n" + expected := "foo/bar/file.md (renderer: markdown)\n canonical => foo/bar/file.html\n\n" +
"alias/test/file1.md (renderer: markdown)\n" + "alias/test/file1.md (renderer: markdown)\n" +
@ -108,9 +109,12 @@ func TestFileTargetUgly(t *testing.T) {
func TestFileTargetPublishDir(t *testing.T) { func TestFileTargetPublishDir(t *testing.T) {
s := &Site{ s := &Site{
Target: &target.Filesystem{PublishDir: "../public"},
Targets: targetList{
Page: &target.PagePub{PublishDir: "../public"},
Alias: &target.HTMLRedirectAlias{PublishDir: "../public"},
},
Source: &source.InMemorySource{ByteSource: fakeSource}, Source: &source.InMemorySource{ByteSource: fakeSource},
Alias: &target.HTMLRedirectAlias{PublishDir: "../public"},
} }
must(s.CreatePages()) must(s.CreatePages())

View file

@ -8,7 +8,9 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/spf13/afero"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"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/viper" "github.com/spf13/viper"
@ -150,11 +152,8 @@ func TestRenderThingOrDefault(t *testing.T) {
{PAGE_SIMPLE_TITLE, false, TEMPLATE_FUNC, HTML("simple-template")}, {PAGE_SIMPLE_TITLE, false, TEMPLATE_FUNC, HTML("simple-template")},
} }
files := make(map[string][]byte) hugofs.DestinationFS = new(afero.MemMapFs)
target := &target.InMemoryTarget{Files: files} s := &Site{}
s := &Site{
Target: target,
}
s.prepTemplates() s.prepTemplates()
for i, test := range tests { for i, test := range tests {
@ -169,18 +168,26 @@ func TestRenderThingOrDefault(t *testing.T) {
} }
var err2 error var err2 error
var b io.Reader
if test.missing { if test.missing {
err2 = s.render("name", p, "out", "missing", templateName) b, err2 = s.renderPage("name", p, "missing", templateName)
} else { } else {
err2 = s.render("name", p, "out", templateName, "missing_default") b, err2 = s.renderPage("name", p, templateName, "missing_default")
} }
if err2 != nil { if err2 != nil {
t.Errorf("Unable to render html: %s", err) t.Errorf("Unable to render html: %s", err)
} }
if err2 := s.WriteDestPage("out", b); err2 != nil {
t.Errorf("Unable to write html: %s", err)
}
if string(files["out"]) != test.expected { file, err := hugofs.DestinationFS.Open("out/index.html")
t.Errorf("Content does not match. Expected '%s', got '%s'", test.expected, files["out"]) 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))
} }
} }
} }
@ -220,8 +227,7 @@ func TestTargetPath(t *testing.T) {
} }
func TestDraftAndFutureRender(t *testing.T) { func TestDraftAndFutureRender(t *testing.T) {
files := make(map[string][]byte) hugofs.DestinationFS = new(afero.MemMapFs)
target := &target.InMemoryTarget{Files: files}
sources := []source.ByteSource{ sources := []source.ByteSource{
{"sect/doc1.md", []byte("---\ntitle: doc1\ndraft: true\npublishdate: \"2414-05-29\"\n---\n# doc1\n*some content*")}, {"sect/doc1.md", []byte("---\ntitle: doc1\ndraft: true\npublishdate: \"2414-05-29\"\n---\n# doc1\n*some content*")},
{"sect/doc2.md", []byte("---\ntitle: doc2\ndraft: true\npublishdate: \"2012-05-29\"\n---\n# doc2\n*some content*")}, {"sect/doc2.md", []byte("---\ntitle: doc2\ndraft: true\npublishdate: \"2012-05-29\"\n---\n# doc2\n*some content*")},
@ -231,7 +237,6 @@ func TestDraftAndFutureRender(t *testing.T) {
siteSetup := func() *Site { siteSetup := func() *Site {
s := &Site{ s := &Site{
Target: target,
Source: &source.InMemorySource{ByteSource: sources}, Source: &source.InMemorySource{ByteSource: sources},
} }
@ -280,8 +285,7 @@ func TestDraftAndFutureRender(t *testing.T) {
} }
func TestSkipRender(t *testing.T) { func TestSkipRender(t *testing.T) {
files := make(map[string][]byte) hugofs.DestinationFS = new(afero.MemMapFs)
target := &target.InMemoryTarget{Files: files}
sources := []source.ByteSource{ sources := []source.ByteSource{
{"sect/doc1.html", []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")}, {"sect/doc1.html", []byte("---\nmarkup: markdown\n---\n# title\nsome *content*")},
{"sect/doc2.html", []byte("<!doctype html><html><body>more content</body></html>")}, {"sect/doc2.html", []byte("<!doctype html><html><body>more content</body></html>")},
@ -297,8 +301,8 @@ func TestSkipRender(t *testing.T) {
viper.Set("CanonifyUrls", true) viper.Set("CanonifyUrls", true)
viper.Set("baseurl", "http://auth/bub") viper.Set("baseurl", "http://auth/bub")
s := &Site{ s := &Site{
Target: target, Source: &source.InMemorySource{ByteSource: sources},
Source: &source.InMemorySource{ByteSource: sources}, Targets: targetList{Page: &target.PagePub{UglyUrls: true}},
} }
s.initializeSiteInfo() s.initializeSiteInfo()
@ -335,10 +339,11 @@ func TestSkipRender(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
content, ok := target.Files[test.doc] file, err := hugofs.DestinationFS.Open(test.doc)
if !ok { if err != nil {
t.Fatalf("Did not find %s in target. %v", test.doc, target.Files) t.Fatalf("Did not find %s in target.", test.doc)
} }
content := helpers.ReaderToBytes(file)
if !bytes.Equal(content, []byte(test.expected)) { if !bytes.Equal(content, []byte(test.expected)) {
t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, string(content)) t.Errorf("%s content expected:\n%q\ngot:\n%q", test.doc, test.expected, string(content))
@ -347,8 +352,7 @@ func TestSkipRender(t *testing.T) {
} }
func TestAbsUrlify(t *testing.T) { func TestAbsUrlify(t *testing.T) {
files := make(map[string][]byte) hugofs.DestinationFS = new(afero.MemMapFs)
target := &target.InMemoryTarget{Files: files}
sources := []source.ByteSource{ sources := []source.ByteSource{
{"sect/doc1.html", []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")}, {"sect/doc1.html", []byte("<!doctype html><html><head></head><body><a href=\"#frag1\">link</a></body></html>")},
{"content/blue/doc2.html", []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")}, {"content/blue/doc2.html", []byte("---\nf: t\n---\n<!doctype html><html><body>more content</body></html>")},
@ -357,8 +361,8 @@ func TestAbsUrlify(t *testing.T) {
viper.Set("CanonifyUrls", canonify) viper.Set("CanonifyUrls", canonify)
viper.Set("BaseUrl", "http://auth/bub") viper.Set("BaseUrl", "http://auth/bub")
s := &Site{ s := &Site{
Target: target, Source: &source.InMemorySource{ByteSource: sources},
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() s.initializeSiteInfo()
@ -385,10 +389,12 @@ func TestAbsUrlify(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
content, ok := target.Files[test.file]
if !ok { file, err := hugofs.DestinationFS.Open(test.file)
if err != nil {
t.Fatalf("Unable to locate rendered content: %s", test.file) t.Fatalf("Unable to locate rendered content: %s", test.file)
} }
content := helpers.ReaderToBytes(file)
expected := test.expected expected := test.expected
if !canonify { if !canonify {
@ -446,12 +452,10 @@ var WEIGHTED_SOURCES = []source.ByteSource{
} }
func TestOrderedPages(t *testing.T) { func TestOrderedPages(t *testing.T) {
files := make(map[string][]byte) hugofs.DestinationFS = new(afero.MemMapFs)
target := &target.InMemoryTarget{Files: files}
viper.Set("baseurl", "http://auth/bub") viper.Set("baseurl", "http://auth/bub")
s := &Site{ s := &Site{
Target: target,
Source: &source.InMemorySource{ByteSource: WEIGHTED_SOURCES}, Source: &source.InMemorySource{ByteSource: WEIGHTED_SOURCES},
} }
s.initializeSiteInfo() s.initializeSiteInfo()
@ -518,12 +522,11 @@ func TestGroupedPages(t *testing.T) {
fmt.Println("Recovered in f", r) fmt.Println("Recovered in f", r)
} }
}() }()
files := make(map[string][]byte)
target := &target.InMemoryTarget{Files: files} hugofs.DestinationFS = new(afero.MemMapFs)
viper.Set("baseurl", "http://auth/bub") viper.Set("baseurl", "http://auth/bub")
s := &Site{ s := &Site{
Target: target,
Source: &source.InMemorySource{ByteSource: GROUPED_SOURCES}, Source: &source.InMemorySource{ByteSource: GROUPED_SOURCES},
} }
s.initializeSiteInfo() s.initializeSiteInfo()
@ -537,6 +540,7 @@ func TestGroupedPages(t *testing.T) {
} }
rbysection, err := s.Pages.GroupBy("Section", "desc") rbysection, err := s.Pages.GroupBy("Section", "desc")
fmt.Println(rbysection)
if err != nil { if err != nil {
t.Fatalf("Unable to make PageGroup array: %s", err) t.Fatalf("Unable to make PageGroup array: %s", err)
} }
@ -697,8 +701,7 @@ date = 2010-05-27T07:32:00Z
Front Matter with weighted tags and categories`) Front Matter with weighted tags and categories`)
func TestWeightedTaxonomies(t *testing.T) { func TestWeightedTaxonomies(t *testing.T) {
files := make(map[string][]byte) hugofs.DestinationFS = new(afero.MemMapFs)
target := &target.InMemoryTarget{Files: files}
sources := []source.ByteSource{ sources := []source.ByteSource{
{"sect/doc1.md", PAGE_WITH_WEIGHTED_TAXONOMIES_1}, {"sect/doc1.md", PAGE_WITH_WEIGHTED_TAXONOMIES_1},
{"sect/doc2.md", PAGE_WITH_WEIGHTED_TAXONOMIES_2}, {"sect/doc2.md", PAGE_WITH_WEIGHTED_TAXONOMIES_2},
@ -712,7 +715,6 @@ func TestWeightedTaxonomies(t *testing.T) {
viper.Set("baseurl", "http://auth/bub") viper.Set("baseurl", "http://auth/bub")
viper.Set("taxonomies", taxonomies) viper.Set("taxonomies", taxonomies)
s := &Site{ s := &Site{
Target: target,
Source: &source.InMemorySource{ByteSource: sources}, Source: &source.InMemorySource{ByteSource: sources},
} }
s.initializeSiteInfo() s.initializeSiteInfo()

View file

@ -4,6 +4,8 @@ import (
"html/template" "html/template"
"testing" "testing"
"github.com/spf13/afero"
"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/viper" "github.com/spf13/viper"
@ -50,14 +52,10 @@ var urlFakeSource = []source.ByteSource{
} }
func TestPageCount(t *testing.T) { func TestPageCount(t *testing.T) {
files := make(map[string][]byte) hugofs.DestinationFS = new(afero.MemMapFs)
target := &target.InMemoryTarget{Files: files}
alias := &InMemoryAliasTarget{files: files}
viper.Set("uglyurls", false) viper.Set("uglyurls", false)
s := &Site{ s := &Site{
Target: target,
Alias: alias,
Source: &source.InMemorySource{ByteSource: urlFakeSource}, Source: &source.InMemorySource{ByteSource: urlFakeSource},
} }
s.initializeSiteInfo() s.initializeSiteInfo()
@ -79,15 +77,15 @@ func TestPageCount(t *testing.T) {
t.Errorf("Unable to render site lists: %s", err) t.Errorf("Unable to render site lists: %s", err)
} }
blueIndex := target.Files["blue"] _, err := hugofs.DestinationFS.Open("blue")
if blueIndex == nil { if err != nil {
t.Errorf("No indexed rendered. %v", target.Files) t.Errorf("No indexed rendered.")
} }
expected := ".." //expected := ".."
if string(blueIndex) != expected { //if string(blueIndex) != expected {
t.Errorf("Index template does not match expected: %q, got: %q", expected, string(blueIndex)) //t.Errorf("Index template does not match expected: %q, got: %q", expected, string(blueIndex))
} //}
for _, s := range []string{ for _, s := range []string{
"sd1/foo/index.html", "sd1/foo/index.html",
@ -95,7 +93,7 @@ func TestPageCount(t *testing.T) {
"sd3/index.html", "sd3/index.html",
"sd4.html", "sd4.html",
} { } {
if _, ok := target.Files[s]; !ok { if _, err := hugofs.DestinationFS.Open(s); err != nil {
t.Errorf("No alias rendered: %s", s) t.Errorf("No alias rendered: %s", s)
} }
} }

View file

@ -4,8 +4,10 @@ import (
"bytes" "bytes"
"testing" "testing"
"github.com/spf13/afero"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source" "github.com/spf13/hugo/source"
"github.com/spf13/hugo/target"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -21,13 +23,11 @@ const SITEMAP_TEMPLATE = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap
</urlset>` </urlset>`
func TestSitemapOutput(t *testing.T) { func TestSitemapOutput(t *testing.T) {
files := make(map[string][]byte) hugofs.DestinationFS = new(afero.MemMapFs)
target := &target.InMemoryTarget{Files: files}
viper.Set("baseurl", "http://auth/bub/") viper.Set("baseurl", "http://auth/bub/")
s := &Site{ s := &Site{
Target: target,
Source: &source.InMemorySource{ByteSource: WEIGHTED_SOURCES}, Source: &source.InMemorySource{ByteSource: WEIGHTED_SOURCES},
} }
@ -52,12 +52,13 @@ func TestSitemapOutput(t *testing.T) {
t.Fatalf("Unable to RenderSitemap: %s", err) t.Fatalf("Unable to RenderSitemap: %s", err)
} }
if _, ok := files["sitemap.xml"]; !ok { sitemapFile, err := hugofs.DestinationFS.Open("sitemap.xml")
t.Errorf("Unable to locate: sitemap.xml")
t.Logf("%q", files) if err != nil {
t.Fatalf("Unable to locate: sitemap.xml")
} }
sitemap, _ := files["sitemap.xml"] sitemap := helpers.ReaderToBytes(sitemapFile)
if !bytes.HasPrefix(sitemap, []byte("<?xml")) { if !bytes.HasPrefix(sitemap, []byte("<?xml")) {
t.Errorf("Sitemap file should start with <?xml. %s", sitemap) t.Errorf("Sitemap file should start with <?xml. %s", sitemap)
} }