mirror of
https://github.com/restic/restic.git
synced 2026-05-25 03:25:35 +00:00
test helpers: add RemoveAll and ResetReadOnly
This is mainly needed in Windows, where files and dirs cannot be removed unless they are writeable.
This commit is contained in:
+30
-1
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user