2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-29 16:23:59 +00:00

fs: Add TestChdir()

This commit is contained in:
Alexander Neumann 2017-12-16 00:01:28 +01:00
parent e4fdc5eb76
commit a472868e06
3 changed files with 29 additions and 45 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/restic/restic/internal/archiver"
"github.com/restic/restic/internal/crypto"
"github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
@ -226,27 +227,6 @@ func TestArchiveEmptySnapshot(t *testing.T) {
}
}
func chdir(t testing.TB, target string) (cleanup func()) {
curdir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
t.Logf("chdir to %v", target)
err = os.Chdir(target)
if err != nil {
t.Fatal(err)
}
return func() {
t.Logf("chdir back to %v", curdir)
err := os.Chdir(curdir)
if err != nil {
t.Fatal(err)
}
}
}
func TestArchiveNameCollision(t *testing.T) {
repo, cleanup := repository.TestRepository(t)
defer cleanup()
@ -260,7 +240,7 @@ func TestArchiveNameCollision(t *testing.T) {
rtest.OK(t, ioutil.WriteFile(filepath.Join(dir, "testfile"), []byte("testfile1"), 0644))
rtest.OK(t, ioutil.WriteFile(filepath.Join(dir, "root", "testfile"), []byte("testfile2"), 0644))
defer chdir(t, root)()
defer fs.TestChdir(t, root)()
arch := archiver.New(repo)

View File

@ -1,6 +1,9 @@
package fs
import "os"
import (
"os"
"testing"
)
// IsRegularFile returns true if fi belongs to a normal file. If fi is nil,
// false is returned.
@ -11,3 +14,25 @@ func IsRegularFile(fi os.FileInfo) bool {
return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0
}
// TestChdir changes the current directory to dest, the function back returns to the previous directory.
func TestChdir(t testing.TB, dest string) (back func()) {
prev, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
t.Logf("chdir to %v", dest)
err = os.Chdir(dest)
if err != nil {
t.Fatal(err)
}
return func() {
t.Logf("chdir back to %v", prev)
err = os.Chdir(prev)
if err != nil {
t.Fatal(err)
}
}
}

View File

@ -346,27 +346,6 @@ func TestRestorer(t *testing.T) {
}
}
func chdir(t testing.TB, target string) func() {
prev, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
t.Logf("chdir to %v", target)
err = os.Chdir(target)
if err != nil {
t.Fatal(err)
}
return func() {
t.Logf("chdir back to %v", prev)
err = os.Chdir(prev)
if err != nil {
t.Fatal(err)
}
}
}
func TestRestorerRelative(t *testing.T) {
var tests = []struct {
Snapshot
@ -406,7 +385,7 @@ func TestRestorerRelative(t *testing.T) {
tempdir, cleanup := rtest.TempDir(t)
defer cleanup()
cleanup = chdir(t, tempdir)
cleanup = fs.TestChdir(t, tempdir)
defer cleanup()
errors := make(map[string]string)