Commit fd975c6a authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

io/ioutil: return better error when TempDir called with non-extant dir

Fixes #14196

Change-Id: Ife7950289ac6adbcfc4d0f2fce31f20bc2657858
Reviewed-on: https://go-review.googlesource.com/28772Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 3a59b562
......@@ -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
}
......
......@@ -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)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment