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

     1  //+build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"bytes"
     7  
     8  	. "gopkg.in/check.v1"
     9  
    10  	"github.com/gotestyourself/gotestyourself/icmd"
    11  )
    12  
    13  type TestSuite struct {
    14  }
    15  
    16  var _ = Suite(&TestSuite{})
    17  
    18  func (s *TestSuite) SetUpTest(c *C) {
    19  }
    20  
    21  func (s *TestSuite) TearDownTest(c *C) {
    22  }
    23  
    24  func (s *TestSuite) TestFail(c *C) {
    25  	result := icmd.RunCommand(GomplateBin, "-i", "{{ fail }}")
    26  	result.Assert(c, icmd.Expected{ExitCode: 1, Err: `template generation failed`})
    27  
    28  	result = icmd.RunCommand(GomplateBin, "-i", "{{ fail `some message` }}")
    29  	result.Assert(c, icmd.Expected{ExitCode: 1, Err: `some message`})
    30  }
    31  
    32  func (s *TestSuite) TestRequired(c *C) {
    33  	result := icmd.RunCmd(icmd.Command(GomplateBin,
    34  		"-i", `{{getenv "FOO" | required "FOO missing" }}`))
    35  	result.Assert(c, icmd.Expected{
    36  		ExitCode: 1,
    37  		Err:      "FOO missing",
    38  	})
    39  
    40  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    41  		"-i", `{{getenv "FOO" | required "FOO missing" }}`),
    42  		func(c *icmd.Cmd) {
    43  			c.Env = []string{"FOO=bar"}
    44  		})
    45  	result.Assert(c, icmd.Expected{
    46  		ExitCode: 0,
    47  		Out:      "bar",
    48  	})
    49  
    50  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    51  		"-d", "in=stdin:///?type=application/yaml",
    52  		"-i", `{{ (ds "in").foo | required "foo should not be null" }}`),
    53  		func(c *icmd.Cmd) {
    54  			c.Stdin = bytes.NewBufferString(`foo: null`)
    55  		})
    56  	result.Assert(c, icmd.Expected{
    57  		ExitCode: 1,
    58  		Err:      "foo should not be null",
    59  	})
    60  
    61  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    62  		"-d", "in=stdin:///?type=application/yaml",
    63  		"-i", `{{ (ds "in").foo | required }}`),
    64  		func(c *icmd.Cmd) {
    65  			c.Stdin = bytes.NewBufferString(`foo: []`)
    66  		})
    67  	result.Assert(c, icmd.Expected{
    68  		ExitCode: 0,
    69  		Out:      "[]",
    70  	})
    71  
    72  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    73  		"-d", "in=stdin:///?type=application/yaml",
    74  		"-i", `{{ (ds "in").foo | required }}`),
    75  		func(c *icmd.Cmd) {
    76  			c.Stdin = bytes.NewBufferString(`foo: {}`)
    77  		})
    78  	result.Assert(c, icmd.Expected{
    79  		ExitCode: 0,
    80  		Out:      "map[]",
    81  	})
    82  
    83  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    84  		"-d", "in=stdin:///?type=application/yaml",
    85  		"-i", `{{ (ds "in").foo | required }}`),
    86  		func(c *icmd.Cmd) {
    87  			c.Stdin = bytes.NewBufferString(`foo: 0`)
    88  		})
    89  	result.Assert(c, icmd.Expected{
    90  		ExitCode: 0,
    91  		Out:      "0",
    92  	})
    93  
    94  	result = icmd.RunCmd(icmd.Command(GomplateBin,
    95  		"-d", "in=stdin:///?type=application/yaml",
    96  		"-i", `{{ (ds "in").foo | required }}`),
    97  		func(c *icmd.Cmd) {
    98  			c.Stdin = bytes.NewBufferString(`foo: false`)
    99  		})
   100  	result.Assert(c, icmd.Expected{
   101  		ExitCode: 0,
   102  		Out:      "false",
   103  	})
   104  }