github.com/wuhuizuo/gomplate@v3.5.0+incompatible/tests/integration/tmpl_test.go (about)

     1  //+build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"bytes"
     7  	"io/ioutil"
     8  	"os"
     9  
    10  	"github.com/gotestyourself/gotestyourself/assert"
    11  	"github.com/gotestyourself/gotestyourself/fs"
    12  	"github.com/gotestyourself/gotestyourself/icmd"
    13  	. "gopkg.in/check.v1"
    14  )
    15  
    16  type TmplSuite struct {
    17  	tmpDir *fs.Dir
    18  }
    19  
    20  var _ = Suite(&TmplSuite{})
    21  
    22  func (s *TmplSuite) SetUpTest(c *C) {
    23  	s.tmpDir = fs.NewDir(c, "gomplate-tmpltests",
    24  		fs.WithFiles(map[string]string{
    25  			"toyaml.tmpl": `{{ . | data.ToYAML }}{{"\n"}}`,
    26  			"services.yaml": `services:
    27    - name: users
    28      config:
    29        replicas: 2
    30    - name: products
    31      config:
    32        replicas: 18
    33  `,
    34  		}),
    35  	)
    36  }
    37  
    38  func (s *TmplSuite) TearDownTest(c *C) {
    39  	s.tmpDir.Remove()
    40  }
    41  
    42  func (s *TmplSuite) TestInline(c *C) {
    43  	inOutTest(c, `
    44  		{{- $nums := dict "first" 5 "second" 10 }}
    45  		{{- tpl "{{ add .first .second }}" $nums }}`,
    46  		"15")
    47  
    48  	inOutTest(c, `
    49  		{{- $nums := dict "first" 5 "second" 10 }}
    50  		{{- $othernums := dict "first" 18 "second" -8 }}
    51  		{{- tmpl.Inline "T" "{{ add .first .second }}" $nums }}
    52  		{{- template "T" $othernums }}`,
    53  		"1510")
    54  }
    55  
    56  func (s *TmplSuite) TestExec(c *C) {
    57  	result := icmd.RunCmd(icmd.Command(GomplateBin,
    58  		"-i", `{{ tmpl.Exec "Nope" }}`,
    59  	))
    60  	result.Assert(c, icmd.Expected{ExitCode: 1, Err: `template "Nope" not defined`})
    61  
    62  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    63  		"-i", `{{define "T1"}}hello world{{end}}{{ tmpl.Exec "T1" | strings.ToUpper }}`,
    64  	))
    65  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: `HELLO WORLD`})
    66  
    67  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    68  		"-c", "in=stdin:///in.json",
    69  		"-t", "toyaml="+s.tmpDir.Join("toyaml.tmpl"),
    70  		"-i", `foo:
    71  {{ tmpl.Exec "toyaml" .in | strings.Indent 2 }}`,
    72  	), func(cmd *icmd.Cmd) {
    73  		in := bytes.NewBufferString(`{"a":{"nested": "object"},"b":true}`)
    74  		cmd.Stdin = in
    75  	})
    76  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: `foo:
    77    a:
    78      nested: object
    79    b: true
    80  `})
    81  
    82  	outDir := s.tmpDir.Join("out")
    83  	err := os.MkdirAll(outDir, 0755)
    84  	if err != nil {
    85  		assert.NilError(c, err)
    86  	}
    87  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    88  		"-d", "services="+s.tmpDir.Join("services.yaml"),
    89  		"-i", `{{- define "config" }}{{ .config | data.ToJSONPretty " " }}{{ end }}
    90  {{- range (ds "services").services -}}
    91  {{- $outPath := path.Join .name "config.json" }}
    92  {{- tmpl.Exec "config" . | file.Write $outPath }}
    93  {{- end -}}`,
    94  	), func(cmd *icmd.Cmd) {
    95  		cmd.Dir = outDir
    96  	})
    97  	result.Assert(c, icmd.Expected{ExitCode: 0})
    98  	assert.Equal(c, "", result.Stdout())
    99  	assert.Equal(c, "", result.Stderr())
   100  
   101  	out, err := ioutil.ReadFile(s.tmpDir.Join("out", "users", "config.json"))
   102  	assert.NilError(c, err)
   103  	assert.Equal(c, `{
   104   "replicas": 2
   105  }`, string(out))
   106  	out, err = ioutil.ReadFile(s.tmpDir.Join("out", "products", "config.json"))
   107  	assert.NilError(c, err)
   108  	assert.Equal(c, `{
   109   "replicas": 18
   110  }`, string(out))
   111  }