-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse-template_test.go
91 lines (68 loc) · 2.09 KB
/
parse-template_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package parse_template
import (
"bytes"
. "github.com/stretchr/testify/assert"
"io/ioutil"
"log"
"testing"
)
func TestGetTemplateData(t *testing.T) {
args := []string{"./parse-template", "source.tpl"}
env := []string{"USER=foo", "HOME=/home/foo"}
source, td := GetTemplateData(args, env)
Equal(t, "source.tpl", source)
Empty(t, td.Args)
Equal(t, "foo", td.Env["USER"])
Equal(t, "/home/foo", td.Env["HOME"])
Equal(t, "foo", td.Any["USER"])
Equal(t, "/home/foo", td.Any["HOME"])
}
func TestGetTemplateDataArgs(t *testing.T) {
args := []string{"./parse-template", "source.tpl", "--USER=bar", "--name=foobar"}
env := []string{"USER=foo", "HOME=/home/foo"}
_, td := GetTemplateData(args, env)
Equal(t, "bar", td.Args["USER"])
Equal(t, "foobar", td.Args["name"])
Equal(t, "bar", td.Any["USER"])
Equal(t, "foobar", td.Any["name"])
}
const TEMPLATE = `
{{- if .Any.local -}}
# Local configuration
{{- end }}
# Common configuration
ENV_USER = {{ .Env.USER }}
ARG_USER = {{ .Args.USER }}
ANY_USER = {{ .Any.USER }}
`
func TestCompileTemplate(t *testing.T) {
args := []string{"./parse-template", "source.tpl", "--USER=bar", "--name=foobar"}
env := []string{"USER=foo", "HOME=/home/foo"}
_, td := GetTemplateData(args, env)
res := new(bytes.Buffer)
CompileTemplate(TEMPLATE, td, res)
contentBytes, err := ioutil.ReadAll(res)
if err != nil {
panic(err)
}
content := string(contentBytes[:])
log.Printf("Generated template: %s", content)
NotContains(t, content, "# Local configuration")
Contains(t, content, "ENV_USER = foo")
Contains(t, content, "ARG_USER = bar")
Contains(t, content, "ANY_USER = bar")
}
func TestTemplateCondition(t *testing.T) {
args := []string{"./parse-template", "source.tpl", "--local=1", "--USER=bar", "--name=foobar"}
env := []string{"USER=foo", "HOME=/home/foo"}
_, td := GetTemplateData(args, env)
res := new(bytes.Buffer)
CompileTemplate(TEMPLATE, td, res)
contentBytes, err := ioutil.ReadAll(res)
if err != nil {
panic(err)
}
content := string(contentBytes[:])
log.Printf("Generated template: %s", content)
Contains(t, content, "# Local configuration")
}