1
0
mirror of https://github.com/golang/go synced 2024-11-26 22:11:25 -07:00

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>
This commit is contained in:
Brad Fitzpatrick 2016-09-08 04:51:21 +00:00
parent 3a59b5626d
commit fd975c6aa5
2 changed files with 21 additions and 0 deletions

View 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
}

View 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)
}
}