github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/pkg/testutil/tempfile/tempfile.go (about) 1 package tempfile 2 3 import ( 4 "github.com/docker/docker/pkg/testutil/assert" 5 "io/ioutil" 6 "os" 7 ) 8 9 // TempFile is a temporary file that can be used with unit tests. TempFile 10 // reduces the boilerplate setup required in each test case by handling 11 // setup errors. 12 type TempFile struct { 13 File *os.File 14 } 15 16 // NewTempFile returns a new temp file with contents 17 func NewTempFile(t assert.TestingT, prefix string, content string) *TempFile { 18 file, err := ioutil.TempFile("", prefix+"-") 19 assert.NilError(t, err) 20 21 _, err = file.Write([]byte(content)) 22 assert.NilError(t, err) 23 file.Close() 24 return &TempFile{File: file} 25 } 26 27 // Name returns the filename 28 func (f *TempFile) Name() string { 29 return f.File.Name() 30 } 31 32 // Remove removes the file 33 func (f *TempFile) Remove() { 34 os.Remove(f.Name()) 35 }