From 17ea5b1be775f13b5fa2500beca8248f3a4c05ab Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 4 Sep 2016 16:28:47 +0200 Subject: [PATCH] Add test.TempDir --- src/restic/test/helpers.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/restic/test/helpers.go b/src/restic/test/helpers.go index d363e09c9..e91528b10 100644 --- a/src/restic/test/helpers.go +++ b/src/restic/test/helpers.go @@ -170,6 +170,25 @@ func SetupTarTestFixture(t testing.TB, outputDir, tarFile string) { OK(t, cmd.Run()) } +func cleanupDir(t testing.TB, dir string) { + if !TestCleanupTempDirs { + t.Logf("leaving temporary directory %v used for test", dir) + return + } + + RemoveAll(t, dir) +} + +// TempDir returns a temporary directory. +func TempDir(t testing.TB) (string, func()) { + tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-") + OK(t, err) + + return tempdir, func() { + cleanupDir(t, tempdir) + } +} + // Env creates a test environment and extracts the repository fixture. // Returned is the repo path and a cleanup function. func Env(t testing.TB, repoFixture string) (repodir string, cleanup func()) { @@ -185,12 +204,7 @@ func Env(t testing.TB, repoFixture string) (repodir string, cleanup func()) { SetupTarTestFixture(t, tempdir, repoFixture) return filepath.Join(tempdir, "repo"), func() { - if !TestCleanupTempDirs { - t.Logf("leaving temporary directory %v used for test", tempdir) - return - } - - RemoveAll(t, tempdir) + cleanupDir(t, tempdir) } }