From: Brad Fitzpatrick Date: Thu, 8 Sep 2016 04:51:21 +0000 (+0000) Subject: io/ioutil: return better error when TempDir called with non-extant dir X-Git-Tag: go1.8beta1~1452 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=fd975c6aa535f2aa066653235be992731d691cfb;p=gostls13.git io/ioutil: return better error when TempDir called with non-extant dir Fixes #14196 Change-Id: Ife7950289ac6adbcfc4d0f2fce31f20bc2657858 Reviewed-on: https://go-review.googlesource.com/28772 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- diff --git a/src/io/ioutil/tempfile.go b/src/io/ioutil/tempfile.go index 42718cc73d..e5e315cfb7 100644 --- a/src/io/ioutil/tempfile.go +++ b/src/io/ioutil/tempfile.go @@ -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 } diff --git a/src/io/ioutil/tempfile_test.go b/src/io/ioutil/tempfile_test.go index d2a132a110..6a70aedc32 100644 --- a/src/io/ioutil/tempfile_test.go +++ b/src/io/ioutil/tempfile_test.go @@ -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) + } +}