Skip to content

Commit

Permalink
fix: pass emptyhash to template.Exec function to avoid issues describ…
Browse files Browse the repository at this point in the history
…ed in [1]

[1] golang/go@277bcbb
  • Loading branch information
Tomas Dohnalek committed Jun 29, 2017
1 parent 3496e44 commit b9c584d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
6 changes: 4 additions & 2 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ var funcMap = template.FuncMap{
func generateTemplate(source, name string) (string, error) {
var t *template.Template
var err error
t, err = template.New(name).Option("missingkey=zero").Funcs(funcMap).Funcs(sprig.TxtFuncMap()).Parse(source)
t, err = template.New(name).Option("missingkey=error").Funcs(funcMap).Funcs(sprig.TxtFuncMap()).Parse(source)
if err != nil {
return "", err
}
var buffer bytes.Buffer
if err = t.Execute(&buffer, nil); err != nil {
// hacking because go 1.7 fails to throw error, see https://github.com/golang/go/commit/277bcbbdcd26f2d64493e596238e34b47782f98e
emptyHash := map[string]interface{}{}
if err = t.Execute(&buffer, &emptyHash); err != nil {
return "", err
}
return buffer.String(), nil
Expand Down
23 changes: 13 additions & 10 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package main
import (
"github.com/joho/godotenv"
"os"
"reflect"
"testing"
"text/template"
)

func TestGenerateTemplate(t *testing.T) {
templateName := "test"
var tests = []struct {
in string
want string
Expand All @@ -15,8 +18,8 @@ func TestGenerateTemplate(t *testing.T) {
{`K={{ env "GOENVTEMPLATOR_DEFINED_VAR" }}`, `K=foo`, nil},
{`K={{ env "GOENVTEMPLATOR_DEFINED_FILE_VAR" }}`, `K=bar`, nil},
{`K={{ env "NONEXISTING" }}`, `K=`, nil},
{`K={{ .NONEXISTING }}`, `K=`, nil},
{`K={{ .NonExisting | default "default value" }}`, `K=default value`, nil},
{`K={{ .NONEXISTING }}`, ``, template.ExecError{}},
{`K={{ .NonExisting | default "default value" }}`, ``, template.ExecError{}},
{`K={{ env "GOENVTEMPLATOR_DEFINED_VAR" | default "xxx" }}`, `K=foo`, nil},
{`K={{ env "GOENVTEMPLATOR_DEFINED_FILE_VAR" | default "xxx" }}`, `K=bar`, nil},
{`K={{ env "NONEXISTING"| default "default value" }}`, `K=default value`, nil},
Expand All @@ -25,24 +28,24 @@ func TestGenerateTemplate(t *testing.T) {
{`<?xml version="1.0"?>`, `<?xml version="1.0"?>`, nil},
}

templateName := "test"

os.Setenv("GOENVTEMPLATOR_DEFINED_VAR", "foo")

err := godotenv.Load("./tests/fixtures.env")
if err != nil {
t.Errorf("Cannot load env file: %q", err)
}

for _, tt := range tests {
got, gotErr := generateTemplate(tt.in, templateName)
for _, testcase := range tests {
got, gotErr := generateTemplate(testcase.in, templateName)

if tt.want != got {
t.Errorf("generateTemplate(%q, %q) => (%q, _), want (%q, _)", tt.in, templateName, got, tt.want)
if testcase.want != got {
t.Errorf("generateTemplate(%q, %q) => (%q, _), want (%q, _)", testcase.in, templateName, got, testcase.want)
}

if tt.err != gotErr {
t.Errorf("generateTemplate(%q, %q) => (_, %q), want (_, %q)", tt.in, templateName, gotErr, tt.err)
errType, gotErrType := reflect.TypeOf(testcase.err), reflect.TypeOf(gotErr)

if errType != gotErrType {
t.Errorf("generateTemplate(%q, %q) => (_, %q), want (_, %q)", testcase.in, templateName, gotErrType, errType)
}
}
}

0 comments on commit b9c584d

Please sign in to comment.