7 "github.com/stretchr/testify/require"
10 // TempFile is a temporary file that can be used with unit tests. TempFile
11 // reduces the boilerplate setup required in each test case by handling
13 type TempFile struct {
17 // NewTempFile returns a new temp file with contents
18 func NewTempFile(t require.TestingT, prefix string, content string) *TempFile {
19 file, err := ioutil.TempFile("", prefix+"-")
20 require.NoError(t, err)
22 _, err = file.Write([]byte(content))
23 require.NoError(t, err)
25 return &TempFile{File: file}
28 // Name returns the filename
29 func (f *TempFile) Name() string {
33 // Remove removes the file
34 func (f *TempFile) Remove() {
38 // TempDir is a temporary directory that can be used with unit tests. TempDir
39 // reduces the boilerplate setup required in each test case by handling
45 // NewTempDir returns a new temp file with contents
46 func NewTempDir(t require.TestingT, prefix string) *TempDir {
47 path, err := ioutil.TempDir("", prefix+"-")
48 require.NoError(t, err)
50 return &TempDir{Path: path}
53 // Remove removes the file
54 func (f *TempDir) Remove() {