1
0
mirror of https://github.com/golang/go synced 2024-11-19 04:34:39 -07:00

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 <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
Ian Lance Taylor 2018-10-31 15:06:59 -07:00 committed by Tobias Klauser
parent 01f0dbbafc
commit 6c9b655ac1

View File

@ -8,18 +8,23 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
. "os" . "os"
"path/filepath"
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
) )
func TestRemoveAll(t *testing.T) { func TestRemoveAll(t *testing.T) {
tmpDir := TempDir() tmpDir, err := ioutil.TempDir("", "TestRemoveAll-")
// Work directory. if err != nil {
file := "file" t.Fatal(err)
path := tmpDir + "/_TestRemoveAll_" }
fpath := path + "/file" defer RemoveAll(tmpDir)
dpath := path + "/dir"
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 // Make a regular file and remove
fd, err := Create(file) fd, err := Create(file)
@ -127,9 +132,13 @@ func TestRemoveAllLarge(t *testing.T) {
t.Skip("skipping in short mode") t.Skip("skipping in short mode")
} }
tmpDir := TempDir() tmpDir, err := ioutil.TempDir("", "TestRemoveAll-")
// Work directory. if err != nil {
path := tmpDir + "/_TestRemoveAllLarge_" t.Fatal(err)
}
defer RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "_TestRemoveAllLarge_")
// Make directory with 1000 files and remove. // Make directory with 1000 files and remove.
if err := MkdirAll(path, 0777); err != nil { if err := MkdirAll(path, 0777); err != nil {
@ -168,6 +177,8 @@ func TestRemoveAllLongPath(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Could not create TempDir: %s", err) t.Fatalf("Could not create TempDir: %s", err)
} }
defer RemoveAll(startPath)
err = Chdir(startPath) err = Chdir(startPath)
if err != nil { if err != nil {
t.Fatalf("Could not chdir %s: %s", startPath, err) t.Fatalf("Could not chdir %s: %s", startPath, err)
@ -215,6 +226,8 @@ func TestRemoveAllDot(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Could not create TempDir: %s", err) t.Fatalf("Could not create TempDir: %s", err)
} }
defer RemoveAll(tempDir)
err = Chdir(tempDir) err = Chdir(tempDir)
if err != nil { if err != nil {
t.Fatalf("Could not chdir to tempdir: %s", err) t.Fatalf("Could not chdir to tempdir: %s", err)