Skip to content

Commit

Permalink
Merge pull request #9 from seznam/td-xml-parsing
Browse files Browse the repository at this point in the history
Td xml parsing
  • Loading branch information
dohnto authored Jul 10, 2017
2 parents 39729be + b9c584d commit 5875fda
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
13 changes: 7 additions & 6 deletions template.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

import (
"bytes"
"errors"
"fmt"
"path/filepath"
// "io/ioutil"
"bytes"
"github.com/Masterminds/sprig"
"html/template"
"io/ioutil"
"log"
"path/filepath"
"text/template"
)

type OptionalString struct {
Expand Down Expand Up @@ -51,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).Funcs(funcMap).Funcs(sprig.FuncMap()).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
24 changes: 14 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,33 +18,34 @@ 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},
{`{{ "hi!" | upper | repeat 3 }}`, `HI!HI!HI!`, nil},
{`{{$v := "foo/bar/baz" | split "/"}}{{$v._1}}`, `bar`, nil},
{`<?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 5875fda

Please sign in to comment.