fs: Add TestTempFile

This commit is contained in:
Alexander Neumann 2018-05-01 14:38:41 +02:00
parent ecbbd851a1
commit bc68d55e94
1 changed files with 20 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package fs
import (
"io/ioutil"
"os"
"testing"
@ -41,3 +42,22 @@ func TestChdir(t testing.TB, dest string) (back func()) {
}
}
}
// TestTempFile returns a new temporary file, which is removed when cleanup()
// is called.
func TestTempFile(t testing.TB, prefix string) (File, func()) {
f, err := ioutil.TempFile("", prefix)
if err != nil {
t.Fatal(err)
}
cleanup := func() {
_ = f.Close()
err = Remove(f.Name())
if err != nil {
t.Fatal(err)
}
}
return f, cleanup
}