]> Cypherpunks repositories - gostls13.git/commitdiff
io/ioutil: make TestTempFile more robust
authorMostyn Bramley-Moore <mostyn@antipode.se>
Mon, 17 Apr 2017 19:01:10 +0000 (21:01 +0200)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 17 Apr 2017 19:45:37 +0000 (19:45 +0000)
The first part of this test tries to confirm that we can't create
a TempFile in a non-existent directory, but does not ensure that
the non-existent directory really does not exist.  Instead, let's
create an empty temp directory, and use a non-existent subdir of
that.

Change-Id: I176f14ed5f5a2d7a8c29d8f6949755db69d7dbb6
Reviewed-on: https://go-review.googlesource.com/40914
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/io/ioutil/tempfile_test.go

index 6a70aedc3241d0e7f2810434c6d069e01f9a1fd8..9d54bad2ffa32a26bf4bd9ded0fa57751b775f7b 100644 (file)
@@ -12,12 +12,19 @@ import (
 )
 
 func TestTempFile(t *testing.T) {
-       f, err := TempFile("/_not_exists_", "foo")
+       dir, err := TempDir("", "TestTempFile_BadDir")
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer os.RemoveAll(dir)
+
+       nonexistentDir := filepath.Join(dir, "_not_exists_")
+       f, err := TempFile(nonexistentDir, "foo")
        if f != nil || err == nil {
-               t.Errorf("TempFile(`/_not_exists_`, `foo`) = %v, %v", f, err)
+               t.Errorf("TempFile(%q, `foo`) = %v, %v", nonexistentDir, f, err)
        }
 
-       dir := os.TempDir()
+       dir = os.TempDir()
        f, err = TempFile(dir, "ioutil_test")
        if f == nil || err != nil {
                t.Errorf("TempFile(dir, `ioutil_test`) = %v, %v", f, err)