ensure reliable cleanup of test repository (#21880)

This commit is contained in:
Michael Eischer
2026-06-14 14:25:00 +02:00
committed by Michael Eischer
parent cb24c4f566
commit a3fa3eb182
5 changed files with 30 additions and 42 deletions
+12 -11
View File
@@ -137,10 +137,18 @@ func SetupTarTestFixture(t testing.TB, outputDir, tarFile string) {
// 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()) {
func Env(t testing.TB, repoFixture string) string {
t.Helper()
tempdir, err := os.MkdirTemp(TestTempDir, "restic-test-env-")
OK(t, err)
var tempdir string
if TestCleanupTempDirs {
tempdir = t.TempDir()
} else {
var err error
tempdir, err = os.MkdirTemp(TestTempDir, "restic-test-env-")
OK(t, err)
t.Logf("leaving temporary directory %v used for test", tempdir)
}
fd, err := os.Open(repoFixture)
if err != nil {
@@ -150,14 +158,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)
}
return filepath.Join(tempdir, "repo")
}
func isFile(fi os.FileInfo) bool {