diff --git a/cmd/restic/integration_fuse_test.go b/cmd/restic/integration_fuse_test.go index 897e1fc75..f9e75ce66 100644 --- a/cmd/restic/integration_fuse_test.go +++ b/cmd/restic/integration_fuse_test.go @@ -56,7 +56,7 @@ func cmdMount(t testing.TB, global GlobalOptions, dir string, ready, done chan s cmd := &CmdMount{global: &global, ready: ready, done: done} OK(t, cmd.Execute([]string{dir})) if TestCleanup { - OK(t, os.RemoveAll(dir)) + RemoveAll(t, dir) } } @@ -101,7 +101,7 @@ func TestMount(t *testing.T) { OK(t, err) // We remove the mountpoint now to check that cmdMount creates it - OK(t, os.RemoveAll(mountpoint)) + RemoveAll(t, mountpoint) ready := make(chan struct{}, 1) done := make(chan struct{}) diff --git a/cmd/restic/integration_helpers_test.go b/cmd/restic/integration_helpers_test.go index 6fd90ac3f..07d64af3b 100644 --- a/cmd/restic/integration_helpers_test.go +++ b/cmd/restic/integration_helpers_test.go @@ -216,7 +216,7 @@ func cleanupTempdir(t testing.TB, tempdir string) { return } - OK(t, os.RemoveAll(tempdir)) + RemoveAll(t, tempdir) } // withTestEnvironment creates a test environment and calls f with it. After f has @@ -245,5 +245,5 @@ func withTestEnvironment(t testing.TB, f func(*testEnvironment, GlobalOptions)) return } - OK(t, os.RemoveAll(tempdir)) + RemoveAll(t, tempdir) } diff --git a/node_test.go b/node_test.go index f6104b49b..4eaa014fe 100644 --- a/node_test.go +++ b/node_test.go @@ -109,7 +109,7 @@ func TestNodeRestoreAt(t *testing.T) { defer func() { if TestCleanup { - OK(t, os.RemoveAll(tempdir)) + RemoveAll(t, tempdir) } else { t.Logf("leaving tempdir at %v", tempdir) } diff --git a/test/helpers.go b/test/helpers.go index f04ac24ac..1b09b7a0e 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -59,6 +59,7 @@ func Equals(tb testing.TB, exp, act interface{}) { } } +// ParseID parses s as a backend.ID and panics if that fails. func ParseID(s string) backend.ID { id, err := backend.ParseID(s) if err != nil { @@ -123,7 +124,7 @@ func WithTestEnvironment(t testing.TB, repoFixture string, f func(repodir string return } - OK(t, os.RemoveAll(tempdir)) + RemoveAll(t, tempdir) } // OpenLocalRepo opens the local repository located at dir. @@ -137,3 +138,31 @@ func OpenLocalRepo(t testing.TB, dir string) *repository.Repository { return repo } + +func isFile(fi os.FileInfo) bool { + return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0 +} + +// ResetReadOnly recursively resets the read-only flag recursively for dir. +// This is mainly used for tests on Windows, which is unable to delete a file +// set read-only. +func ResetReadOnly(t testing.TB, dir string) { + OK(t, filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error { + if fi.IsDir() { + return os.Chmod(path, 0777) + } + + if isFile(fi) { + return os.Chmod(path, 0666) + } + + return nil + })) +} + +// RemoveAll recursively resets the read-only flag of all files and dirs and +// afterwards uses os.RemoveAll() to remove the path. +func RemoveAll(t testing.TB, path string) { + ResetReadOnly(t, path) + OK(t, os.RemoveAll(path)) +} diff --git a/tree_test.go b/tree_test.go index 2d9cb7b7e..725e80b14 100644 --- a/tree_test.go +++ b/tree_test.go @@ -50,7 +50,7 @@ func TestTree(t *testing.T) { dir := createTempDir(t) defer func() { if TestCleanup { - OK(t, os.RemoveAll(dir)) + RemoveAll(t, dir) } }() }