Skip to content

Commit

Permalink
all: use t.TempDir in tests
Browse files Browse the repository at this point in the history
This removes all usage of ioutil.TempDir in tests (and a few cases of
os.TempDir as well, while we're at it), which simplifies test cleanup a
lot.

Compile tested (go test -c) for most platforms (except zos).

Change-Id: I9178f5ec615c0a6cfef36e8de78c6b72e055b7cb
Reviewed-on: https://go-review.googlesource.com/c/sys/+/526297
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
kolyshkin authored and gopherbot committed Sep 8, 2023
1 parent 0514fec commit 0e97d69
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 178 deletions.
16 changes: 4 additions & 12 deletions execabs/execabs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,12 @@ func TestCommand(t *testing.T) {
func(s string) *Cmd { return Command(s) },
func(s string) *Cmd { return CommandContext(context.Background(), s) },
} {
tmpDir, err := ioutil.TempDir("", "execabs-test")
if err != nil {
t.Fatalf("ioutil.TempDir failed: %s", err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()
executable := "execabs-test"
if runtime.GOOS == "windows" {
executable += ".exe"
}
if err = ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
if err := ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
t.Fatalf("ioutil.WriteFile failed: %s", err)
}
cwd, err := os.Getwd()
Expand Down Expand Up @@ -97,16 +93,12 @@ func TestCommand(t *testing.T) {
func TestLookPath(t *testing.T) {
mustHaveExec(t)

tmpDir, err := ioutil.TempDir("", "execabs-test")
if err != nil {
t.Fatalf("ioutil.TempDir failed: %s", err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()
executable := "execabs-test"
if runtime.GOOS == "windows" {
executable += ".exe"
}
if err = ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
if err := ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
t.Fatalf("ioutil.WriteFile failed: %s", err)
}
cwd, err := os.Getwd()
Expand Down
17 changes: 4 additions & 13 deletions unix/dirent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
Expand All @@ -29,16 +28,12 @@ func TestDirent(t *testing.T) {
filenameMinSize = 11
)

d, err := ioutil.TempDir("", "dirent-test")
if err != nil {
t.Fatalf("tempdir: %v", err)
}
defer os.RemoveAll(d)
d := t.TempDir()
t.Logf("tmpdir: %s", d)

for i, c := range []byte("0123456789") {
name := string(bytes.Repeat([]byte{c}, filenameMinSize+i))
err = ioutil.WriteFile(filepath.Join(d, name), nil, 0644)
err := ioutil.WriteFile(filepath.Join(d, name), nil, 0644)
if err != nil {
t.Fatalf("writefile: %v", err)
}
Expand Down Expand Up @@ -98,18 +93,14 @@ func TestDirentRepeat(t *testing.T) {
}

// Make a directory containing N files
d, err := ioutil.TempDir("", "direntRepeat-test")
if err != nil {
t.Fatalf("tempdir: %v", err)
}
defer os.RemoveAll(d)
d := t.TempDir()

var files []string
for i := 0; i < N; i++ {
files = append(files, fmt.Sprintf("file%d", i))
}
for _, file := range files {
err = ioutil.WriteFile(filepath.Join(d, file), []byte("contents"), 0644)
err := ioutil.WriteFile(filepath.Join(d, file), []byte("contents"), 0644)
if err != nil {
t.Fatalf("writefile: %v", err)
}
Expand Down
7 changes: 2 additions & 5 deletions unix/getdirentries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ func testGetdirentries(t *testing.T, count int) {
if count > 100 && testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
t.Skip("skipping in -short mode")
}
d, err := ioutil.TempDir("", "getdirentries-test")
if err != nil {
t.Fatalf("Tempdir: %v", err)
}
defer os.RemoveAll(d)
d := t.TempDir()

var names []string
for i := 0; i < count; i++ {
names = append(names, fmt.Sprintf("file%03d", i))
Expand Down
20 changes: 5 additions & 15 deletions unix/mmap_zos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ import (
)

func TestMmap(t *testing.T) {
tmpdir := mktmpdir(t)
filename := filepath.Join(filepath.Join(tmpdir, "testdata"), "memmapped_file")
tmpdir := filepath.Join(t.TempDir(), "testdata")
if err := os.Mkdir(tmpdir, 0700); err != nil {
t.Fatal(err)
}
filename := filepath.Join(tmpdir, "memmapped_file")
destination, err := os.Create(filename)
if err != nil {
t.Fatal("os.Create:", err)
return
}
defer os.RemoveAll(tmpdir)

fmt.Fprintf(destination, "%s\n", "0 <- Flipped between 0 and 1 when test runs successfully")
fmt.Fprintf(destination, "%s\n", "//Do not change contents - mmap test relies on this")
Expand Down Expand Up @@ -73,15 +75,3 @@ func TestMmap(t *testing.T) {
t.Fatalf("Munmap: %v", err)
}
}

func mktmpdir(t *testing.T) string {
tmpdir, err := ioutil.TempDir("", "memmapped_file")
if err != nil {
t.Fatal("mktmpdir:", err)
}
if err := os.Mkdir(filepath.Join(tmpdir, "testdata"), 0700); err != nil {
os.RemoveAll(tmpdir)
t.Fatal("mktmpdir:", err)
}
return tmpdir
}
9 changes: 2 additions & 7 deletions unix/sendfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ import (

func TestSendfile(t *testing.T) {
// Set up source data file.
tempDir, err := ioutil.TempDir("", "TestSendfile")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
name := filepath.Join(tempDir, "source")
name := filepath.Join(t.TempDir(), "source")
const contents = "contents"
err = ioutil.WriteFile(name, []byte(contents), 0666)
err := ioutil.WriteFile(name, []byte(contents), 0666)
if err != nil {
t.Fatal(err)
}
Expand Down
13 changes: 2 additions & 11 deletions unix/syscall_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,8 @@ func TestClonefileatWithCwd(t *testing.T) {
}

func TestClonefileatWithRelativePaths(t *testing.T) {
srcDir, err := ioutil.TempDir("", "src")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(srcDir)

dstDir, err := ioutil.TempDir("", "dest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dstDir)
srcDir := t.TempDir()
dstDir := t.TempDir()

srcFd, err := unix.Open(srcDir, unix.O_RDONLY|unix.O_DIRECTORY, 0)
if err != nil {
Expand Down
38 changes: 3 additions & 35 deletions unix/syscall_freebsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package unix_test
import (
"flag"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
Expand All @@ -34,10 +33,8 @@ func TestSysctlUint64(t *testing.T) {
// corresponding to the given key.

type testProc struct {
fn func() // should always exit instead of returning
arg func(t *testing.T) string // generate argument for test
cleanup func(arg string) error // for instance, delete coredumps from testing pledge
success bool // whether zero-exit means success or failure
fn func() // should always exit instead of returning
success bool // whether zero-exit means success or failure
}

var (
Expand Down Expand Up @@ -71,16 +68,7 @@ func testCmd(procName string, procArg string) (*exec.Cmd, error) {
// a testProc with a key.
func ExitsCorrectly(t *testing.T, procName string) {
s := testProcs[procName]
arg := "-"
if s.arg != nil {
arg = s.arg(t)
}
c, err := testCmd(procName, arg)
defer func(arg string) {
if err := s.cleanup(arg); err != nil {
t.Fatalf("Failed to run cleanup for %s %s %#v", procName, err, err)
}
}(arg)
c, err := testCmd(procName, t.TempDir())
if err != nil {
t.Fatalf("Failed to construct command for %s", procName)
}
Expand Down Expand Up @@ -134,27 +122,9 @@ func CapEnterTest() {
os.Exit(0)
}

func makeTempDir(t *testing.T) string {
d, err := ioutil.TempDir("", "go_openat_test")
if err != nil {
t.Fatalf("TempDir failed: %s", err)
}
return d
}

func removeTempDir(arg string) error {
err := os.RemoveAll(arg)
if err != nil && err.(*os.PathError).Err == unix.ENOENT {
return nil
}
return err
}

func init() {
testProcs["cap_enter"] = testProc{
CapEnterTest,
makeTempDir,
removeTempDir,
true,
}
}
Expand Down Expand Up @@ -252,8 +222,6 @@ func OpenatTest() {
func init() {
testProcs["openat"] = testProc{
OpenatTest,
makeTempDir,
removeTempDir,
true,
}
}
Expand Down
8 changes: 1 addition & 7 deletions unix/syscall_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,13 +986,7 @@ func TestOpenat2(t *testing.T) {
}

// prepare
tempDir, err := ioutil.TempDir("", t.Name())
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)

subdir := filepath.Join(tempDir, "dir")
subdir := filepath.Join(t.TempDir(), "dir")
if err := os.Mkdir(subdir, 0755); err != nil {
t.Fatal(err)
}
Expand Down
28 changes: 4 additions & 24 deletions unix/syscall_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,11 @@ func TestFcntlInt(t *testing.T) {
// TestFcntlFlock tests whether the file locking structure matches
// the calling convention of each kernel.
func TestFcntlFlock(t *testing.T) {
name := filepath.Join(os.TempDir(), "TestFcntlFlock")
name := filepath.Join(t.TempDir(), "TestFcntlFlock")
fd, err := unix.Open(name, unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC, 0)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
defer unix.Unlink(name)
defer unix.Close(fd)
flock := unix.Flock_t{
Type: unix.F_RDLCK,
Expand Down Expand Up @@ -199,12 +198,6 @@ func TestPassFD(t *testing.T) {
}
}

tempDir, err := ioutil.TempDir("", "TestPassFD")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)

fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM, 0)
if err != nil {
t.Fatalf("Socketpair: %v", err)
Expand All @@ -214,7 +207,7 @@ func TestPassFD(t *testing.T) {
defer writeFile.Close()
defer readFile.Close()

cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", tempDir)
cmd := exec.Command(os.Args[0], "-test.run=^TestPassFD$", "--", t.TempDir())
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
if lp := os.Getenv("LD_LIBRARY_PATH"); lp != "" {
cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+lp)
Expand Down Expand Up @@ -659,15 +652,7 @@ func TestGetwd(t *testing.T) {
case "android":
dirs = []string{"/", "/system/bin"}
case "ios":
d1, err := ioutil.TempDir("", "d1")
if err != nil {
t.Fatalf("TempDir: %v", err)
}
d2, err := ioutil.TempDir("", "d2")
if err != nil {
t.Fatalf("TempDir: %v", err)
}
dirs = []string{d1, d2}
dirs = []string{t.TempDir(), t.TempDir()}
}
oldwd := os.Getenv("PWD")
for _, d := range dirs {
Expand Down Expand Up @@ -1166,17 +1151,12 @@ func chtmpdir(t *testing.T) func() {
if err != nil {
t.Fatalf("chtmpdir: %v", err)
}
d, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatalf("chtmpdir: %v", err)
}
if err := os.Chdir(d); err != nil {
if err := os.Chdir(t.TempDir()); err != nil {
t.Fatalf("chtmpdir: %v", err)
}
return func() {
if err := os.Chdir(oldwd); err != nil {
t.Fatalf("chtmpdir: %v", err)
}
os.RemoveAll(d)
}
}
Loading

0 comments on commit 0e97d69

Please sign in to comment.