2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 10:58:55 +00:00

Move internal/fs.TestChdir to internal/test.Chdir

This commit is contained in:
greatroar 2020-02-17 13:24:09 +01:00
parent b10dce541e
commit 9abef3bf1a
9 changed files with 44 additions and 44 deletions

View File

@ -21,7 +21,6 @@ import (
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/filter" "github.com/restic/restic/internal/filter"
"github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
@ -66,7 +65,7 @@ func testRunBackupAssumeFailure(t testing.TB, dir string, target []string, opts
gopts.stdout = ioutil.Discard gopts.stdout = ioutil.Discard
t.Logf("backing up %v in %v", target, dir) t.Logf("backing up %v in %v", target, dir)
if dir != "" { if dir != "" {
cleanup := fs.TestChdir(t, dir) cleanup := rtest.Chdir(t, dir)
defer cleanup() defer cleanup()
} }
@ -1003,7 +1002,7 @@ func TestRestoreLatest(t *testing.T) {
// chdir manually here so we can get the current directory. This is not the // chdir manually here so we can get the current directory. This is not the
// same as the temp dir returned by ioutil.TempDir() on darwin. // same as the temp dir returned by ioutil.TempDir() on darwin.
back := fs.TestChdir(t, filepath.Dir(env.testdata)) back := rtest.Chdir(t, filepath.Dir(env.testdata))
defer back() defer back()
curdir, err := os.Getwd() curdir, err := os.Getwd()

View File

@ -803,7 +803,7 @@ func TestArchiverSaveDir(t *testing.T) {
chdir = filepath.Join(chdir, test.chdir) chdir = filepath.Join(chdir, test.chdir)
} }
back := fs.TestChdir(t, chdir) back := restictest.Chdir(t, chdir)
defer back() defer back()
fi, err := fs.Lstat(test.target) fi, err := fs.Lstat(test.target)
@ -1063,7 +1063,7 @@ func TestArchiverSaveTree(t *testing.T) {
arch.runWorkers(ctx, &tmb) arch.runWorkers(ctx, &tmb)
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
if test.prepare != nil { if test.prepare != nil {
@ -1353,7 +1353,7 @@ func TestArchiverSnapshot(t *testing.T) {
chdir = filepath.Join(chdir, filepath.FromSlash(test.chdir)) chdir = filepath.Join(chdir, filepath.FromSlash(test.chdir))
} }
back := fs.TestChdir(t, chdir) back := restictest.Chdir(t, chdir)
defer back() defer back()
var targets []string var targets []string
@ -1507,7 +1507,7 @@ func TestArchiverSnapshotSelect(t *testing.T) {
arch := New(repo, fs.Track{FS: fs.Local{}}, Options{}) arch := New(repo, fs.Track{FS: fs.Local{}}, Options{})
arch.Select = test.selFn arch.Select = test.selFn
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
targets := []string{"."} targets := []string{"."}
@ -1614,7 +1614,7 @@ func TestArchiverParent(t *testing.T) {
arch := New(repo, testFS, Options{}) arch := New(repo, testFS, Options{})
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
_, firstSnapshotID, err := arch.Snapshot(ctx, []string{"."}, SnapshotOptions{Time: time.Now()}) _, firstSnapshotID, err := arch.Snapshot(ctx, []string{"."}, SnapshotOptions{Time: time.Now()})
@ -1774,7 +1774,7 @@ func TestArchiverErrorReporting(t *testing.T) {
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src) tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src)
defer cleanup() defer cleanup()
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
if test.prepare != nil { if test.prepare != nil {
@ -1915,7 +1915,7 @@ func TestArchiverAbortEarlyOnError(t *testing.T) {
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src) tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src)
defer cleanup() defer cleanup()
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
testFS := &TrackFS{ testFS := &TrackFS{
@ -2046,7 +2046,7 @@ func TestMetadataChanged(t *testing.T) {
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, files) tempdir, repo, cleanup := prepareTempdirRepoSrc(t, files)
defer cleanup() defer cleanup()
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
// get metadata // get metadata
@ -2121,7 +2121,7 @@ func TestRacyFileSwap(t *testing.T) {
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, files) tempdir, repo, cleanup := prepareTempdirRepoSrc(t, files)
defer cleanup() defer cleanup()
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
// get metadata of current folder // get metadata of current folder

View File

@ -88,7 +88,7 @@ func TestScanner(t *testing.T) {
TestCreateFiles(t, tempdir, test.src) TestCreateFiles(t, tempdir, test.src)
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
cur, err := os.Getwd() cur, err := os.Getwd()
@ -225,7 +225,7 @@ func TestScannerError(t *testing.T) {
TestCreateFiles(t, tempdir, test.src) TestCreateFiles(t, tempdir, test.src)
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
cur, err := os.Getwd() cur, err := os.Getwd()
@ -299,7 +299,7 @@ func TestScannerCancel(t *testing.T) {
TestCreateFiles(t, tempdir, src) TestCreateFiles(t, tempdir, src)
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
cur, err := os.Getwd() cur, err := os.Getwd()

View File

@ -495,7 +495,7 @@ func TestTestEnsureSnapshot(t *testing.T) {
createFilesAt(t, targetDir, test.files) createFilesAt(t, targetDir, test.files)
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
repo, cleanup := repository.TestRepository(t) repo, cleanup := repository.TestRepository(t)

View File

@ -444,7 +444,7 @@ func TestTree(t *testing.T) {
TestCreateFiles(t, tempdir, test.src) TestCreateFiles(t, tempdir, test.src)
back := fs.TestChdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
tree, err := NewTree(fs.Local{}, test.targets) tree, err := NewTree(fs.Local{}, test.targets)

View File

@ -79,7 +79,7 @@ func TestWriteTar(t *testing.T) {
arch := archiver.New(repo, fs.Track{FS: fs.Local{}}, archiver.Options{}) arch := archiver.New(repo, fs.Track{FS: fs.Local{}}, archiver.Options{})
back := fs.TestChdir(t, tmpdir) back := rtest.Chdir(t, tmpdir)
defer back() defer back()
sn, _, err := arch.Snapshot(ctx, []string{"."}, archiver.SnapshotOptions{}) sn, _, err := arch.Snapshot(ctx, []string{"."}, archiver.SnapshotOptions{})

View File

@ -16,31 +16,6 @@ func IsRegularFile(fi os.FileInfo) bool {
return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0 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()) {
t.Helper()
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.Helper()
t.Logf("chdir back to %v", prev)
err = os.Chdir(prev)
if err != nil {
t.Fatal(err)
}
}
}
// TestTempFile returns a new temporary file, which is removed when cleanup() // TestTempFile returns a new temporary file, which is removed when cleanup()
// is called. // is called.
func TestTempFile(t testing.TB, prefix string) (File, func()) { func TestTempFile(t testing.TB, prefix string) (File, func()) {

View File

@ -442,7 +442,7 @@ func TestRestorerRelative(t *testing.T) {
tempdir, cleanup := rtest.TempDir(t) tempdir, cleanup := rtest.TempDir(t)
defer cleanup() defer cleanup()
cleanup = fs.TestChdir(t, tempdir) cleanup = rtest.Chdir(t, tempdir)
defer cleanup() defer cleanup()
errors := make(map[string]string) errors := make(map[string]string)

View File

@ -202,3 +202,29 @@ func TempDir(t testing.TB) (path string, cleanup func()) {
RemoveAll(t, tempdir) RemoveAll(t, tempdir)
} }
} }
// Chdir changes the current directory to dest.
// The function back returns to the previous directory.
func Chdir(t testing.TB, dest string) (back func()) {
t.Helper()
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.Helper()
t.Logf("chdir back to %v", prev)
err = os.Chdir(prev)
if err != nil {
t.Fatal(err)
}
}
}