From a472868e06211c6ef217115d88d0eee8977ebea0 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 16 Dec 2017 00:01:28 +0100 Subject: [PATCH] fs: Add TestChdir() --- internal/archiver/archiver_test.go | 24 ++---------------------- internal/fs/helpers.go | 27 ++++++++++++++++++++++++++- internal/restic/restorer_test.go | 23 +---------------------- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/internal/archiver/archiver_test.go b/internal/archiver/archiver_test.go index 035355a32..1e8b9355f 100644 --- a/internal/archiver/archiver_test.go +++ b/internal/archiver/archiver_test.go @@ -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) diff --git a/internal/fs/helpers.go b/internal/fs/helpers.go index b7f7ad6ba..1789f2bca 100644 --- a/internal/fs/helpers.go +++ b/internal/fs/helpers.go @@ -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) + } + } +} diff --git a/internal/restic/restorer_test.go b/internal/restic/restorer_test.go index ec3282e5d..9b1758dc9 100644 --- a/internal/restic/restorer_test.go +++ b/internal/restic/restorer_test.go @@ -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)