diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2016-09-08 04:51:21 +0000 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-09-08 15:08:35 +0000 |
| commit | fd975c6aa535f2aa066653235be992731d691cfb (patch) | |
| tree | c66c6f21a5786ff4ae166831561d05c55f91d0db /src | |
| parent | 3a59b5626da498de0e74a5c02298f04a330f2911 (diff) | |
| download | go-fd975c6aa535f2aa066653235be992731d691cfb.tar.xz | |
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 <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/io/ioutil/tempfile.go | 5 | ||||
| -rw-r--r-- | src/io/ioutil/tempfile_test.go | 16 |
2 files changed, 21 insertions, 0 deletions
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) + } +} |
