hugo/hugolib/site_show_plan_test.go

125 lines
3.7 KiB
Go
Raw Normal View History

2013-09-01 04:13:04 +00:00
package hugolib
import (
2013-09-01 14:43:29 +00:00
"bytes"
"strings"
"testing"
"github.com/spf13/hugo/source"
2013-09-04 03:52:50 +00:00
"github.com/spf13/hugo/target"
2013-09-01 04:13:04 +00:00
)
2013-09-12 23:17:53 +00:00
const ALIAS_DOC_1 = "---\ntitle: alias doc\naliases:\n - \"alias1/\"\n - \"alias-2/\"\n---\naliases\n"
var fakeSource = []source.ByteSource{
{
Name: "foo/bar/file.md",
Content: []byte(SIMPLE_PAGE),
},
{
Name: "alias/test/file1.md",
Content: []byte(ALIAS_DOC_1),
},
{
Name: "section/somecontent.html",
Content: []byte(RENDER_NO_FRONT_MATTER),
},
2013-09-01 14:43:29 +00:00
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func checkShowPlanExpected(t *testing.T, s *Site, expected string) {
2013-09-01 14:43:29 +00:00
out := new(bytes.Buffer)
if err := s.ShowPlan(out); err != nil {
t.Fatalf("ShowPlan unexpectedly returned an error: %s", err)
2013-09-01 14:43:29 +00:00
}
got := out.String()
gotList := strings.Split(got, "\n")
expectedList := strings.Split(expected, "\n")
for _, x := range gotList {
if !stringInSlice(x, expectedList) {
t.Errorf("ShowPlan expected:\n%q\ngot\n%q", expected, got)
}
}
for _, x := range expectedList {
if !stringInSlice(x, gotList) {
t.Errorf("ShowPlan expected:\n%q\ngot\n%q", expected, got)
}
}
2013-09-01 14:43:29 +00:00
}
func TestDegenerateNoFiles(t *testing.T) {
checkShowPlanExpected(t, new(Site), "No source files provided.\n")
}
2013-09-01 14:43:29 +00:00
func TestDegenerateNoTarget(t *testing.T) {
2013-09-12 23:17:53 +00:00
s := &Site{
2014-03-01 05:31:21 +00:00
Source: &source.InMemorySource{ByteSource: fakeSource},
2013-09-12 23:17:53 +00:00
}
must(s.CreatePages())
2013-09-18 18:52:30 +00:00
expected := "foo/bar/file.md (renderer: markdown)\n canonical => !no target specified!\n\n" +
"alias/test/file1.md (renderer: markdown)\n canonical => !no target specified!\n\n" +
"section/somecontent.html (renderer: n/a)\n canonical => !no target specified!\n\n"
checkShowPlanExpected(t, s, expected)
2013-09-01 04:13:04 +00:00
}
2013-09-04 03:52:50 +00:00
func TestFileTarget(t *testing.T) {
s := &Site{
2014-03-01 05:31:21 +00:00
Source: &source.InMemorySource{ByteSource: fakeSource},
Target: new(target.Filesystem),
2013-09-12 23:17:53 +00:00
Alias: new(target.HTMLRedirectAlias),
}
must(s.CreatePages())
2013-09-18 18:52:30 +00:00
expected := "foo/bar/file.md (renderer: markdown)\n canonical => foo/bar/file/index.html\n\n" +
"alias/test/file1.md (renderer: markdown)\n" +
2013-09-12 23:17:53 +00:00
" canonical => alias/test/file1/index.html\n" +
" alias1/ => alias1/index.html\n" +
2013-09-18 18:52:30 +00:00
" alias-2/ => alias-2/index.html\n\n" +
"section/somecontent.html (renderer: n/a)\n canonical => section/somecontent/index.html\n\n"
2013-09-12 23:17:53 +00:00
checkShowPlanExpected(t, s, expected)
2013-09-04 03:52:50 +00:00
}
func TestFileTargetUgly(t *testing.T) {
s := &Site{
Target: &target.Filesystem{UglyUrls: true},
2014-03-01 05:31:21 +00:00
Source: &source.InMemorySource{ByteSource: fakeSource},
2013-09-12 23:17:53 +00:00
Alias: new(target.HTMLRedirectAlias),
}
s.CreatePages()
2013-09-18 18:52:30 +00:00
expected := "foo/bar/file.md (renderer: markdown)\n canonical => foo/bar/file.html\n\n" +
"alias/test/file1.md (renderer: markdown)\n" +
2013-09-12 23:17:53 +00:00
" canonical => alias/test/file1.html\n" +
" alias1/ => alias1/index.html\n" +
2013-09-18 18:52:30 +00:00
" alias-2/ => alias-2/index.html\n\n" +
"section/somecontent.html (renderer: n/a)\n canonical => section/somecontent.html\n\n"
checkShowPlanExpected(t, s, expected)
2013-09-04 03:52:50 +00:00
}
func TestFileTargetPublishDir(t *testing.T) {
s := &Site{
Target: &target.Filesystem{PublishDir: "../public"},
2014-03-01 05:31:21 +00:00
Source: &source.InMemorySource{ByteSource: fakeSource},
2013-09-12 23:17:53 +00:00
Alias: &target.HTMLRedirectAlias{PublishDir: "../public"},
}
2013-09-04 03:52:50 +00:00
must(s.CreatePages())
2013-09-18 18:52:30 +00:00
expected := "foo/bar/file.md (renderer: markdown)\n canonical => ../public/foo/bar/file/index.html\n\n" +
"alias/test/file1.md (renderer: markdown)\n" +
2013-09-12 23:17:53 +00:00
" canonical => ../public/alias/test/file1/index.html\n" +
" alias1/ => ../public/alias1/index.html\n" +
2013-09-18 18:52:30 +00:00
" alias-2/ => ../public/alias-2/index.html\n\n" +
"section/somecontent.html (renderer: n/a)\n canonical => ../public/section/somecontent/index.html\n\n"
checkShowPlanExpected(t, s, expected)
}