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

@@ -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)
}
}
}