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

     1  //+build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"bytes"
     7  	"io/ioutil"
     8  	"os"
     9  	"runtime"
    10  
    11  	. "gopkg.in/check.v1"
    12  
    13  	"github.com/gotestyourself/gotestyourself/assert"
    14  	"github.com/gotestyourself/gotestyourself/assert/cmp"
    15  	"github.com/gotestyourself/gotestyourself/fs"
    16  	"github.com/gotestyourself/gotestyourself/icmd"
    17  )
    18  
    19  type BasicSuite struct {
    20  	tmpDir *fs.Dir
    21  }
    22  
    23  var _ = Suite(&BasicSuite{})
    24  
    25  func (s *BasicSuite) SetUpTest(c *C) {
    26  	s.tmpDir = fs.NewDir(c, "gomplate-inttests",
    27  		fs.WithFile("one", "hi\n", fs.WithMode(0640)),
    28  		fs.WithFile("two", "hello\n"))
    29  }
    30  
    31  func (s *BasicSuite) TearDownTest(c *C) {
    32  	s.tmpDir.Remove()
    33  }
    34  
    35  func (s *BasicSuite) TestReportsVersion(c *C) {
    36  	result := icmd.RunCommand(GomplateBin, "-v")
    37  	result.Assert(c, icmd.Success)
    38  	assert.Assert(c, cmp.Contains(result.Combined(), "gomplate version "))
    39  }
    40  
    41  func (s *BasicSuite) TestTakesStdinByDefault(c *C) {
    42  	result := icmd.RunCmd(icmd.Command(GomplateBin), func(cmd *icmd.Cmd) {
    43  		cmd.Stdin = bytes.NewBufferString("hello world")
    44  	})
    45  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "hello world"})
    46  }
    47  
    48  func (s *BasicSuite) TestTakesStdinWithFileFlag(c *C) {
    49  	result := icmd.RunCmd(icmd.Command(GomplateBin, "--file", "-"), func(cmd *icmd.Cmd) {
    50  		cmd.Stdin = bytes.NewBufferString("hello world")
    51  	})
    52  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "hello world"})
    53  }
    54  func (s *BasicSuite) TestWritesToStdoutWithOutFlag(c *C) {
    55  	result := icmd.RunCmd(icmd.Command(GomplateBin, "--out", "-"), func(cmd *icmd.Cmd) {
    56  		cmd.Stdin = bytes.NewBufferString("hello world")
    57  	})
    58  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "hello world"})
    59  }
    60  
    61  func (s *BasicSuite) TestIgnoresStdinWithInFlag(c *C) {
    62  	result := icmd.RunCmd(icmd.Command(GomplateBin, "--in", "hi"), func(cmd *icmd.Cmd) {
    63  		cmd.Stdin = bytes.NewBufferString("hello world")
    64  	})
    65  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "hi"})
    66  }
    67  
    68  func (s *BasicSuite) TestErrorsWithInputOutputImbalance(c *C) {
    69  	result := icmd.RunCmd(icmd.Command(GomplateBin,
    70  		"-f", s.tmpDir.Join("one"),
    71  		"-f", s.tmpDir.Join("two"),
    72  		"-o", s.tmpDir.Join("out")), func(cmd *icmd.Cmd) {
    73  		cmd.Stdin = bytes.NewBufferString("hello world")
    74  	})
    75  	result.Assert(c, icmd.Expected{
    76  		ExitCode: 1,
    77  		Err:      "must provide same number of --out (1) as --file (2) options",
    78  	})
    79  }
    80  
    81  func (s *BasicSuite) TestRoutesInputsToProperOutputs(c *C) {
    82  	oneOut := s.tmpDir.Join("one.out")
    83  	twoOut := s.tmpDir.Join("two.out")
    84  	result := icmd.RunCmd(icmd.Command(GomplateBin,
    85  		"-f", s.tmpDir.Join("one"),
    86  		"-f", s.tmpDir.Join("two"),
    87  		"-o", oneOut,
    88  		"-o", twoOut), func(cmd *icmd.Cmd) {
    89  		cmd.Stdin = bytes.NewBufferString("hello world")
    90  	})
    91  	result.Assert(c, icmd.Success)
    92  
    93  	testdata := []struct {
    94  		path    string
    95  		mode    os.FileMode
    96  		content string
    97  	}{
    98  		{oneOut, 0640, "hi\n"},
    99  		{twoOut, 0644, "hello\n"},
   100  	}
   101  	for _, v := range testdata {
   102  		info, err := os.Stat(v.path)
   103  		assert.NilError(c, err)
   104  		if runtime.GOOS != "windows" {
   105  			assert.Equal(c, v.mode, info.Mode())
   106  		}
   107  		content, err := ioutil.ReadFile(v.path)
   108  		assert.NilError(c, err)
   109  		assert.Equal(c, v.content, string(content))
   110  	}
   111  }
   112  
   113  func (s *BasicSuite) TestFlagRules(c *C) {
   114  	result := icmd.RunCommand(GomplateBin, "-f", "-", "-i", "HELLO WORLD")
   115  	result.Assert(c, icmd.Expected{
   116  		ExitCode: 1,
   117  		Err:      "--in and --file may not be used together",
   118  	})
   119  
   120  	result = icmd.RunCommand(GomplateBin, "--output-dir", ".")
   121  	result.Assert(c, icmd.Expected{
   122  		ExitCode: 1,
   123  		Err:      "--input-dir must be set when --output-dir is set",
   124  	})
   125  
   126  	result = icmd.RunCommand(GomplateBin, "--input-dir", ".", "--in", "param")
   127  	result.Assert(c, icmd.Expected{
   128  		ExitCode: 1,
   129  		Err:      "--input-dir can not be used together with --in or --file",
   130  	})
   131  
   132  	result = icmd.RunCommand(GomplateBin, "--input-dir", ".", "--file", "input.txt")
   133  	result.Assert(c, icmd.Expected{
   134  		ExitCode: 1,
   135  		Err:      "--input-dir can not be used together with --in or --file",
   136  	})
   137  
   138  	result = icmd.RunCommand(GomplateBin, "--output-dir", ".", "--out", "param")
   139  	result.Assert(c, icmd.Expected{
   140  		ExitCode: 1,
   141  		Err:      "--output-dir can not be used together with --out",
   142  	})
   143  
   144  	result = icmd.RunCommand(GomplateBin, "--output-map", ".", "--out", "param")
   145  	result.Assert(c, icmd.Expected{
   146  		ExitCode: 1,
   147  		Err:      "--output-map can not be used together with --out or --output-dir",
   148  	})
   149  }
   150  
   151  func (s *BasicSuite) TestDelimsChangedThroughOpts(c *C) {
   152  	result := icmd.RunCommand(GomplateBin,
   153  		"--left-delim", "((",
   154  		"--right-delim", "))",
   155  		"-i", `((print "hi"))`)
   156  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "hi"})
   157  }
   158  
   159  func (s *BasicSuite) TestDelimsChangedThroughEnvVars(c *C) {
   160  	result := icmd.RunCmd(icmd.Command(GomplateBin, "-i", `<<print "hi">>`),
   161  		func(cmd *icmd.Cmd) {
   162  			cmd.Env = []string{
   163  				"GOMPLATE_LEFT_DELIM=<<",
   164  				"GOMPLATE_RIGHT_DELIM=>>",
   165  			}
   166  		})
   167  	result.Assert(c, icmd.Expected{ExitCode: 0, Out: "hi"})
   168  }
   169  
   170  func (s *BasicSuite) TestUnknownArgErrors(c *C) {
   171  	result := icmd.RunCommand(GomplateBin, "-in", "flibbit")
   172  	result.Assert(c, icmd.Expected{ExitCode: 1, Err: `unknown command "flibbit" for "gomplate"`})
   173  }
   174  
   175  func (s *BasicSuite) TestExecCommand(c *C) {
   176  	out := s.tmpDir.Join("out")
   177  	result := icmd.RunCmd(icmd.Command(GomplateBin,
   178  		"-i", `{{print "hello world"}}`,
   179  		"-o", out,
   180  		"--", "cat", out))
   181  	result.Assert(c, icmd.Expected{
   182  		ExitCode: 0,
   183  		Out:      "hello world",
   184  	})
   185  }
   186  
   187  func (s *BasicSuite) TestEmptyOutputSuppression(c *C) {
   188  	out := s.tmpDir.Join("out")
   189  	result := icmd.RunCmd(icmd.Command(GomplateBin,
   190  		"-i",
   191  		`{{print "\t  \n\n\r\n\t\t     \v\n"}}`,
   192  		"-o", out),
   193  		func(cmd *icmd.Cmd) {
   194  			cmd.Env = []string{
   195  				"GOMPLATE_SUPPRESS_EMPTY=true",
   196  			}
   197  		})
   198  	result.Assert(c, icmd.Expected{ExitCode: 0})
   199  	_, err := os.Stat(out)
   200  	assert.Equal(c, true, os.IsNotExist(err))
   201  }