github.com/hairyhenderson/gomplate/v3@v3.11.7/internal/tests/integration/tmpl_test.go (about) 1 package integration 2 3 import ( 4 "os" 5 "testing" 6 7 "gotest.tools/v3/assert" 8 "gotest.tools/v3/fs" 9 ) 10 11 func setupTmplTest(t *testing.T) *fs.Dir { 12 tmpDir := fs.NewDir(t, "gomplate-tmpltests", 13 fs.WithFiles(map[string]string{ 14 "toyaml.tmpl": `{{ . | data.ToYAML }}{{"\n"}}`, 15 "services.yaml": `services: 16 - name: users 17 config: 18 replicas: 2 19 - name: products 20 config: 21 replicas: 18 22 `, 23 }), 24 fs.WithDir("a", 25 fs.WithFiles(map[string]string{ 26 "pathtest.tpl": "{{ tmpl.Path }}\n{{ template `nested` }}", 27 "a.tpl": "{{ tmpl.PathDir }}", 28 "b.tpl": "{{ tmpl.Path }}", 29 }), 30 ), 31 ) 32 t.Cleanup(tmpDir.Remove) 33 34 return tmpDir 35 } 36 37 func TestTmpl_Inline(t *testing.T) { 38 inOutTest(t, ` 39 {{- $nums := dict "first" 5 "second" 10 }} 40 {{- tpl "{{ add .first .second }}" $nums }}`, 41 "15") 42 43 inOutTest(t, ` 44 {{- $nums := dict "first" 5 "second" 10 }} 45 {{- $othernums := dict "first" 18 "second" -8 }} 46 {{- tmpl.Inline "T" "{{ add .first .second }}" $nums }} 47 {{- template "T" $othernums }}`, 48 "1510") 49 } 50 51 func TestTmpl_Exec(t *testing.T) { 52 tmpDir := setupTmplTest(t) 53 54 _, _, err := cmd(t, "-i", `{{ tmpl.Exec "Nope" }}`).run() 55 assert.ErrorContains(t, err, "Nope") 56 assert.ErrorContains(t, err, " not defined") 57 58 inOutTest(t, `{{define "T1"}}hello world{{end}}{{ tmpl.Exec "T1" | strings.ToUpper }}`, `HELLO WORLD`) 59 60 o, e, err := cmd(t, 61 "-c", "in=stdin:///in.json", 62 "-t", "toyaml="+tmpDir.Join("toyaml.tmpl"), 63 "-i", `foo: 64 {{ tmpl.Exec "toyaml" .in | strings.Indent 2 }}`). 65 withStdin(`{"a":{"nested": "object"},"b":true}`).run() 66 assertSuccess(t, o, e, err, `foo: 67 a: 68 nested: object 69 b: true 70 71 `) 72 73 outDir := tmpDir.Join("out") 74 err = os.MkdirAll(outDir, 0o755) 75 if err != nil { 76 assert.NilError(t, err) 77 } 78 o, e, err = cmd(t, 79 "-d", "services="+tmpDir.Join("services.yaml"), 80 "-i", `{{- define "config" }}{{ .config | data.ToJSONPretty " " }}{{ end }} 81 {{- range (ds "services").services -}} 82 {{- $outPath := path.Join .name "config.json" }} 83 {{- tmpl.Exec "config" . | file.Write $outPath }} 84 {{- end -}}`). 85 withDir(outDir).run() 86 assertSuccess(t, o, e, err, "") 87 88 out, err := os.ReadFile(tmpDir.Join("out", "users", "config.json")) 89 assert.NilError(t, err) 90 assert.Equal(t, `{ 91 "replicas": 2 92 }`, string(out)) 93 out, err = os.ReadFile(tmpDir.Join("out", "products", "config.json")) 94 assert.NilError(t, err) 95 assert.Equal(t, `{ 96 "replicas": 18 97 }`, string(out)) 98 } 99 100 func TestTmpl_Path(t *testing.T) { 101 tmpDir := setupTmplTest(t) 102 103 o, e, err := cmd(t, 104 "-f", "a/pathtest.tpl", 105 "-t", "nested=a/a.tpl", 106 ).withDir(tmpDir.Path()).run() 107 assertSuccess(t, o, e, err, "a/pathtest.tpl\na") 108 109 o, e, err = cmd(t, 110 "-f", "a/a.tpl", 111 "-f", "a/b.tpl", 112 "-o", "-", 113 "-o", "-", 114 ).withDir(tmpDir.Path()).run() 115 assertSuccess(t, o, e, err, "aa/b.tpl") 116 }