forked from kashifshah-mirrors/zs
rewritten using zs templates, allowing go templates using <% %> delimiters
This commit is contained in:
parent
097012ddbd
commit
2ce4b993b0
17
testdata/blog/.test/index.html
vendored
17
testdata/blog/.test/index.html
vendored
@ -1,17 +0,0 @@
|
|||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>My blog</title>
|
|
||||||
<link href="styles.css" rel="stylesheet" type="text/css" />
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<p>Here goes list of posts</p>
|
|
||||||
<ul>
|
|
||||||
<li>
|
|
||||||
<a href="/posts/hello.html">First post</a>
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<a href="/posts/update.html">Second post</a>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
3
testdata/page/.test/index.html
vendored
3
testdata/page/.test/index.html
vendored
@ -1,6 +1,5 @@
|
|||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<h1>Hello
|
<h1>Hello</h1>
|
||||||
</h1>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
2
testdata/page/index.html
vendored
2
testdata/page/index.html
vendored
@ -1,5 +1,5 @@
|
|||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<h1>{{ println "Hello" }}</h1>
|
<h1>{{ printf Hello }}</h1>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
208
zs.go
208
zs.go
@ -8,7 +8,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
@ -17,6 +16,7 @@ import (
|
|||||||
"github.com/eknkc/amber"
|
"github.com/eknkc/amber"
|
||||||
"github.com/russross/blackfriday"
|
"github.com/russross/blackfriday"
|
||||||
"github.com/yosssi/gcss"
|
"github.com/yosssi/gcss"
|
||||||
|
"gopkg.in/yaml.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -26,17 +26,22 @@ const (
|
|||||||
|
|
||||||
type Vars map[string]string
|
type Vars map[string]string
|
||||||
|
|
||||||
func renameExt(path, from, to string) string {
|
// renameExt renames extension (if any) from oldext to newext
|
||||||
if from == "" {
|
// If oldext is an empty string - extension is extracted automatically.
|
||||||
from = filepath.Ext(path)
|
// If path has no extension - new extension is appended
|
||||||
|
func renameExt(path, oldext, newext string) string {
|
||||||
|
if oldext == "" {
|
||||||
|
oldext = filepath.Ext(path)
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(path, from) {
|
if oldext == "" || strings.HasSuffix(path, oldext) {
|
||||||
return strings.TrimSuffix(path, from) + to
|
return strings.TrimSuffix(path, oldext) + newext
|
||||||
} else {
|
} else {
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// globals returns list of global OS environment variables that start
|
||||||
|
// with ZS_ prefix as Vars, so the values can be used inside templates
|
||||||
func globals() Vars {
|
func globals() Vars {
|
||||||
vars := Vars{}
|
vars := Vars{}
|
||||||
for _, e := range os.Environ() {
|
for _, e := range os.Environ() {
|
||||||
@ -48,25 +53,28 @@ func globals() Vars {
|
|||||||
return vars
|
return vars
|
||||||
}
|
}
|
||||||
|
|
||||||
// Converts zs markdown variables into environment variables
|
// run executes a command or a script. Vars define the command environment,
|
||||||
func env(vars Vars) []string {
|
// each zs var is converted into OS environemnt variable with ZS_ prefix
|
||||||
|
// prepended. Additional variable $ZS contains path to the zs binary. Command
|
||||||
|
// stderr is printed to zs stderr, command output is returned as a string.
|
||||||
|
func run(vars Vars, cmd string, args ...string) (string, error) {
|
||||||
|
// First check if partial exists (.amber or .html)
|
||||||
|
if b, err := ioutil.ReadFile(filepath.Join(ZSDIR, cmd+".amber")); err == nil {
|
||||||
|
return string(b), nil
|
||||||
|
}
|
||||||
|
if b, err := ioutil.ReadFile(filepath.Join(ZSDIR, cmd+".html")); err == nil {
|
||||||
|
return string(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var errbuf, outbuf bytes.Buffer
|
||||||
|
c := exec.Command(cmd, args...)
|
||||||
env := []string{"ZS=" + os.Args[0], "ZS_OUTDIR=" + PUBDIR}
|
env := []string{"ZS=" + os.Args[0], "ZS_OUTDIR=" + PUBDIR}
|
||||||
env = append(env, os.Environ()...)
|
env = append(env, os.Environ()...)
|
||||||
if vars != nil {
|
for k, v := range vars {
|
||||||
for k, v := range vars {
|
env = append(env, "ZS_"+strings.ToUpper(k)+"="+v)
|
||||||
env = append(env, "ZS_"+strings.ToUpper(k)+"="+v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return env
|
c.Env = env
|
||||||
}
|
c.Stdout = &outbuf
|
||||||
|
|
||||||
// Runs command with given arguments and variables, intercepts stderr and
|
|
||||||
// redirects stdout into the given writer
|
|
||||||
func run(cmd string, args []string, vars Vars, output io.Writer) error {
|
|
||||||
var errbuf bytes.Buffer
|
|
||||||
c := exec.Command(cmd, args...)
|
|
||||||
c.Env = env(vars)
|
|
||||||
c.Stdout = output
|
|
||||||
c.Stderr = &errbuf
|
c.Stderr = &errbuf
|
||||||
|
|
||||||
err := c.Run()
|
err := c.Run()
|
||||||
@ -74,79 +82,97 @@ func run(cmd string, args []string, vars Vars, output io.Writer) error {
|
|||||||
if errbuf.Len() > 0 {
|
if errbuf.Len() > 0 {
|
||||||
log.Println("ERROR:", errbuf.String())
|
log.Println("ERROR:", errbuf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
return nil
|
return string(outbuf.Bytes()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Splits a string in exactly two parts by delimiter
|
// getVars returns list of variables defined in a text file and actual file
|
||||||
// If no delimiter is found - the second string is be empty
|
// content following the variables declaration. Header is separated from
|
||||||
func split2(s, delim string) (string, string) {
|
// content by an empty line. Header can be either YAML or JSON.
|
||||||
parts := strings.SplitN(s, delim, 2)
|
// If no empty newline is found - file is treated as content-only.
|
||||||
if len(parts) == 2 {
|
func getVars(path string, globals Vars) (Vars, string, error) {
|
||||||
return parts[0], parts[1]
|
|
||||||
} else {
|
|
||||||
return parts[0], ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parses markdown content. Returns parsed header variables and content
|
|
||||||
func md(path string, globals Vars) (Vars, string, error) {
|
|
||||||
b, err := ioutil.ReadFile(path)
|
b, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
s := string(b)
|
s := string(b)
|
||||||
url := path[:len(path)-len(filepath.Ext(path))] + ".html"
|
|
||||||
v := Vars{
|
// Copy globals first
|
||||||
"title": "",
|
v := Vars{}
|
||||||
"description": "",
|
|
||||||
"keywords": "",
|
|
||||||
}
|
|
||||||
for name, value := range globals {
|
for name, value := range globals {
|
||||||
v[name] = value
|
v[name] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Override them by default values extracted from file name/path
|
||||||
if _, err := os.Stat(filepath.Join(ZSDIR, "layout.amber")); err == nil {
|
if _, err := os.Stat(filepath.Join(ZSDIR, "layout.amber")); err == nil {
|
||||||
v["layout"] = "layout.amber"
|
v["layout"] = "layout.amber"
|
||||||
} else {
|
} else {
|
||||||
v["layout"] = "layout.html"
|
v["layout"] = "layout.html"
|
||||||
}
|
}
|
||||||
v["file"] = path
|
v["file"] = path
|
||||||
v["url"] = url
|
v["url"] = path[:len(path)-len(filepath.Ext(path))] + ".html"
|
||||||
v["output"] = filepath.Join(PUBDIR, url)
|
v["output"] = filepath.Join(PUBDIR, v["url"])
|
||||||
|
|
||||||
if strings.Index(s, "\n\n") == -1 {
|
if sep := strings.Index(s, "\n\n"); sep == -1 {
|
||||||
return v, s, nil
|
return v, s, nil
|
||||||
|
} else {
|
||||||
|
header := s[:sep]
|
||||||
|
body := s[sep+len("\n\n"):]
|
||||||
|
vars := Vars{}
|
||||||
|
if err := yaml.Unmarshal([]byte(header), &vars); err != nil {
|
||||||
|
fmt.Println("ERROR: failed to parse header", err)
|
||||||
|
} else {
|
||||||
|
for key, value := range vars {
|
||||||
|
v[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(v["url"], "./") {
|
||||||
|
v["url"] = v["url"][2:]
|
||||||
|
}
|
||||||
|
return v, body, nil
|
||||||
}
|
}
|
||||||
header, body := split2(s, "\n\n")
|
|
||||||
for _, line := range strings.Split(header, "\n") {
|
|
||||||
key, value := split2(line, ":")
|
|
||||||
v[strings.ToLower(strings.TrimSpace(key))] = strings.TrimSpace(value)
|
|
||||||
}
|
|
||||||
if strings.HasPrefix(v["url"], "./") {
|
|
||||||
v["url"] = v["url"][2:]
|
|
||||||
}
|
|
||||||
return v, body, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use standard Go templates
|
// Render expanding zs plugins and variables
|
||||||
func render(s string, vars Vars) (string, error) {
|
func render(s string, vars Vars) (string, error) {
|
||||||
tmpl, err := template.New("").Parse(s)
|
delim_open := "{{"
|
||||||
if err != nil {
|
delim_close := "}}"
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
if err := tmpl.Execute(out, vars); err != nil {
|
for {
|
||||||
return "", err
|
if from := strings.Index(s, delim_open); from == -1 {
|
||||||
|
out.WriteString(s)
|
||||||
|
return out.String(), nil
|
||||||
|
} else {
|
||||||
|
if to := strings.Index(s, delim_close); to == -1 {
|
||||||
|
return "", fmt.Errorf("Close delim not found")
|
||||||
|
} else {
|
||||||
|
out.WriteString(s[:from])
|
||||||
|
cmd := s[from+len(delim_open) : to]
|
||||||
|
s = s[to+len(delim_close):]
|
||||||
|
m := strings.Fields(cmd)
|
||||||
|
if len(m) == 1 {
|
||||||
|
if v, ok := vars[m[0]]; ok {
|
||||||
|
out.WriteString(v)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if res, err := run(vars, m[0], m[1:]...); err == nil {
|
||||||
|
out.WriteString(res)
|
||||||
|
} else {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return string(out.Bytes()), nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Renders markdown with the given layout into html expanding all the macros
|
// Renders markdown with the given layout into html expanding all the macros
|
||||||
func buildMarkdown(path string, w io.Writer, vars Vars) error {
|
func buildMarkdown(path string, w io.Writer, vars Vars) error {
|
||||||
v, body, err := md(path, vars)
|
v, body, err := getVars(path, vars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -172,11 +198,14 @@ func buildMarkdown(path string, w io.Writer, vars Vars) error {
|
|||||||
|
|
||||||
// Renders text file expanding all variable macros inside it
|
// Renders text file expanding all variable macros inside it
|
||||||
func buildHTML(path string, w io.Writer, vars Vars) error {
|
func buildHTML(path string, w io.Writer, vars Vars) error {
|
||||||
b, err := ioutil.ReadFile(path)
|
v, body, err := getVars(path, vars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
content, err := render(string(b), vars)
|
if body, err = render(body, v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tmpl, err := template.New("").Delims("<%", "%>").Parse(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -188,21 +217,22 @@ func buildHTML(path string, w io.Writer, vars Vars) error {
|
|||||||
defer f.Close()
|
defer f.Close()
|
||||||
w = f
|
w = f
|
||||||
}
|
}
|
||||||
_, err = io.WriteString(w, content)
|
return tmpl.Execute(w, vars)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Renders .amber file into .html
|
// Renders .amber file into .html
|
||||||
func buildAmber(path string, w io.Writer, vars Vars) error {
|
func buildAmber(path string, w io.Writer, vars Vars) error {
|
||||||
a := amber.New()
|
v, body, err := getVars(path, vars)
|
||||||
err := a.ParseFile(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if body, err = render(body, v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
data := map[string]interface{}{}
|
a := amber.New()
|
||||||
for k, v := range vars {
|
if err := a.Parse(body); err != nil {
|
||||||
data[k] = v
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
t, err := a.Compile()
|
t, err := a.Compile()
|
||||||
@ -217,7 +247,7 @@ func buildAmber(path string, w io.Writer, vars Vars) error {
|
|||||||
defer f.Close()
|
defer f.Close()
|
||||||
w = f
|
w = f
|
||||||
}
|
}
|
||||||
return t.Execute(w, data)
|
return t.Execute(w, vars)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compiles .gcss into .css
|
// Compiles .gcss into .css
|
||||||
@ -298,12 +328,11 @@ func buildAll(watch bool) {
|
|||||||
return nil
|
return nil
|
||||||
} else if info.ModTime().After(lastModified) {
|
} else if info.ModTime().After(lastModified) {
|
||||||
if !modified {
|
if !modified {
|
||||||
// About to be modified, so run pre-build hook
|
// First file in this build cycle is about to be modified
|
||||||
// FIXME on windows it might not work well
|
run(vars, "prehook")
|
||||||
run(filepath.Join(ZSDIR, "pre"), []string{}, nil, nil)
|
|
||||||
modified = true
|
modified = true
|
||||||
}
|
}
|
||||||
log.Println("build: ", path)
|
log.Println("build:", path)
|
||||||
return build(path, nil, vars)
|
return build(path, nil, vars)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -312,9 +341,8 @@ func buildAll(watch bool) {
|
|||||||
log.Println("ERROR:", err)
|
log.Println("ERROR:", err)
|
||||||
}
|
}
|
||||||
if modified {
|
if modified {
|
||||||
// Something was modified, so post-build hook
|
// At least one file in this build cycle has been modified
|
||||||
// FIXME on windows it might not work well
|
run(vars, "posthook")
|
||||||
run(filepath.Join(ZSDIR, "post"), []string{}, nil, nil)
|
|
||||||
modified = false
|
modified = false
|
||||||
}
|
}
|
||||||
if !watch {
|
if !watch {
|
||||||
@ -325,6 +353,13 @@ func buildAll(watch bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// prepend .zs to $PATH, so plugins will be found before OS commands
|
||||||
|
p := os.Getenv("PATH")
|
||||||
|
p = ZSDIR + ":" + p
|
||||||
|
os.Setenv("PATH", p)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) == 1 {
|
if len(os.Args) == 1 {
|
||||||
fmt.Println(os.Args[0], "<command> [args]")
|
fmt.Println(os.Args[0], "<command> [args]")
|
||||||
@ -350,7 +385,7 @@ func main() {
|
|||||||
fmt.Println("var: filename expected")
|
fmt.Println("var: filename expected")
|
||||||
} else {
|
} else {
|
||||||
s := ""
|
s := ""
|
||||||
if vars, _, err := md(args[0], globals()); err != nil {
|
if vars, _, err := getVars(args[0], globals()); err != nil {
|
||||||
fmt.Println("var: " + err.Error())
|
fmt.Println("var: " + err.Error())
|
||||||
} else {
|
} else {
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
@ -366,9 +401,10 @@ func main() {
|
|||||||
fmt.Println(strings.TrimSpace(s))
|
fmt.Println(strings.TrimSpace(s))
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
err := run(path.Join(ZSDIR, cmd), args, globals(), os.Stdout)
|
if s, err := run(globals(), cmd, args...); err != nil {
|
||||||
if err != nil {
|
fmt.Println(err)
|
||||||
log.Println("ERROR:", err)
|
} else {
|
||||||
|
fmt.Println(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
189
zs_test.go
189
zs_test.go
@ -1,146 +1,107 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSplit2(t *testing.T) {
|
func TestRenameExt(t *testing.T) {
|
||||||
if a, b := split2("a:b", ":"); a != "a" || b != "b" {
|
if s := renameExt("foo.amber", ".amber", ".html"); s != "foo.html" {
|
||||||
t.Fail()
|
t.Error(s)
|
||||||
}
|
}
|
||||||
if a, b := split2(":b", ":"); a != "" || b != "b" {
|
if s := renameExt("foo.amber", "", ".html"); s != "foo.html" {
|
||||||
t.Fail()
|
t.Error(s)
|
||||||
}
|
}
|
||||||
if a, b := split2("a:", ":"); a != "a" || b != "" {
|
if s := renameExt("foo.amber", ".md", ".html"); s != "foo.amber" {
|
||||||
t.Fail()
|
t.Error(s)
|
||||||
}
|
}
|
||||||
if a, b := split2(":", ":"); a != "" || b != "" {
|
if s := renameExt("foo", ".amber", ".html"); s != "foo" {
|
||||||
t.Fail()
|
t.Error(s)
|
||||||
}
|
}
|
||||||
if a, b := split2("a", ":"); a != "a" || b != "" {
|
if s := renameExt("foo", "", ".html"); s != "foo.html" {
|
||||||
t.Fail()
|
t.Error(s)
|
||||||
}
|
|
||||||
if a, b := split2("", ":"); a != "" || b != "" {
|
|
||||||
t.Fail()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func tmpfile(path, s string) string {
|
func TestRun(t *testing.T) {
|
||||||
ioutil.WriteFile(path, []byte(s), 0644)
|
// external command
|
||||||
return path
|
if s, err := run(Vars{}, "echo", "hello"); err != nil || s != "hello\n" {
|
||||||
|
t.Error(s, err)
|
||||||
|
}
|
||||||
|
// passing variables to plugins
|
||||||
|
if s, err := run(Vars{"foo": "bar"}, "sh", "-c", "echo $ZS_FOO"); err != nil || s != "bar\n" {
|
||||||
|
t.Error(s, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// custom plugin overriding external command
|
||||||
|
os.Mkdir(ZSDIR, 0755)
|
||||||
|
script := `#!/bin/sh
|
||||||
|
echo foo
|
||||||
|
`
|
||||||
|
ioutil.WriteFile(filepath.Join(ZSDIR, "echo"), []byte(script), 0755)
|
||||||
|
if s, err := run(Vars{}, "echo", "hello"); err != nil || s != "foo\n" {
|
||||||
|
t.Error(s, err)
|
||||||
|
}
|
||||||
|
os.Remove(filepath.Join(ZSDIR, "echo"))
|
||||||
|
os.Remove(ZSDIR)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMD(t *testing.T) {
|
func TestVars(t *testing.T) {
|
||||||
defer os.Remove("foo.md")
|
tests := map[string]Vars{
|
||||||
v, body, _ := md(tmpfile("foo.md", `
|
`
|
||||||
title: Hello, world!
|
foo: bar
|
||||||
keywords: foo, bar, baz
|
title: Hello, world!
|
||||||
empty:
|
|
||||||
bayan: [:|||:]
|
|
||||||
|
|
||||||
this: is a content`), Vars{})
|
Some content in markdown
|
||||||
if v["title"] != "Hello, world!" {
|
`: Vars{
|
||||||
t.Error()
|
"foo": "bar",
|
||||||
}
|
"title": "Hello, world!",
|
||||||
if v["keywords"] != "foo, bar, baz" {
|
"url": "test.html",
|
||||||
t.Error()
|
"file": "test.md",
|
||||||
}
|
"output": filepath.Join(PUBDIR, "test.html"),
|
||||||
if s, ok := v["empty"]; !ok || len(s) != 0 {
|
"__content": "Some content in markdown\n",
|
||||||
t.Error()
|
},
|
||||||
}
|
`url: "example.com/foo.html"
|
||||||
if v["bayan"] != "[:|||:]" {
|
|
||||||
t.Error()
|
Hello
|
||||||
}
|
`: Vars{
|
||||||
if body != "this: is a content" {
|
"url": "example.com/foo.html",
|
||||||
t.Error(body)
|
"__content": "Hello\n",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test empty md
|
for script, vars := range tests {
|
||||||
v, body, _ = md(tmpfile("foo.md", ""), Vars{})
|
ioutil.WriteFile("test.md", []byte(script), 0644)
|
||||||
if v["url"] != "foo.html" || len(body) != 0 {
|
if v, s, err := getVars("test.md", Vars{"baz": "123"}); err != nil {
|
||||||
t.Error(v, body)
|
t.Error(err)
|
||||||
}
|
} else if s != vars["__content"] {
|
||||||
|
t.Error(s, vars["__content"])
|
||||||
// Test empty header
|
} else {
|
||||||
v, body, _ = md(tmpfile("foo.md", "Hello"), Vars{})
|
for key, value := range vars {
|
||||||
if v["url"] != "foo.html" || body != "Hello" {
|
if key != "__content" && v[key] != value {
|
||||||
t.Error(v, body)
|
t.Error(key, v[key], value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRender(t *testing.T) {
|
func TestRender(t *testing.T) {
|
||||||
vars := map[string]string{"foo": "bar"}
|
vars := map[string]string{"foo": "bar"}
|
||||||
funcs := Funcs{
|
|
||||||
"greet": func(s ...string) string {
|
|
||||||
if len(s) == 0 {
|
|
||||||
return "hello"
|
|
||||||
} else {
|
|
||||||
return "hello " + strings.Join(s, " ")
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
if s, err := render("plain text", funcs, vars); err != nil || s != "plain text" {
|
if s, _ := render("foo bar", vars); s != "foo bar" {
|
||||||
t.Error(s, err)
|
t.Error(s)
|
||||||
}
|
}
|
||||||
if s, err := render("a {{greet}} text", funcs, vars); err != nil || s != "a hello text" {
|
if s, _ := render("a {{printf short}} text", vars); s != "a short text" {
|
||||||
t.Error(s, err)
|
t.Error(s)
|
||||||
}
|
}
|
||||||
if s, err := render("{{greet}} x{{foo}}z", funcs, vars); err != nil || s != "hello xbarz" {
|
if s, _ := render("{{printf Hello}} x{{foo}}z", vars); s != "Hello xbarz" {
|
||||||
t.Error(s, err)
|
t.Error(s)
|
||||||
}
|
}
|
||||||
// Test error case
|
// Test error case
|
||||||
if s, err := render("a {{greet text ", funcs, vars); err == nil || len(s) != 0 {
|
if _, err := render("a {{greet text ", vars); err == nil {
|
||||||
t.Error(s, err)
|
t.Error("error expected")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user