hugo/hugolib/site_show_plan_test.go

108 lines
2.9 KiB
Go
Raw Normal View History

2013-09-01 00:13:04 -04:00
package hugolib
import (
2013-09-01 10:43:29 -04:00
"bytes"
"github.com/spf13/hugo/source"
2013-09-03 23:52:50 -04:00
"github.com/spf13/hugo/target"
"testing"
2013-09-01 00:13:04 -04:00
)
2013-09-12 19:17:53 -04:00
const ALIAS_DOC_1 = "---\ntitle: alias doc\naliases:\n - \"alias1/\"\n - \"alias-2/\"\n---\naliases\n"
type byteSource struct {
name string
content []byte
2013-09-12 19:17:53 -04:00
}
var fakeSource = []byteSource{
{"foo/bar/file.md", []byte(SIMPLE_PAGE)},
2013-09-12 19:17:53 -04:00
{"alias/test/file1.md", []byte(ALIAS_DOC_1)},
//{"slug/test/file1.md", []byte(SLUG_DOC_1)},
}
type inMemorySource struct {
2013-09-12 19:17:53 -04:00
byteSource []byteSource
}
2013-09-12 19:17:53 -04:00
func (i *inMemorySource) Files() (files []*source.File) {
files = make([]*source.File, len(i.byteSource))
for i, fake := range i.byteSource {
files[i] = &source.File{
Name: fake.name,
Contents: bytes.NewReader(fake.content),
}
2013-09-01 10:43:29 -04:00
}
return
2013-09-01 10:43:29 -04:00
}
func checkShowPlanExpected(t *testing.T, s *Site, expected string) {
2013-09-01 10:43:29 -04:00
out := new(bytes.Buffer)
if err := s.ShowPlan(out); err != nil {
t.Fatalf("ShowPlan unexpectedly returned an error: %s", err)
2013-09-01 10:43:29 -04:00
}
got := out.String()
if got != expected {
t.Errorf("ShowPlan expected:\n%q\ngot\n%q", expected, got)
}
2013-09-01 10:43:29 -04:00
}
func TestDegenerateNoFiles(t *testing.T) {
checkShowPlanExpected(t, new(Site), "No source files provided.\n")
}
2013-09-01 10:43:29 -04:00
func TestDegenerateNoTarget(t *testing.T) {
2013-09-12 19:17:53 -04:00
s := &Site{
Source: &inMemorySource{fakeSource},
}
must(s.CreatePages())
2013-09-12 19:17:53 -04:00
expected := "foo/bar/file.md\n canonical => !no target specified!\n" +
"alias/test/file1.md\n canonical => !no target specified!\n"
checkShowPlanExpected(t, s, expected)
2013-09-01 00:13:04 -04:00
}
2013-09-03 23:52:50 -04:00
func TestFileTarget(t *testing.T) {
s := &Site{
2013-09-12 19:17:53 -04:00
Source: &inMemorySource{fakeSource},
Target: new(target.Filesystem),
2013-09-12 19:17:53 -04:00
Alias: new(target.HTMLRedirectAlias),
}
must(s.CreatePages())
2013-09-12 19:17:53 -04:00
expected := "foo/bar/file.md\n canonical => foo/bar/file/index.html\n" +
"alias/test/file1.md\n" +
" canonical => alias/test/file1/index.html\n" +
" alias1/ => alias1/index.html\n" +
" alias-2/ => alias-2/index.html\n"
checkShowPlanExpected(t, s, expected)
2013-09-03 23:52:50 -04:00
}
func TestFileTargetUgly(t *testing.T) {
s := &Site{
Target: &target.Filesystem{UglyUrls: true},
2013-09-12 19:17:53 -04:00
Source: &inMemorySource{fakeSource},
Alias: new(target.HTMLRedirectAlias),
}
s.CreatePages()
2013-09-12 19:17:53 -04:00
expected := "foo/bar/file.md\n canonical => foo/bar/file.html\n" +
"alias/test/file1.md\n" +
" canonical => alias/test/file1.html\n" +
" alias1/ => alias1/index.html\n" +
" alias-2/ => alias-2/index.html\n"
checkShowPlanExpected(t, s, expected)
2013-09-03 23:52:50 -04:00
}
func TestFileTargetPublishDir(t *testing.T) {
s := &Site{
Target: &target.Filesystem{PublishDir: "../public"},
2013-09-12 19:17:53 -04:00
Source: &inMemorySource{fakeSource},
Alias: &target.HTMLRedirectAlias{PublishDir: "../public"},
}
2013-09-03 23:52:50 -04:00
must(s.CreatePages())
2013-09-12 19:17:53 -04:00
expected := "foo/bar/file.md\n canonical => ../public/foo/bar/file/index.html\n" +
"alias/test/file1.md\n" +
" canonical => ../public/alias/test/file1/index.html\n" +
" alias1/ => ../public/alias1/index.html\n" +
" alias-2/ => ../public/alias-2/index.html\n"
checkShowPlanExpected(t, s, expected)
}