From 3a4c55e63b7f611e4b0536798e02e823b562b8a2 Mon Sep 17 00:00:00 2001 From: Aaron Schlesinger Date: Fri, 29 Apr 2016 08:28:15 -0700 Subject: [PATCH 1/4] add test case for sprig functions --- testdata/sprig.tpl | 1 + 1 file changed, 1 insertion(+) create mode 100644 testdata/sprig.tpl diff --git a/testdata/sprig.tpl b/testdata/sprig.tpl new file mode 100644 index 0000000..d4eaabb --- /dev/null +++ b/testdata/sprig.tpl @@ -0,0 +1 @@ +Hello {{ "world!" | upper | repeat 5 }} From 370087721c20b57e0974cc43815757be22eaf4a2 Mon Sep 17 00:00:00 2001 From: Aaron Schlesinger Date: Fri, 29 Apr 2016 08:28:38 -0700 Subject: [PATCH 2/4] insert sprig functions at parse time not execution time --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 8a9e799..4cbbcc3 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,7 @@ import ( "flag" "fmt" "os" - + "path/filepath" template "text/template" "github.com/Masterminds/sprig" @@ -19,7 +19,7 @@ func main() { os.Exit(1) } - tpl, err := template.ParseFiles(*in) + tpl, err := template.New(filepath.Base(*in)).Funcs(sprig.TxtFuncMap()).ParseFiles(*in) if err != nil { fmt.Fprintf(os.Stderr, "Error: parsing template %s (%s)\n", *in, err) os.Exit(1) @@ -27,7 +27,7 @@ func main() { envMap := collectEnv() - if err := tpl.Funcs(sprig.TxtFuncMap()).Execute(os.Stdout, envMap); err != nil { + if err := tpl.Execute(os.Stdout, envMap); err != nil { fmt.Fprintf(os.Stderr, "Rendering template (%s)", err) os.Exit(1) } From 746ced05b0453b029d169af503bc0f8fa2b06f63 Mon Sep 17 00:00:00 2001 From: Aaron Schlesinger Date: Fri, 29 Apr 2016 08:38:16 -0700 Subject: [PATCH 3/4] split template create and render funcs and add tests for the new ones --- main.go | 7 ++----- tpl.go | 16 ++++++++++++++++ tpl_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 tpl.go create mode 100644 tpl_test.go diff --git a/main.go b/main.go index 4cbbcc3..539cf4c 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,6 @@ import ( "fmt" "os" "path/filepath" - template "text/template" - - "github.com/Masterminds/sprig" ) func main() { @@ -19,7 +16,7 @@ func main() { os.Exit(1) } - tpl, err := template.New(filepath.Base(*in)).Funcs(sprig.TxtFuncMap()).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) @@ -27,7 +24,7 @@ func main() { envMap := collectEnv() - if err := tpl.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) } diff --git a/tpl.go b/tpl.go new file mode 100644 index 0000000..78d9512 --- /dev/null +++ b/tpl.go @@ -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) +} diff --git a/tpl_test.go b/tpl_test.go new file mode 100644 index 0000000..5712046 --- /dev/null +++ b/tpl_test.go @@ -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) + } + } +} From b111e074166555f95fdb32579d9f71725895bf02 Mon Sep 17 00:00:00 2001 From: Aaron Schlesinger Date: Fri, 29 Apr 2016 08:40:05 -0700 Subject: [PATCH 4/4] add test target --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 529cdb1..044aef7 100644 --- a/Makefile +++ b/Makefile @@ -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