[go] os,path/filepath: use defer to restore the working directory

75 views
Skip to first unread message

Emmanuel Odeke (Gerrit)

unread,
Apr 3, 2021, 8:01:09 PM4/3/21
to ZiZhao Zhang, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Go Bot, Ian Lance Taylor, Manlio Perillo, Rob Pike, Brad Fitzpatrick, Robert Griesemer, golang-co...@googlegroups.com

Emmanuel Odeke submitted this change.

View Change

Approvals: Ian Lance Taylor: Looks good to me, approved Emmanuel Odeke: Trusted; Run TryBots Go Bot: TryBots succeeded
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 <emma...@orijtech.com>
Run-TryBot: Emmanuel Odeke <emma...@orijtech.com>
TryBot-Result: Go Bot <go...@golang.org>
Reviewed-by: Ian Lance Taylor <ia...@golang.org>
---
M src/os/os_windows_test.go
M src/os/removeall_test.go
M src/path/filepath/path_test.go
3 files changed, 45 insertions(+), 74 deletions(-)

diff --git a/src/os/os_windows_test.go b/src/os/os_windows_test.go
index b0929b4..b4339c3 100644
--- a/src/os/os_windows_test.go
+++ b/src/os/os_windows_test.go
@@ -35,16 +35,7 @@
t.Fatal(err)
}
defer os.RemoveAll(temp)
-
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(temp)
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(wd)
+ chdir(t, temp)

f, err := os.Create("a")
if err != nil {
@@ -94,16 +85,7 @@
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
-
- oldwd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(tmpdir)
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(oldwd)
+ chdir(t, tmpdir)

dir := filepath.Join(tmpdir, "dir")
err = os.Mkdir(dir, 0777)
@@ -444,15 +426,7 @@
}
defer os.RemoveAll(dir)

- oldwd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(dir)
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(oldwd)
+ chdir(t, dir)

shareName := "GoSymbolicLinkTestShare" // hope no conflictions
sharePath := filepath.Join(dir, shareName)
@@ -604,16 +578,7 @@
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
-
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(tmpdir)
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(wd)
+ chdir(t, tmpdir)

want := []string{"file1", "file2", "file3", "gopher.txt"}
sort.Strings(want)
@@ -1226,16 +1191,7 @@
if err != nil {
t.Fatal(err)
}
-
- wd, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- err = os.Chdir(tmpdir)
- if err != nil {
- t.Fatal(err)
- }
- defer os.Chdir(wd)
+ chdir(t, tmpdir)

vol := filepath.VolumeName(tmpdir)
output, err := osexec.Command("cmd", "/c", "mountvol", vol, "/L").CombinedOutput()
diff --git a/src/os/removeall_test.go b/src/os/removeall_test.go
index 3a2f6e3..45a8579 100644
--- a/src/os/removeall_test.go
+++ b/src/os/removeall_test.go
@@ -156,6 +156,25 @@
}
}

+// 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("chdir: %v", 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("chdir to original working directory %s: %v", olddir, err)
+ os.Exit(1)
+ }
+ })
+}
+
func TestRemoveAllLongPath(t *testing.T) {
switch runtime.GOOS {
case "aix", "darwin", "ios", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "illumos", "solaris":
@@ -164,21 +183,12 @@
t.Skip("skipping for not implemented platforms")
}

- prevDir, err := Getwd()
- if err != nil {
- t.Fatalf("Could not get wd: %s", err)
- }
-
startPath, err := os.MkdirTemp("", "TestRemoveAllLongPath-")
if err != nil {
t.Fatalf("Could not create TempDir: %s", err)
}
defer RemoveAll(startPath)
-
- err = Chdir(startPath)
- if err != nil {
- t.Fatalf("Could not chdir %s: %s", startPath, err)
- }
+ chdir(t, startPath)

// Removing paths with over 4096 chars commonly fails
for i := 0; i < 41; i++ {
@@ -195,11 +205,6 @@
}
}

- err = Chdir(prevDir)
- if err != nil {
- t.Fatalf("Could not chdir %s: %s", prevDir, err)
- }
-
err = RemoveAll(startPath)
if err != nil {
t.Errorf("RemoveAll could not remove long file path %s: %s", startPath, err)
diff --git a/src/path/filepath/path_test.go b/src/path/filepath/path_test.go
index 1d9889d..51eca49 100644
--- a/src/path/filepath/path_test.go
+++ b/src/path/filepath/path_test.go
@@ -410,6 +410,25 @@
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 @@
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 {

5 is the latest approved patch-set. The change was submitted with unreviewed changes in the following files: The name of the file: src/os/removeall_test.go Insertions: 1, Deletions: 1. ``` @@ -159:160, +159:160 @@ - // and then returns to the current directory at the end of the test. + // and then restore the original working directory at the end of the test. ``` The name of the file: src/path/filepath/path_test.go Insertions: 1, Deletions: 1. ``` @@ -413:414, +413:414 @@ - // and then returns to the current directory at the end of the test. + // and then restore the original working directory at the end of the test. ```

To view, visit change 306290. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Iaf3bdcc345c72fa9669fdc99908ada4e89904edd
Gerrit-Change-Number: 306290
Gerrit-PatchSet: 7
Gerrit-Owner: ZiZhao Zhang <btw51...@gmail.com>
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Rob Pike <r...@golang.org>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-CC: Manlio Perillo <manlio....@gmail.com>
Gerrit-MessageType: merged
Reply all
Reply to author
Forward
0 new messages