2014-12-05 11:59:56 -05:00
|
|
|
package main
|
|
|
|
|
2014-12-05 13:09:03 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2015-08-29 11:47:16 -04:00
|
|
|
"io/ioutil"
|
2014-12-05 13:09:03 -05:00
|
|
|
"log"
|
|
|
|
"os"
|
2014-12-05 13:21:42 -05:00
|
|
|
"os/exec"
|
2014-12-05 13:09:03 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-08-29 12:46:05 -04:00
|
|
|
"text/template"
|
2014-12-05 13:09:03 -05:00
|
|
|
)
|
2014-12-05 11:59:56 -05:00
|
|
|
|
|
|
|
func TestSplit2(t *testing.T) {
|
|
|
|
if a, b := split2("a:b", ":"); a != "a" || b != "b" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if a, b := split2(":b", ":"); a != "" || b != "b" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if a, b := split2("a:", ":"); a != "a" || b != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if a, b := split2(":", ":"); a != "" || b != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if a, b := split2("a", ":"); a != "a" || b != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if a, b := split2("", ":"); a != "" || b != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-29 11:47:16 -04:00
|
|
|
func tmpfile(path, s string) string {
|
|
|
|
ioutil.WriteFile(path, []byte(s), 0644)
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2014-12-05 11:59:56 -05:00
|
|
|
func TestMD(t *testing.T) {
|
2015-08-29 11:47:16 -04:00
|
|
|
defer os.Remove("foo.md")
|
|
|
|
v, body, _ := md(tmpfile("foo.md", `
|
2014-12-05 11:59:56 -05:00
|
|
|
title: Hello, world!
|
|
|
|
keywords: foo, bar, baz
|
|
|
|
empty:
|
|
|
|
bayan: [:|||:]
|
|
|
|
|
2015-08-29 13:54:55 -04:00
|
|
|
this: is a content`), Vars{})
|
2014-12-05 11:59:56 -05:00
|
|
|
if v["title"] != "Hello, world!" {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
if v["keywords"] != "foo, bar, baz" {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
if s, ok := v["empty"]; !ok || len(s) != 0 {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
if v["bayan"] != "[:|||:]" {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
if body != "this: is a content" {
|
|
|
|
t.Error(body)
|
|
|
|
}
|
2014-12-05 12:09:10 -05:00
|
|
|
|
|
|
|
// Test empty md
|
2015-08-29 13:54:55 -04:00
|
|
|
v, body, _ = md(tmpfile("foo.md", ""), Vars{})
|
|
|
|
if v["url"] != "foo.html" || len(body) != 0 {
|
2014-12-05 12:09:10 -05:00
|
|
|
t.Error(v, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test empty header
|
2015-08-29 13:54:55 -04:00
|
|
|
v, body, _ = md(tmpfile("foo.md", "Hello"), Vars{})
|
|
|
|
if v["url"] != "foo.html" || body != "Hello" {
|
2014-12-05 12:09:10 -05:00
|
|
|
t.Error(v, body)
|
|
|
|
}
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRender(t *testing.T) {
|
|
|
|
vars := map[string]string{"foo": "bar"}
|
2015-08-29 12:46:05 -04:00
|
|
|
funcs := template.FuncMap{
|
|
|
|
"greet": func(s ...string) string {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return "hello"
|
|
|
|
} else {
|
|
|
|
return "hello " + strings.Join(s, " ")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2014-12-05 11:59:56 -05:00
|
|
|
|
2015-08-29 12:46:05 -04:00
|
|
|
if s, err := render("plain text", funcs, vars); err != nil || s != "plain text" {
|
|
|
|
t.Error(s, err)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-08-29 12:46:05 -04:00
|
|
|
if s, err := render("a {{greet}} text", funcs, vars); err != nil || s != "a hello text" {
|
|
|
|
t.Error(s, err)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2015-08-29 12:46:05 -04:00
|
|
|
if s, err := render("{{greet}} x{{foo}}z", funcs, vars); err != nil || s != "hello xbarz" {
|
|
|
|
t.Error(s, err)
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|
2014-12-05 13:09:03 -05:00
|
|
|
// Test error case
|
2015-08-29 12:46:05 -04:00
|
|
|
if s, err := render("a {{greet text ", funcs, vars); err == nil || len(s) != 0 {
|
|
|
|
t.Error(s, err)
|
2014-12-05 13:09:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnv(t *testing.T) {
|
|
|
|
e := env(map[string]string{"foo": "bar", "baz": "hello world"})
|
|
|
|
mustHave := []string{"ZS=" + os.Args[0], "ZS_FOO=bar", "ZS_BAZ=hello world", "PATH="}
|
|
|
|
for _, s := range mustHave {
|
|
|
|
found := false
|
|
|
|
for _, v := range e {
|
|
|
|
if strings.HasPrefix(v, s) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Error("Missing", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRun(t *testing.T) {
|
|
|
|
out := bytes.NewBuffer(nil)
|
|
|
|
err := run("some_unbelievable_command_name", []string{}, map[string]string{}, out)
|
|
|
|
if err == nil {
|
|
|
|
t.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
out = bytes.NewBuffer(nil)
|
|
|
|
err = run(os.Args[0], []string{"-test.run=TestHelperProcess"},
|
|
|
|
map[string]string{"helper": "1", "out": "foo", "err": "bar"}, out)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if out.String() != "foo\n" {
|
|
|
|
t.Error(out.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 13:21:42 -05:00
|
|
|
func TestEvalCommand(t *testing.T) {
|
|
|
|
s, err := eval([]string{"echo", "hello"}, map[string]string{})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if s != "hello\n" {
|
|
|
|
t.Error(s)
|
|
|
|
}
|
|
|
|
_, err = eval([]string{"cat", "bogus/file"}, map[string]string{})
|
|
|
|
if _, ok := err.(*exec.ExitError); !ok {
|
|
|
|
t.Error("expected ExitError")
|
|
|
|
}
|
|
|
|
_, err = eval([]string{"missing command"}, map[string]string{})
|
|
|
|
if err != nil {
|
|
|
|
t.Error("missing command should be ignored")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 13:09:03 -05:00
|
|
|
func TestHelperProcess(*testing.T) {
|
|
|
|
if os.Getenv("ZS_HELPER") != "1" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Exit(0) // TODO check exit code
|
|
|
|
log.Println(os.Getenv("ZS_ERR")) // stderr
|
|
|
|
fmt.Println(os.Getenv("ZS_OUT")) // stdout
|
2014-12-05 11:59:56 -05:00
|
|
|
}
|