github.com/hairyhenderson/gomplate/v3@v3.11.7/gomplate_test.go (about) 1 package gomplate 2 3 import ( 4 "bytes" 5 "context" 6 "os" 7 "path/filepath" 8 "testing" 9 "text/template" 10 11 "github.com/hairyhenderson/gomplate/v3/aws" 12 "github.com/hairyhenderson/gomplate/v3/conv" 13 "github.com/hairyhenderson/gomplate/v3/data" 14 "github.com/hairyhenderson/gomplate/v3/env" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func testTemplate(t *testing.T, tr *Renderer, tmpl string) string { 20 t.Helper() 21 22 var out bytes.Buffer 23 err := tr.Render(context.Background(), "testtemplate", tmpl, &out) 24 assert.NoError(t, err) 25 26 return out.String() 27 } 28 29 func TestGetenvTemplates(t *testing.T) { 30 tr := NewRenderer(Options{ 31 Funcs: template.FuncMap{ 32 "getenv": env.Getenv, 33 "bool": conv.Bool, 34 }, 35 }) 36 assert.Empty(t, testTemplate(t, tr, `{{getenv "BLAHBLAHBLAH"}}`)) 37 assert.Equal(t, os.Getenv("USER"), testTemplate(t, tr, `{{getenv "USER"}}`)) 38 assert.Equal(t, "default value", testTemplate(t, tr, `{{getenv "BLAHBLAHBLAH" "default value"}}`)) 39 } 40 41 func TestBoolTemplates(t *testing.T) { 42 g := NewRenderer(Options{ 43 Funcs: template.FuncMap{ 44 "bool": conv.Bool, 45 }, 46 }) 47 assert.Equal(t, "true", testTemplate(t, g, `{{bool "true"}}`)) 48 assert.Equal(t, "false", testTemplate(t, g, `{{bool "false"}}`)) 49 assert.Equal(t, "false", testTemplate(t, g, `{{bool "foo"}}`)) 50 assert.Equal(t, "false", testTemplate(t, g, `{{bool ""}}`)) 51 } 52 53 func TestEc2MetaTemplates(t *testing.T) { 54 createGomplate := func(data map[string]string, region string) *Renderer { 55 ec2meta := aws.MockEC2Meta(data, nil, region) 56 return NewRenderer(Options{Funcs: template.FuncMap{"ec2meta": ec2meta.Meta}}) 57 } 58 59 g := createGomplate(nil, "") 60 assert.Equal(t, "", testTemplate(t, g, `{{ec2meta "foo"}}`)) 61 assert.Equal(t, "default", testTemplate(t, g, `{{ec2meta "foo" "default"}}`)) 62 63 g = createGomplate(map[string]string{"instance-id": "i-1234"}, "") 64 assert.Equal(t, "i-1234", testTemplate(t, g, `{{ec2meta "instance-id"}}`)) 65 assert.Equal(t, "i-1234", testTemplate(t, g, `{{ec2meta "instance-id" "default"}}`)) 66 } 67 68 func TestEc2MetaTemplates_WithJSON(t *testing.T) { 69 ec2meta := aws.MockEC2Meta(map[string]string{"obj": `"foo": "bar"`}, map[string]string{"obj": `"foo": "baz"`}, "") 70 71 g := NewRenderer(Options{ 72 Funcs: template.FuncMap{ 73 "ec2meta": ec2meta.Meta, 74 "ec2dynamic": ec2meta.Dynamic, 75 "json": data.JSON, 76 }, 77 }) 78 79 assert.Equal(t, "bar", testTemplate(t, g, `{{ (ec2meta "obj" | json).foo }}`)) 80 assert.Equal(t, "baz", testTemplate(t, g, `{{ (ec2dynamic "obj" | json).foo }}`)) 81 } 82 83 func TestJSONArrayTemplates(t *testing.T) { 84 g := NewRenderer(Options{ 85 Funcs: template.FuncMap{ 86 "jsonArray": data.JSONArray, 87 }, 88 }) 89 90 assert.Equal(t, "[foo bar]", testTemplate(t, g, `{{jsonArray "[\"foo\",\"bar\"]"}}`)) 91 assert.Equal(t, "bar", testTemplate(t, g, `{{ index (jsonArray "[\"foo\",\"bar\"]") 1 }}`)) 92 } 93 94 func TestYAMLTemplates(t *testing.T) { 95 g := NewRenderer(Options{ 96 Funcs: template.FuncMap{ 97 "yaml": data.YAML, 98 "yamlArray": data.YAMLArray, 99 }, 100 }) 101 102 assert.Equal(t, "bar", testTemplate(t, g, `{{(yaml "foo: bar").foo}}`)) 103 assert.Equal(t, "[foo bar]", testTemplate(t, g, `{{yamlArray "- foo\n- bar\n"}}`)) 104 assert.Equal(t, "bar", testTemplate(t, g, `{{ index (yamlArray "[\"foo\",\"bar\"]") 1 }}`)) 105 } 106 107 func TestSliceTemplates(t *testing.T) { 108 g := NewRenderer(Options{ 109 Funcs: template.FuncMap{ 110 "slice": conv.Slice, 111 }, 112 }) 113 assert.Equal(t, "foo", testTemplate(t, g, `{{index (slice "foo") 0}}`)) 114 assert.Equal(t, `[foo bar 42]`, testTemplate(t, g, `{{slice "foo" "bar" 42}}`)) 115 assert.Equal(t, `helloworld`, testTemplate(t, g, `{{range slice "hello" "world"}}{{.}}{{end}}`)) 116 } 117 118 func TestHasTemplate(t *testing.T) { 119 g := NewRenderer(Options{ 120 Funcs: template.FuncMap{ 121 "yaml": data.YAML, 122 "has": conv.Has, 123 }, 124 }) 125 assert.Equal(t, "true", testTemplate(t, g, `{{has ("foo:\n bar: true" | yaml) "foo"}}`)) 126 assert.Equal(t, "true", testTemplate(t, g, `{{has ("foo:\n bar: true" | yaml).foo "bar"}}`)) 127 assert.Equal(t, "false", testTemplate(t, g, `{{has ("foo: true" | yaml) "bah"}}`)) 128 tmpl := `{{- $data := yaml "foo: bar\nbaz: qux\n" }} 129 {{- if (has $data "baz") }} 130 {{- $data.baz }} 131 {{- end }}` 132 assert.Equal(t, "qux", testTemplate(t, g, tmpl)) 133 tmpl = `{{- $data := yaml "foo: bar\nbaz: qux\n" }} 134 {{- if (has $data "quux") }} 135 {{- $data.quux }} 136 {{- else }} 137 {{- $data.foo }} 138 {{- end }}` 139 assert.Equal(t, "bar", testTemplate(t, g, tmpl)) 140 } 141 142 func TestCustomDelim(t *testing.T) { 143 g := NewRenderer(Options{ 144 LDelim: "[", 145 RDelim: "]", 146 }) 147 assert.Equal(t, "hi", testTemplate(t, g, `[print "hi"]`)) 148 } 149 150 func TestRunTemplates(t *testing.T) { 151 buf := &bytes.Buffer{} 152 config := &Config{Input: "foo", OutputFiles: []string{"-"}, Out: buf} 153 err := RunTemplates(config) 154 assert.NoError(t, err) 155 assert.Equal(t, "foo", buf.String()) 156 assert.Equal(t, 1, Metrics.TemplatesGathered) 157 assert.Equal(t, 1, Metrics.TemplatesProcessed) 158 assert.Equal(t, 0, Metrics.Errors) 159 } 160 161 func TestSimpleNamer(t *testing.T) { 162 n := simpleNamer("out/") 163 out, err := n(context.Background(), "file") 164 assert.NoError(t, err) 165 expected := filepath.FromSlash("out/file") 166 assert.Equal(t, expected, out) 167 } 168 169 func TestMappingNamer(t *testing.T) { 170 ctx := context.Background() 171 tr := &Renderer{ 172 data: &data.Data{}, 173 funcs: map[string]interface{}{ 174 "foo": func() string { return "foo" }, 175 }, 176 } 177 n := mappingNamer("out/{{ .in }}", tr) 178 out, err := n(ctx, "file") 179 assert.NoError(t, err) 180 expected := filepath.FromSlash("out/file") 181 assert.Equal(t, expected, out) 182 183 n = mappingNamer("out/{{ foo }}{{ .in }}", tr) 184 out, err = n(ctx, "file") 185 assert.NoError(t, err) 186 expected = filepath.FromSlash("out/foofile") 187 assert.Equal(t, expected, out) 188 }