diff --git a/internal/fs/helpers.go b/internal/fs/helpers.go index 7235834ae..768cc975b 100644 --- a/internal/fs/helpers.go +++ b/internal/fs/helpers.go @@ -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 +}