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/filter"
"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"
@ -66,7 +65,7 @@ func testRunBackupAssumeFailure(t testing.TB, dir string, target []string, opts
gopts.stdout = ioutil.Discard
t.Logf("backing up %v in %v", target, dir)
if dir != "" {
cleanup := fs.TestChdir(t, dir)
cleanup := rtest.Chdir(t, dir)
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
// 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()
curdir, err := os.Getwd()

View File

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

View File

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

View File

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

View File

@ -444,7 +444,7 @@ func TestTree(t *testing.T) {
TestCreateFiles(t, tempdir, test.src)
back := fs.TestChdir(t, tempdir)
back := restictest.Chdir(t, tempdir)
defer back()
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{})
back := fs.TestChdir(t, tmpdir)
back := rtest.Chdir(t, tmpdir)
defer back()
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
}
// 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()
// is called.
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)
defer cleanup()
cleanup = fs.TestChdir(t, tempdir)
cleanup = rtest.Chdir(t, tempdir)
defer cleanup()
errors := make(map[string]string)

View File

@ -202,3 +202,29 @@ func TempDir(t testing.TB) (path string, cleanup func()) {
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)
}
}
}