From 6c9b655ac1ab99eb2d3f944deb09b0339717f8cc Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 31 Oct 2018 15:06:59 -0700 Subject: [PATCH] os: don't create files in local directory Also, use a random temporary directory rather than os.TempDir. Defer removal of existing random temporary directories. Change-Id: Id7549031cdf78a2bab28c07b6eeff621bdf6e49c Reviewed-on: https://go-review.googlesource.com/c/146457 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Tobias Klauser --- src/os/removeall_test.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/os/removeall_test.go b/src/os/removeall_test.go index 4b6f3e92565..93a6733d6ab 100644 --- a/src/os/removeall_test.go +++ b/src/os/removeall_test.go @@ -8,18 +8,23 @@ import ( "fmt" "io/ioutil" . "os" + "path/filepath" "runtime" "strings" "testing" ) func TestRemoveAll(t *testing.T) { - tmpDir := TempDir() - // Work directory. - file := "file" - path := tmpDir + "/_TestRemoveAll_" - fpath := path + "/file" - dpath := path + "/dir" + tmpDir, err := ioutil.TempDir("", "TestRemoveAll-") + if err != nil { + t.Fatal(err) + } + defer RemoveAll(tmpDir) + + file := filepath.Join(tmpDir, "file") + path := filepath.Join(tmpDir, "_TestRemoveAll_") + fpath := filepath.Join(path, "file") + dpath := filepath.Join(path, "dir") // Make a regular file and remove fd, err := Create(file) @@ -127,9 +132,13 @@ func TestRemoveAllLarge(t *testing.T) { t.Skip("skipping in short mode") } - tmpDir := TempDir() - // Work directory. - path := tmpDir + "/_TestRemoveAllLarge_" + tmpDir, err := ioutil.TempDir("", "TestRemoveAll-") + if err != nil { + t.Fatal(err) + } + defer RemoveAll(tmpDir) + + path := filepath.Join(tmpDir, "_TestRemoveAllLarge_") // Make directory with 1000 files and remove. if err := MkdirAll(path, 0777); err != nil { @@ -168,6 +177,8 @@ func TestRemoveAllLongPath(t *testing.T) { if err != nil { t.Fatalf("Could not create TempDir: %s", err) } + defer RemoveAll(startPath) + err = Chdir(startPath) if err != nil { t.Fatalf("Could not chdir %s: %s", startPath, err) @@ -215,6 +226,8 @@ func TestRemoveAllDot(t *testing.T) { if err != nil { t.Fatalf("Could not create TempDir: %s", err) } + defer RemoveAll(tempDir) + err = Chdir(tempDir) if err != nil { t.Fatalf("Could not chdir to tempdir: %s", err)