From 80a864c52c57344e673ed9ef25740c2f55261b20 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 26 Mar 2017 20:40:45 +0200 Subject: [PATCH] test: Add TempDir() helper --- src/restic/test/helpers.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/restic/test/helpers.go b/src/restic/test/helpers.go index 4e19000e8..a6dffc0a4 100644 --- a/src/restic/test/helpers.go +++ b/src/restic/test/helpers.go @@ -170,3 +170,21 @@ func RemoveAll(t testing.TB, path string) { ResetReadOnly(t, path) OK(t, os.RemoveAll(path)) } + +// TempDir returns a temporary directory that is removed when cleanup is +// called, except if TestCleanupTempDirs is set to false. +func TempDir(t testing.TB) (path string, cleanup func()) { + tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-") + if err != nil { + t.Fatal(err) + } + + return tempdir, func() { + if !TestCleanupTempDirs { + t.Logf("leaving temporary directory %v used for test", tempdir) + return + } + + RemoveAll(t, tempdir) + } +}