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

     1  //+build integration
     2  
     3  package integration
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/gotestyourself/gotestyourself/assert"
    11  
    12  	. "gopkg.in/check.v1"
    13  
    14  	"github.com/gotestyourself/gotestyourself/fs"
    15  	"github.com/gotestyourself/gotestyourself/icmd"
    16  )
    17  
    18  type FileSuite struct {
    19  	tmpDir *fs.Dir
    20  }
    21  
    22  var _ = Suite(&FileSuite{})
    23  
    24  func (s *FileSuite) SetUpSuite(c *C) {
    25  	s.tmpDir = fs.NewDir(c, "gomplate-inttests",
    26  		fs.WithFile("one", "hi\n"),
    27  		fs.WithFile("two", "hello\n"))
    28  }
    29  
    30  func (s *FileSuite) TearDownSuite(c *C) {
    31  	s.tmpDir.Remove()
    32  }
    33  
    34  func (s *FileSuite) TestReadsFile(c *C) {
    35  	inOutTest(c, "{{ file.Read `"+s.tmpDir.Join("one")+"`}}", "hi")
    36  }
    37  
    38  func (s *FileSuite) TestWrite(c *C) {
    39  	outDir := s.tmpDir.Join("writeOutput")
    40  	os.MkdirAll(outDir, 0755)
    41  	result := icmd.RunCmd(icmd.Command(GomplateBin,
    42  		"-i", `{{ "hello world" | file.Write "./out" }}`,
    43  	), func(cmd *icmd.Cmd) {
    44  		cmd.Dir = outDir
    45  	})
    46  	result.Assert(c, icmd.Expected{ExitCode: 0})
    47  	out, err := ioutil.ReadFile(filepath.Join(outDir, "out"))
    48  	assert.NilError(c, err)
    49  	assert.Equal(c, "hello world", string(out))
    50  }