]> Cypherpunks.ru repositories - gostls13.git/commitdiff
os, path/filepath: use T.Cleanup to restore the original working directory
authorianwoolf <btw515wolf2@gmail.com>
Wed, 31 Mar 2021 17:27:29 +0000 (01:27 +0800)
committerEmmanuel Odeke <emmanuel@orijtech.com>
Sun, 4 Apr 2021 00:01:03 +0000 (00:01 +0000)
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>
src/os/os_windows_test.go
src/os/removeall_test.go
src/path/filepath/path_test.go

index b0929b4f3085a5532e7b0ce5dc75ac9953804110..b4339c3f7eb7c5d08dc992569b8f594c58ed3e4b 100644 (file)
@@ -35,16 +35,7 @@ func TestSameWindowsFile(t *testing.T) {
                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 @@ func testDirLinks(t *testing.T, tests []dirLinkTest) {
                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 @@ func TestNetworkSymbolicLink(t *testing.T) {
        }
        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 @@ func TestOpenVolumeName(t *testing.T) {
                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 @@ func TestWindowsReadlink(t *testing.T) {
        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()
index 3a2f6e37591632db2711afff5a4e571d9f03002c..45a85792cef98d61df0a4b4585b65b2b3b597533 100644 (file)
@@ -156,6 +156,25 @@ func TestRemoveAllLarge(t *testing.T) {
        }
 }
 
+// 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 @@ func TestRemoveAllLongPath(t *testing.T) {
                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 @@ func TestRemoveAllLongPath(t *testing.T) {
                }
        }
 
-       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)
index 1d9889d320cd3be2c725cbf28d1936358efb1daf..51eca49e4c3f55da314acb938abbf8f9d1ad3dde 100644 (file)
@@ -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 {