summaryrefslogtreecommitdiff
path: root/src/path
diff options
context:
space:
mode:
authorianwoolf <btw515wolf2@gmail.com>2021-04-01 01:27:29 +0800
committerEmmanuel Odeke <emmanuel@orijtech.com>2021-04-04 00:01:03 +0000
commit776d8d387c262d3dd159f7868b3c04fb0f617865 (patch)
tree844be480840d110e3bb9cc3fffaed9c6e7377323 /src/path
parent9e7bc80b31088dc62faf4776ffdb1a2e27afa94e (diff)
downloadgo-git-776d8d387c262d3dd159f7868b3c04fb0f617865.tar.gz
os, path/filepath: use T.Cleanup to restore the original working directory
Updates #45182 Change-Id: Iaf3bdcc345c72fa9669fdc99908ada4e89904edd Reviewed-on: https://go-review.googlesource.com/c/go/+/306290 Trust: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/path')
-rw-r--r--src/path/filepath/path_test.go30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go
index 1d9889d320..51eca49e4c 100644
--- a/src/path/filepath/path_test.go
+++ b/src/path/filepath/path_test.go
@@ -410,6 +410,25 @@ func mark(d fs.DirEntry, err error, errors *[]error, clear bool) error {
return nil
}
+// chdir changes the current working directory to the named directory,
+// and then restore the original working directory at the end of the test.
+func chdir(t *testing.T, dir string) {
+ olddir, err := os.Getwd()
+ if err != nil {
+ t.Fatalf("getwd %s: %v", dir, err)
+ }
+ if err := os.Chdir(dir); err != nil {
+ t.Fatalf("chdir %s: %v", dir, err)
+ }
+
+ t.Cleanup(func() {
+ if err := os.Chdir(olddir); err != nil {
+ t.Errorf("restore original working directory %s: %v", olddir, err)
+ os.Exit(1)
+ }
+ })
+}
+
func chtmpdir(t *testing.T) (restore func()) {
oldwd, err := os.Getwd()
if err != nil {
@@ -1496,16 +1515,7 @@ func TestEvalSymlinksAboveRootChdir(t *testing.T) {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
-
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(wd)
-
- if err := os.Chdir(tmpDir); err != nil {
- t.Fatal(err)
- }
+ chdir(t, tmpDir)
subdir := filepath.Join("a", "b")
if err := os.MkdirAll(subdir, 0777); err != nil {