]> Cypherpunks repositories - gostls13.git/commitdiff
io/ioutil: return better error when TempDir called with non-extant dir
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 8 Sep 2016 04:51:21 +0000 (04:51 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 8 Sep 2016 15:08:35 +0000 (15:08 +0000)
Fixes #14196

Change-Id: Ife7950289ac6adbcfc4d0f2fce31f20bc2657858
Reviewed-on: https://go-review.googlesource.com/28772
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/io/ioutil/tempfile.go
src/io/ioutil/tempfile_test.go

index 42718cc73d37ecc3049bff448f8709b144fe1d72..e5e315cfb7d48b6fcf07730b20b916abd8f5ba84 100644 (file)
@@ -90,6 +90,11 @@ func TempDir(dir, prefix string) (name string, err error) {
                        }
                        continue
                }
+               if os.IsNotExist(err) {
+                       if _, err := os.Stat(dir); os.IsNotExist(err) {
+                               return "", err
+                       }
+               }
                if err == nil {
                        name = try
                }
index d2a132a110a654dd289f3d56594cc47ae1aee1e9..6a70aedc3241d0e7f2810434c6d069e01f9a1fd8 100644 (file)
@@ -51,3 +51,19 @@ func TestTempDir(t *testing.T) {
                }
        }
 }
+
+// test that we return a nice error message if the dir argument to TempDir doesn't
+// exist (or that it's empty and os.TempDir doesn't exist)
+func TestTempDir_BadDir(t *testing.T) {
+       dir, err := TempDir("", "TestTempDir_BadDir")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(dir)
+
+       badDir := filepath.Join(dir, "not-exist")
+       _, err = TempDir(badDir, "foo")
+       if pe, ok := err.(*os.PathError); !ok || !os.IsNotExist(err) || pe.Path != badDir {
+               t.Errorf("TempDir error = %#v; want PathError for path %q satisifying os.IsNotExist", err, badDir)
+       }
+}