Improve test helpers

This commit is contained in:
Alexander Neumann 2017-05-01 22:46:32 +02:00
parent 6f5fd72738
commit f10c24e404

View file

@ -10,6 +10,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"reflect" "reflect"
"restic/errors"
"runtime" "runtime"
"testing" "testing"
@ -122,7 +123,7 @@ func SetupTarTestFixture(t testing.TB, outputDir, tarFile string) {
// Env creates a test environment and extracts the repository fixture. // Env creates a test environment and extracts the repository fixture.
// Returned is the repo path and a cleanup function. // 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) (repodir string, cleanup func()) {
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-") tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-env-")
OK(t, err) OK(t, err)
fd, err := os.Open(repoFixture) fd, err := os.Open(repoFixture)
@ -151,7 +152,11 @@ func isFile(fi os.FileInfo) bool {
// This is mainly used for tests on Windows, which is unable to delete a file // This is mainly used for tests on Windows, which is unable to delete a file
// set read-only. // set read-only.
func ResetReadOnly(t testing.TB, dir string) { func ResetReadOnly(t testing.TB, dir string) {
OK(t, filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error { err := filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
if fi == nil {
return err
}
if fi.IsDir() { if fi.IsDir() {
return os.Chmod(path, 0777) return os.Chmod(path, 0777)
} }
@ -161,14 +166,22 @@ func ResetReadOnly(t testing.TB, dir string) {
} }
return nil return nil
})) })
if os.IsNotExist(errors.Cause(err)) {
err = nil
}
OK(t, err)
} }
// RemoveAll recursively resets the read-only flag of all files and dirs and // RemoveAll recursively resets the read-only flag of all files and dirs and
// afterwards uses os.RemoveAll() to remove the path. // afterwards uses os.RemoveAll() to remove the path.
func RemoveAll(t testing.TB, path string) { func RemoveAll(t testing.TB, path string) {
ResetReadOnly(t, path) ResetReadOnly(t, path)
OK(t, os.RemoveAll(path)) err := os.RemoveAll(path)
if os.IsNotExist(errors.Cause(err)) {
err = nil
}
OK(t, err)
} }
// TempDir returns a temporary directory that is removed when cleanup is // TempDir returns a temporary directory that is removed when cleanup is