]> Cypherpunks repositories - gostls13.git/commitdiff
os: restore testErrNotExist's working directory on os.Chdir success
authorGrace Han <hgrace503@gmail.com>
Fri, 9 Apr 2021 05:49:22 +0000 (15:49 +1000)
committerIan Lance Taylor <iant@golang.org>
Mon, 12 Apr 2021 22:19:11 +0000 (22:19 +0000)
The existing implementation calls os.Chdir expecting the call not to
succeed. This change restores the original working directory in the
case that the call does succeed.

Fixes #45407

Change-Id: I61c57f6858b9a9058226e45e24276c7af8913048
Reviewed-on: https://go-review.googlesource.com/c/go/+/308849
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/error_test.go

index 58b3f391d1dd53635de372945141a98cda95c255..4ab6246d2ecafc466222f68d61e3eef56a188d9a 100644 (file)
@@ -33,7 +33,12 @@ func TestErrIsExist(t *testing.T) {
        }
 }
 
-func testErrNotExist(name string) string {
+func testErrNotExist(t *testing.T, name string) string {
+       originalWD, err := os.Getwd()
+       if err != nil {
+               t.Fatal(err)
+       }
+
        f, err := os.Open(name)
        if err == nil {
                f.Close()
@@ -45,7 +50,10 @@ func testErrNotExist(name string) string {
 
        err = os.Chdir(name)
        if err == nil {
-               return "Chdir should have failed"
+               if err := os.Chdir(originalWD); err != nil {
+                       t.Fatalf("Chdir should have failed, failed to restore original working directory: %v", err)
+               }
+               return "Chdir should have failed, restored original working directory"
        }
        if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err, fs.ErrNotExist); s != "" {
                return s
@@ -56,13 +64,13 @@ func testErrNotExist(name string) string {
 func TestErrIsNotExist(t *testing.T) {
        tmpDir := t.TempDir()
        name := filepath.Join(tmpDir, "NotExists")
-       if s := testErrNotExist(name); s != "" {
+       if s := testErrNotExist(t, name); s != "" {
                t.Fatal(s)
                return
        }
 
        name = filepath.Join(name, "NotExists2")
-       if s := testErrNotExist(name); s != "" {
+       if s := testErrNotExist(t, name); s != "" {
                t.Fatal(s)
                return
        }