github.com/gofunct/common@v0.0.0-20190131174352-fd058c7fbf22/pkg/temp/temptest/file.go (about) 1 package temptest 2 3 import ( 4 "bytes" 5 "errors" 6 "io" 7 ) 8 9 // FakeFile is an implementation of a WriteCloser, that records what has 10 // been written in the file (in a bytes.Buffer) and if the file has been 11 // closed. 12 type FakeFile struct { 13 Buffer bytes.Buffer 14 Closed bool 15 } 16 17 var _ io.WriteCloser = &FakeFile{} 18 19 // Write appends the contents of p to the Buffer. If the file has 20 // already been closed, an error is returned. 21 func (f *FakeFile) Write(p []byte) (n int, err error) { 22 if f.Closed { 23 return 0, errors.New("can't write to closed FakeFile") 24 } 25 return f.Buffer.Write(p) 26 } 27 28 // Close records that the file has been closed. If the file has already 29 // been closed, an error is returned. 30 func (f *FakeFile) Close() error { 31 if f.Closed { 32 return errors.New("FakeFile was closed multiple times") 33 } 34 f.Closed = true 35 return nil 36 }