github.com/hairyhenderson/gomplate/v3@v3.11.7/internal/tests/integration/file_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"gotest.tools/v3/assert"
     9  	"gotest.tools/v3/fs"
    10  )
    11  
    12  func setupFileTest(t *testing.T) *fs.Dir {
    13  	tmpDir := fs.NewDir(t, "gomplate-inttests",
    14  		fs.WithFile("one", "hi\n"),
    15  		fs.WithFile("two", "hello\n"))
    16  	t.Cleanup(tmpDir.Remove)
    17  
    18  	return tmpDir
    19  }
    20  
    21  func TestFile_ReadsFile(t *testing.T) {
    22  	tmpDir := setupFileTest(t)
    23  
    24  	inOutTest(t, "{{ file.Read `"+tmpDir.Join("one")+"`}}", "hi\n")
    25  }
    26  
    27  func TestFile_Write(t *testing.T) {
    28  	tmpDir := setupFileTest(t)
    29  
    30  	outDir := tmpDir.Join("writeOutput")
    31  	os.MkdirAll(outDir, 0o755)
    32  	o, e, err := cmd(t, "-i", `{{ "hello world" | file.Write "./out" }}`).
    33  		withDir(outDir).run()
    34  	assertSuccess(t, o, e, err, "")
    35  
    36  	out, err := os.ReadFile(filepath.Join(outDir, "out"))
    37  	assert.NilError(t, err)
    38  	assert.Equal(t, "hello world", string(out))
    39  }