2013-09-01 04:13:04 +00:00
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2013-09-01 14:43:29 +00:00
|
|
|
"bytes"
|
2013-09-05 05:28:59 +00:00
|
|
|
"github.com/spf13/hugo/source"
|
2013-09-04 03:52:50 +00:00
|
|
|
"github.com/spf13/hugo/target"
|
2013-09-05 05:28:59 +00:00
|
|
|
"testing"
|
2013-09-01 04:13:04 +00:00
|
|
|
)
|
|
|
|
|
2013-09-05 05:28:59 +00:00
|
|
|
var fakeSource = []struct {
|
|
|
|
name string
|
|
|
|
content []byte
|
|
|
|
}{
|
|
|
|
{"foo/bar/file.md", []byte(SIMPLE_PAGE)},
|
|
|
|
}
|
|
|
|
|
|
|
|
type inMemorySource struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i inMemorySource) Files() (files []*source.File) {
|
|
|
|
files = make([]*source.File, len(fakeSource))
|
|
|
|
for i, fake := range fakeSource {
|
|
|
|
files[i] = &source.File{
|
|
|
|
Name: fake.name,
|
|
|
|
Contents: bytes.NewReader(fake.content),
|
|
|
|
}
|
2013-09-01 14:43:29 +00:00
|
|
|
}
|
2013-09-05 05:28:59 +00:00
|
|
|
return
|
2013-09-01 14:43:29 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 05:28:59 +00:00
|
|
|
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 {
|
2013-09-05 05:28:59 +00:00
|
|
|
t.Fatalf("ShowPlan unexpectedly returned an error: %s", err)
|
2013-09-01 14:43:29 +00:00
|
|
|
}
|
|
|
|
got := out.String()
|
2013-09-05 05:28:59 +00:00
|
|
|
if got != expected {
|
|
|
|
t.Errorf("ShowPlan expected:\n%q\ngot\n%q", expected, got)
|
|
|
|
}
|
2013-09-01 14:43:29 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 05:28:59 +00:00
|
|
|
func TestDegenerateNoFiles(t *testing.T) {
|
|
|
|
checkShowPlanExpected(t, new(Site), "No source files provided.\n")
|
|
|
|
}
|
2013-09-01 14:43:29 +00:00
|
|
|
|
2013-09-05 05:28:59 +00:00
|
|
|
func TestDegenerateNoTarget(t *testing.T) {
|
|
|
|
s := &Site{Source: new(inMemorySource)}
|
|
|
|
must(s.CreatePages())
|
2013-09-04 03:52:50 +00:00
|
|
|
expected := "foo/bar/file.md\n canonical => !no target specified!\n"
|
2013-09-05 05:28:59 +00:00
|
|
|
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) {
|
2013-09-05 05:28:59 +00:00
|
|
|
s := &Site{
|
|
|
|
Source: new(inMemorySource),
|
|
|
|
Target: new(target.Filesystem),
|
|
|
|
}
|
|
|
|
must(s.CreatePages())
|
|
|
|
checkShowPlanExpected(t, s, "foo/bar/file.md\n canonical => foo/bar/file/index.html\n")
|
2013-09-04 03:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileTargetUgly(t *testing.T) {
|
2013-09-05 05:28:59 +00:00
|
|
|
s := &Site{
|
|
|
|
Target: &target.Filesystem{UglyUrls: true},
|
|
|
|
Source: new(inMemorySource),
|
|
|
|
}
|
|
|
|
s.CreatePages()
|
2013-09-04 03:52:50 +00:00
|
|
|
expected := "foo/bar/file.md\n canonical => foo/bar/file.html\n"
|
2013-09-05 05:28:59 +00:00
|
|
|
checkShowPlanExpected(t, s, expected)
|
2013-09-04 03:52:50 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 05:28:59 +00:00
|
|
|
func TestFileTargetPublishDir(t *testing.T) {
|
|
|
|
s := &Site{
|
|
|
|
Target: &target.Filesystem{PublishDir: "../public"},
|
|
|
|
Source: new(inMemorySource),
|
|
|
|
}
|
2013-09-04 03:52:50 +00:00
|
|
|
|
2013-09-05 05:28:59 +00:00
|
|
|
must(s.CreatePages())
|
|
|
|
expected := "foo/bar/file.md\n canonical => ../public/foo/bar/file/index.html\n"
|
|
|
|
checkShowPlanExpected(t, s, expected)
|
|
|
|
}
|