2012-12-23 03:51:14 +00:00
|
|
|
|
package smplt_test
|
|
|
|
|
|
|
|
|
|
import (
|
2012-12-24 05:12:11 +00:00
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"go/parser"
|
|
|
|
|
"go/token"
|
2012-12-25 15:19:54 +00:00
|
|
|
|
"log"
|
2012-12-23 04:32:26 +00:00
|
|
|
|
"mime"
|
2012-12-24 05:12:11 +00:00
|
|
|
|
"os"
|
2012-12-25 15:19:54 +00:00
|
|
|
|
"os/exec"
|
2012-12-24 05:12:11 +00:00
|
|
|
|
"path"
|
2012-12-25 19:22:21 +00:00
|
|
|
|
"sort"
|
2012-12-25 15:19:54 +00:00
|
|
|
|
"strings"
|
2012-12-23 03:51:14 +00:00
|
|
|
|
"testing"
|
2012-12-24 05:12:11 +00:00
|
|
|
|
"time"
|
2012-12-23 03:51:14 +00:00
|
|
|
|
|
|
|
|
|
. "github.com/meatballhat/box-o-sand/gotime/smplt"
|
|
|
|
|
)
|
|
|
|
|
|
2012-12-23 04:32:26 +00:00
|
|
|
|
const (
|
|
|
|
|
BASIC_RENDERED_TXT_SIMPLATE = `
|
2012-12-23 03:51:14 +00:00
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Dance struct {
|
|
|
|
|
Who string
|
|
|
|
|
When time.Time
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-25 05:06:55 +00:00
|
|
|
|
ctx["D"] = &Dance{
|
2012-12-23 04:32:26 +00:00
|
|
|
|
Who: "Everybody",
|
2012-12-23 03:51:14 +00:00
|
|
|
|
When: time.Now(),
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-23 16:07:23 +00:00
|
|
|
|
{{.D.Who}} Dance {{.D.When}}!
|
2012-12-23 03:51:14 +00:00
|
|
|
|
`
|
2012-12-23 04:32:26 +00:00
|
|
|
|
BASIC_STATIC_TXT_SIMPLATE = `
|
2012-12-23 03:51:14 +00:00
|
|
|
|
Everybody Dance Now!
|
|
|
|
|
`
|
2012-12-23 04:32:26 +00:00
|
|
|
|
BASIC_JSON_SIMPLATE = `
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Dance struct {
|
|
|
|
|
Who string
|
|
|
|
|
When time.Time
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-25 05:06:55 +00:00
|
|
|
|
ctx["D"] = &Dance{
|
2012-12-23 04:32:26 +00:00
|
|
|
|
Who: "Everybody",
|
|
|
|
|
When: time.Now(),
|
2012-12-25 05:06:55 +00:00
|
|
|
|
response.SetBody(ctx["D"])
|
2012-12-23 04:32:26 +00:00
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
BASIC_NEGOTIATED_SIMPLATE = `
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Dance struct {
|
|
|
|
|
Who string
|
|
|
|
|
When time.Time
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-25 05:06:55 +00:00
|
|
|
|
ctx["D"] = &Dance{
|
2012-12-23 04:32:26 +00:00
|
|
|
|
Who: "Everybody",
|
|
|
|
|
When: time.Now(),
|
|
|
|
|
}
|
|
|
|
|
text/plain
|
2012-12-23 16:07:23 +00:00
|
|
|
|
{{.D.Who}} Dance {{.D.When}}!
|
2012-12-23 04:32:26 +00:00
|
|
|
|
|
|
|
|
|
application/json
|
2012-12-23 16:07:23 +00:00
|
|
|
|
{"who":"{{.D.Who}}","when":"{{.D.When}}"}
|
2012-12-23 04:32:26 +00:00
|
|
|
|
`
|
|
|
|
|
)
|
|
|
|
|
|
2012-12-24 05:12:11 +00:00
|
|
|
|
var (
|
|
|
|
|
tmpdir = path.Join(os.TempDir(),
|
|
|
|
|
fmt.Sprintf("smplt_test-%d", time.Now().UTC().UnixNano()))
|
2012-12-25 19:22:21 +00:00
|
|
|
|
smpltgenDir = path.Join(tmpdir, "src", "smpltgen")
|
|
|
|
|
testSiteRoot = path.Join(tmpdir, "test-site")
|
|
|
|
|
goCmd string
|
|
|
|
|
noCleanup bool
|
|
|
|
|
testSiteFiles = map[string]string{
|
|
|
|
|
"hams/bone/derp": BASIC_NEGOTIATED_SIMPLATE,
|
|
|
|
|
"shill/cans.txt": BASIC_RENDERED_TXT_SIMPLATE,
|
|
|
|
|
"hat/v.json": BASIC_JSON_SIMPLATE,
|
|
|
|
|
"silmarillion.handlebar.mustache.moniker.html": "<html>INVALID AS BUTT</html>",
|
|
|
|
|
"Big CMS/Owns_UR Contents/flurb.txt": BASIC_STATIC_TXT_SIMPLATE,
|
|
|
|
|
}
|
2012-12-24 05:12:11 +00:00
|
|
|
|
)
|
|
|
|
|
|
2012-12-25 15:19:54 +00:00
|
|
|
|
func init() {
|
|
|
|
|
err := os.Setenv("GOPATH", strings.Join([]string{tmpdir, os.Getenv("GOPATH")}, ":"))
|
|
|
|
|
if err != nil {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
if noCleanup {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
} else {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2012-12-25 15:19:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd, err := exec.LookPath("go")
|
|
|
|
|
if err != nil {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
if noCleanup {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
} else {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2012-12-25 15:19:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
goCmdAddr := &goCmd
|
|
|
|
|
*goCmdAddr = cmd
|
2012-12-25 19:22:21 +00:00
|
|
|
|
|
|
|
|
|
noCleanupAddr := &noCleanup
|
|
|
|
|
*noCleanupAddr = len(os.Getenv("SMPLT_TEST_NOCLEANUP")) > 0
|
2012-12-25 15:19:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-24 05:12:11 +00:00
|
|
|
|
func mkTmpDir() {
|
2012-12-25 15:19:54 +00:00
|
|
|
|
err := os.MkdirAll(smpltgenDir, os.ModeDir|os.ModePerm)
|
2012-12-24 05:12:11 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func rmTmpDir() {
|
|
|
|
|
err := os.RemoveAll(tmpdir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-25 19:22:21 +00:00
|
|
|
|
func mkTestSite() string {
|
|
|
|
|
mkTmpDir()
|
|
|
|
|
|
|
|
|
|
for filePath, content := range testSiteFiles {
|
|
|
|
|
fullPath := path.Join(testSiteRoot, filePath)
|
|
|
|
|
err := os.MkdirAll(path.Dir(fullPath), os.ModeDir|os.ModePerm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f, err := os.Create(fullPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = f.WriteString(content)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return testSiteRoot
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-25 15:19:54 +00:00
|
|
|
|
func writeRenderedTemplate() (string, error) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
|
2012-12-25 15:19:54 +00:00
|
|
|
|
outfileName := path.Join(smpltgenDir, s.OutputName())
|
|
|
|
|
outf, err := os.Create(outfileName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return outfileName, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.Execute(outf)
|
|
|
|
|
err = outf.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return outfileName, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return outfileName, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func runGoCommandOnSmpltgen(command string) error {
|
|
|
|
|
cmd := exec.Command(goCmd, command, "smpltgen")
|
|
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
cmd.Stderr = &out
|
|
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-25 19:22:21 +00:00
|
|
|
|
if noCleanup {
|
|
|
|
|
log.Println(out.String())
|
|
|
|
|
}
|
2012-12-25 15:19:54 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func formatRenderedTemplate() error {
|
|
|
|
|
return runGoCommandOnSmpltgen("fmt")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func buildRenderedTemplate() error {
|
|
|
|
|
return runGoCommandOnSmpltgen("install")
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-23 04:32:26 +00:00
|
|
|
|
func TestSimplateKnowsItsFilename(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("hasty-decisions.txt", "herpherpderpherp")
|
2012-12-23 04:32:26 +00:00
|
|
|
|
if s.Filename != "hasty-decisions.txt" {
|
|
|
|
|
t.Errorf("Simplate filename incorrectly assigned as %s instead of %s",
|
|
|
|
|
s.Filename, "hasty-decisions.txt")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSimplateKnowsItsContentType(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("hasty-decisions.js", "function herp() { return 'derp'; }")
|
2012-12-23 04:32:26 +00:00
|
|
|
|
expected := mime.TypeByExtension(".js")
|
|
|
|
|
|
|
|
|
|
if s.ContentType != expected {
|
|
|
|
|
t.Errorf("Simplate content type incorrectly assigned as %s instead of %s",
|
|
|
|
|
s.ContentType, expected)
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-23 03:51:14 +00:00
|
|
|
|
|
2012-12-24 17:03:21 +00:00
|
|
|
|
func TestStaticSimplateKnowsItsOutputName(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("nothing.txt", "foo\nham\n")
|
2012-12-24 17:03:21 +00:00
|
|
|
|
if s.OutputName() != "nothing.txt" {
|
|
|
|
|
t.Errorf("Static simplate output name is wrong!: %v", s.OutputName())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRenderedSimplateKnowsItsOutputName(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("flip/dippy slippy/snork.d/basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
|
2012-12-24 17:03:21 +00:00
|
|
|
|
if s.OutputName() != "flip-SLASH-dippy-SPACE-slippy-SLASH-snork-DOT-d-SLASH-basic-rendered-DOT-txt.go" {
|
|
|
|
|
t.Errorf("Rendered simplate output name is wrong!: %v", s.OutputName())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-23 03:51:14 +00:00
|
|
|
|
func TestDetectsRenderedSimplate(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
|
2012-12-23 03:51:14 +00:00
|
|
|
|
if s.Type != SIMPLATE_TYPE_RENDERED {
|
|
|
|
|
t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_RENDERED)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDetectsStaticSimplate(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-static.txt", BASIC_STATIC_TXT_SIMPLATE)
|
2012-12-23 03:51:14 +00:00
|
|
|
|
if s.Type != SIMPLATE_TYPE_STATIC {
|
|
|
|
|
t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_STATIC)
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-23 04:32:26 +00:00
|
|
|
|
|
|
|
|
|
func TestDetectsJSONSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic.json", BASIC_JSON_SIMPLATE)
|
2012-12-23 04:32:26 +00:00
|
|
|
|
if s.Type != SIMPLATE_TYPE_JSON {
|
|
|
|
|
t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_JSON)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDetectsNegotiatedSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("hork", BASIC_NEGOTIATED_SIMPLATE)
|
2012-12-23 04:32:26 +00:00
|
|
|
|
if s.Type != SIMPLATE_TYPE_NEGOTIATED {
|
|
|
|
|
t.Errorf("Simplate detected as %s instead of %s",
|
|
|
|
|
s.Type, SIMPLATE_TYPE_NEGOTIATED)
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-23 16:07:23 +00:00
|
|
|
|
|
|
|
|
|
func TestAssignsNoGoPagesToStaticSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-static.txt", BASIC_STATIC_TXT_SIMPLATE)
|
2012-12-23 16:07:23 +00:00
|
|
|
|
if s.InitPage != nil {
|
|
|
|
|
t.Errorf("Static simplate had init page assigned!: %v", s.InitPage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(s.LogicPages) > 0 {
|
|
|
|
|
t.Errorf("Static simplate had logic pages assigned!: %v", s.LogicPages)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAssignsAnInitPageToRenderedSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
|
2012-12-23 16:07:23 +00:00
|
|
|
|
if s.InitPage == nil {
|
|
|
|
|
t.Errorf("Rendered simplate had no init page assigned!: %v", s.InitPage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAssignsOneLogicPageToRenderedSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
|
2012-12-23 16:07:23 +00:00
|
|
|
|
if len(s.LogicPages) != 1 {
|
|
|
|
|
t.Errorf("Rendered simplate unexpected number "+
|
|
|
|
|
"of logic pages assigned!: %v", len(s.LogicPages))
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-23 17:11:48 +00:00
|
|
|
|
|
|
|
|
|
func TestAssignsOneTemplatePageToRenderedSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
|
2012-12-23 17:11:48 +00:00
|
|
|
|
if s.TemplatePage == nil {
|
|
|
|
|
t.Errorf("Rendered simplate had no template page assigned!: %v", s.TemplatePage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAssignsAnInitPageToJSONSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic.json", BASIC_JSON_SIMPLATE)
|
2012-12-23 17:11:48 +00:00
|
|
|
|
if s.InitPage == nil {
|
|
|
|
|
t.Errorf("JSON simplate had no init page assigned!: %v", s.InitPage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAssignsOneLogicPageToJSONSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic.json", BASIC_JSON_SIMPLATE)
|
2012-12-23 17:11:48 +00:00
|
|
|
|
if len(s.LogicPages) != 1 {
|
|
|
|
|
t.Errorf("Rendered simplate unexpected number "+
|
|
|
|
|
"of logic pages assigned!: %v", len(s.LogicPages))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAssignsNoTemplatePageToJSONSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic.json", BASIC_JSON_SIMPLATE)
|
2012-12-23 17:11:48 +00:00
|
|
|
|
if s.TemplatePage != nil {
|
|
|
|
|
t.Errorf("JSON simplate had a template page assigned!: %v", s.TemplatePage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAssignsAnInitPageToNegotiatedSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-negotiated.txt", BASIC_NEGOTIATED_SIMPLATE)
|
2012-12-23 17:11:48 +00:00
|
|
|
|
if s.InitPage == nil {
|
|
|
|
|
t.Errorf("Negotiated simplate had no init page assigned!: %v", s.InitPage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAssignsAtLeastOneLogicPageToNegotiatedSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-negotiated.txt", BASIC_NEGOTIATED_SIMPLATE)
|
2012-12-23 17:11:48 +00:00
|
|
|
|
if len(s.LogicPages) < 1 {
|
|
|
|
|
t.Errorf("Negotiated simplate unexpected number "+
|
|
|
|
|
"of logic pages assigned!: %v", len(s.LogicPages))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAssignsNoTemplatePageToNegotiatedSimplates(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-negotiated.txt", BASIC_NEGOTIATED_SIMPLATE)
|
2012-12-23 17:11:48 +00:00
|
|
|
|
if s.TemplatePage != nil {
|
|
|
|
|
t.Errorf("Negotiated simplate had a template page assigned!: %v", s.TemplatePage)
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-24 05:12:11 +00:00
|
|
|
|
|
|
|
|
|
func TestRenderedSimplateCanExecuteToWriter(t *testing.T) {
|
2012-12-25 19:22:21 +00:00
|
|
|
|
s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE)
|
2012-12-24 05:12:11 +00:00
|
|
|
|
var out bytes.Buffer
|
2012-12-24 17:03:21 +00:00
|
|
|
|
err := s.Execute(&out)
|
2012-12-24 05:12:11 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRenderedSimplateOutputIsValidGoSource(t *testing.T) {
|
|
|
|
|
mkTmpDir()
|
2012-12-25 19:22:21 +00:00
|
|
|
|
if noCleanup {
|
2012-12-24 20:38:32 +00:00
|
|
|
|
fmt.Println("tmpdir =", tmpdir)
|
|
|
|
|
} else {
|
|
|
|
|
defer rmTmpDir()
|
|
|
|
|
}
|
2012-12-24 05:12:11 +00:00
|
|
|
|
|
2012-12-25 15:19:54 +00:00
|
|
|
|
outfileName, err := writeRenderedTemplate()
|
2012-12-24 05:12:11 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-25 15:19:54 +00:00
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
|
_, err = parser.ParseFile(fset, outfileName, nil, parser.DeclarationErrors)
|
2012-12-24 17:03:21 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
2012-12-25 15:19:54 +00:00
|
|
|
|
return
|
2012-12-24 17:03:21 +00:00
|
|
|
|
}
|
2012-12-25 15:19:54 +00:00
|
|
|
|
}
|
2012-12-24 05:12:11 +00:00
|
|
|
|
|
2012-12-25 15:19:54 +00:00
|
|
|
|
func TestRenderedSimplateCanBeCompiled(t *testing.T) {
|
|
|
|
|
mkTmpDir()
|
2012-12-25 19:22:21 +00:00
|
|
|
|
if noCleanup {
|
2012-12-25 15:19:54 +00:00
|
|
|
|
fmt.Println("tmpdir =", tmpdir)
|
|
|
|
|
} else {
|
|
|
|
|
defer rmTmpDir()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := writeRenderedTemplate()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = formatRenderedTemplate()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = buildRenderedTemplate()
|
2012-12-24 05:12:11 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-12-25 19:22:21 +00:00
|
|
|
|
|
|
|
|
|
func TestTreeWalkerRequiresValidDirectoryRoot(t *testing.T) {
|
|
|
|
|
_, err := NewTreeWalker("/dev/null")
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("New tree walker failed to reject invalid dir!")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestTreeWalkerYieldsSimplates(t *testing.T) {
|
|
|
|
|
siteRoot := mkTestSite()
|
|
|
|
|
if noCleanup {
|
|
|
|
|
fmt.Println("tmpdir =", tmpdir)
|
|
|
|
|
} else {
|
|
|
|
|
defer rmTmpDir()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tw, err := NewTreeWalker(siteRoot)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n := 0
|
|
|
|
|
|
|
|
|
|
simplates, err := tw.Simplates()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for simplate := range simplates {
|
|
|
|
|
if sort.SearchStrings(SIMPLATE_TYPES, simplate.Type) < 0 {
|
|
|
|
|
t.Errorf("Simplate yielded with invalid type: %v", simplate.Type)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
n++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n != 5 {
|
|
|
|
|
t.Errorf("Tree walking yielded unexpected number of files: %v", n)
|
|
|
|
|
}
|
|
|
|
|
}
|