Skip to content

Commit

Permalink
Merge pull request #16 from arschles/more-tests
Browse files Browse the repository at this point in the history
Fixing sprig error
  • Loading branch information
arschles committed Apr 29, 2016
2 parents c0e37be + b111e07 commit 325804b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ glideup:
${DEV_ENV_CMD} glide up

test:
@echo "no tests yet"
${DEV_ENV_CMD} go test $$(glide nv)

build:
${DEV_ENV_PREFIX} -e CGO_ENABLED=0 ${DEV_ENV_IMAGE} go build -a -installsuffix cgo -ldflags '-s' -o envtpl
Expand Down
9 changes: 3 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import (
"flag"
"fmt"
"os"

template "text/template"

"github.com/Masterminds/sprig"
"path/filepath"
)

func main() {
Expand All @@ -19,15 +16,15 @@ func main() {
os.Exit(1)
}

tpl, err := template.ParseFiles(*in)
tpl, err := createTpl(filepath.Base(*in), *in)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: parsing template %s (%s)\n", *in, err)
os.Exit(1)
}

envMap := collectEnv()

if err := tpl.Funcs(sprig.TxtFuncMap()).Execute(os.Stdout, envMap); err != nil {
if err := renderTpl(tpl, os.Stdout, envMap); err != nil {
fmt.Fprintf(os.Stderr, "Rendering template (%s)", err)
os.Exit(1)
}
Expand Down
1 change: 1 addition & 0 deletions testdata/sprig.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello {{ "world!" | upper | repeat 5 }}
16 changes: 16 additions & 0 deletions tpl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"io"
"text/template"

"github.com/Masterminds/sprig"
)

func createTpl(tplName, fileName string) (*template.Template, error) {
return template.New(tplName).Funcs(sprig.TxtFuncMap()).ParseFiles(fileName)
}

func renderTpl(tpl *template.Template, w io.Writer, data interface{}) error {
return tpl.Execute(w, data)
}
40 changes: 40 additions & 0 deletions tpl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"bytes"
"os"
"path/filepath"
"testing"

"github.com/arschles/assert"
)

func testDataDir() (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Join(wd, "testdata"), nil
}

func TestCreateAndRenderTpl(t *testing.T) {
td, err := testDataDir()
assert.NoErr(t, err)
templateNames := []string{
"sprig.tpl",
"pwd.tpl",
}
envs := collectEnv()
for i, tplName := range templateNames {
fName := filepath.Join(td, tplName)
tpl, err := createTpl(filepath.Base(fName), fName)
if err != nil {
t.Errorf("Error creating template %d (%s)", i, err)
continue
}
buf := new(bytes.Buffer)
if err := renderTpl(tpl, buf, envs); err != nil {
t.Errorf("Error rendering template %d (%s)", i, err)
}
}
}

0 comments on commit 325804b

Please sign in to comment.