Skip to content

Commit fc717d3

Browse files
kolyshkingopherbot
authored andcommitted
unix: remove usage of ioutil.TempFile in tests
Mostly change that to os.Create(filepath.Join(t.TempDir(), "filename")) so that the cleanup is done automatically (and, because this is a new directory, we do not need to have a "create unique temp file name" logic). While at it, fix some log/error messages -- if an error is coming from e.g. os package, it is already wrapped, so there's no need to add more context. Change-Id: I62f13679f256b94095be5ca1e77a3fa302f01b97 Reviewed-on: https://go-review.googlesource.com/c/sys/+/526298 Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Run-TryBot: Kirill Kolyshkin <[email protected]>
1 parent cb4ecd9 commit fc717d3

7 files changed

+47
-73
lines changed

unix/dup3_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ package unix_test
99

1010
import (
1111
"os"
12+
"path/filepath"
1213
"runtime"
1314
"testing"
1415

1516
"golang.org/x/sys/unix"
1617
)
1718

1819
func TestDup3(t *testing.T) {
19-
tempFile, err := os.CreateTemp("", "TestDup")
20+
tempFile, err := os.Create(filepath.Join(t.TempDir(), "TestDup"))
2021
if err != nil {
21-
t.Fatalf("CreateTemp failed: %v", err)
22+
t.Fatal(err)
2223
}
23-
defer os.Remove(tempFile.Name())
2424
defer tempFile.Close()
2525
oldFd := int(tempFile.Fd())
2626

unix/syscall_darwin_test.go

+20-40
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,25 @@ func stringsFromByteSlice(buf []byte) []string {
3131
return result
3232
}
3333

34-
func createTestFile(t *testing.T, dir string) (f *os.File, cleanup func() error) {
35-
file, err := ioutil.TempFile(dir, t.Name())
34+
func createTestFile(t *testing.T) string {
35+
filename := filepath.Join(t.TempDir(), t.Name())
36+
err := os.WriteFile(filename, testData, 0600)
3637
if err != nil {
3738
t.Fatal(err)
3839
}
39-
40-
_, err = file.Write(testData)
41-
if err != nil {
42-
t.Fatal(err)
43-
}
44-
45-
err = file.Close()
46-
if err != nil {
47-
t.Fatal(err)
48-
}
49-
50-
return file, func() error {
51-
return os.Remove(file.Name())
52-
}
40+
return filename
5341
}
5442

5543
func TestClonefile(t *testing.T) {
56-
file, cleanup := createTestFile(t, "")
57-
defer cleanup()
44+
fileName := createTestFile(t)
5845

59-
clonedName := file.Name() + "-cloned"
60-
err := unix.Clonefile(file.Name(), clonedName, 0)
46+
clonedName := fileName + "-cloned"
47+
err := unix.Clonefile(fileName, clonedName, 0)
6148
if err == unix.ENOSYS || err == unix.ENOTSUP {
6249
t.Skip("clonefile is not available or supported, skipping test")
6350
} else if err != nil {
6451
t.Fatal(err)
6552
}
66-
defer os.Remove(clonedName)
6753

6854
clonedData, err := ioutil.ReadFile(clonedName)
6955
if err != nil {
@@ -76,17 +62,15 @@ func TestClonefile(t *testing.T) {
7662
}
7763

7864
func TestClonefileatWithCwd(t *testing.T) {
79-
file, cleanup := createTestFile(t, "")
80-
defer cleanup()
65+
fileName := createTestFile(t)
8166

82-
clonedName := file.Name() + "-cloned"
83-
err := unix.Clonefileat(unix.AT_FDCWD, file.Name(), unix.AT_FDCWD, clonedName, 0)
67+
clonedName := fileName + "-cloned"
68+
err := unix.Clonefileat(unix.AT_FDCWD, fileName, unix.AT_FDCWD, clonedName, 0)
8469
if err == unix.ENOSYS || err == unix.ENOTSUP {
8570
t.Skip("clonefileat is not available or supported, skipping test")
8671
} else if err != nil {
8772
t.Fatal(err)
8873
}
89-
defer os.Remove(clonedName)
9074

9175
clonedData, err := ioutil.ReadFile(clonedName)
9276
if err != nil {
@@ -99,25 +83,22 @@ func TestClonefileatWithCwd(t *testing.T) {
9983
}
10084

10185
func TestClonefileatWithRelativePaths(t *testing.T) {
102-
srcDir := t.TempDir()
103-
dstDir := t.TempDir()
104-
86+
srcFileName := createTestFile(t)
87+
srcDir := filepath.Dir(srcFileName)
10588
srcFd, err := unix.Open(srcDir, unix.O_RDONLY|unix.O_DIRECTORY, 0)
10689
if err != nil {
10790
t.Fatal(err)
10891
}
10992
defer unix.Close(srcFd)
11093

94+
dstDir := t.TempDir()
11195
dstFd, err := unix.Open(dstDir, unix.O_RDONLY|unix.O_DIRECTORY, 0)
11296
if err != nil {
11397
t.Fatal(err)
11498
}
11599
defer unix.Close(dstFd)
116100

117-
srcFile, cleanup := createTestFile(t, srcDir)
118-
defer cleanup()
119-
120-
dstFile, err := ioutil.TempFile(dstDir, "TestClonefileat")
101+
dstFile, err := os.Create(filepath.Join(dstDir, "TestClonefileat"))
121102
if err != nil {
122103
t.Fatal(err)
123104
}
@@ -126,7 +107,7 @@ func TestClonefileatWithRelativePaths(t *testing.T) {
126107
t.Fatal(err)
127108
}
128109

129-
src := filepath.Base(srcFile.Name())
110+
src := filepath.Base(srcFileName)
130111
dst := filepath.Base(dstFile.Name())
131112
err = unix.Clonefileat(srcFd, src, dstFd, dst, 0)
132113
if err == unix.ENOSYS || err == unix.ENOTSUP {
@@ -146,16 +127,16 @@ func TestClonefileatWithRelativePaths(t *testing.T) {
146127
}
147128

148129
func TestFclonefileat(t *testing.T) {
149-
file, cleanup := createTestFile(t, "")
150-
defer cleanup()
130+
fileName := createTestFile(t)
131+
dir := filepath.Dir(fileName)
151132

152-
fd, err := unix.Open(file.Name(), unix.O_RDONLY, 0)
133+
fd, err := unix.Open(fileName, unix.O_RDONLY, 0)
153134
if err != nil {
154135
t.Fatal(err)
155136
}
156137
defer unix.Close(fd)
157138

158-
dstFile, err := ioutil.TempFile("", "TestFclonefileat")
139+
dstFile, err := os.Create(filepath.Join(dir, "dst"))
159140
if err != nil {
160141
t.Fatal(err)
161142
}
@@ -179,11 +160,10 @@ func TestFclonefileat(t *testing.T) {
179160
}
180161

181162
func TestFcntlFstore(t *testing.T) {
182-
f, err := ioutil.TempFile("", t.Name())
163+
f, err := os.CreateTemp(t.TempDir(), t.Name())
183164
if err != nil {
184165
t.Fatal(err)
185166
}
186-
defer os.Remove(f.Name())
187167
defer f.Close()
188168

189169
fstore := &unix.Fstore_t{

unix/syscall_linux_test.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,10 @@ func TestFaccessat(t *testing.T) {
688688
}
689689

690690
func TestSyncFileRange(t *testing.T) {
691-
file, err := ioutil.TempFile("", "TestSyncFileRange")
691+
file, err := os.Create(filepath.Join(t.TempDir(), t.Name()))
692692
if err != nil {
693693
t.Fatal(err)
694694
}
695-
defer os.Remove(file.Name())
696695
defer file.Close()
697696

698697
err = unix.SyncFileRange(int(file.Fd()), 0, 0, 0)
@@ -1024,12 +1023,12 @@ func TestOpenat2(t *testing.T) {
10241023
}
10251024

10261025
func TestIoctlFileDedupeRange(t *testing.T) {
1027-
f1, err := ioutil.TempFile("", t.Name())
1026+
dir := t.TempDir()
1027+
f1, err := os.Create(filepath.Join(dir, "f1"))
10281028
if err != nil {
10291029
t.Fatal(err)
10301030
}
10311031
defer f1.Close()
1032-
defer os.Remove(f1.Name())
10331032

10341033
// Test deduplication with two blocks of zeros
10351034
data := make([]byte, 4096)
@@ -1041,12 +1040,11 @@ func TestIoctlFileDedupeRange(t *testing.T) {
10411040
}
10421041
}
10431042

1044-
f2, err := ioutil.TempFile("", t.Name())
1043+
f2, err := os.Create(filepath.Join(dir, "f2"))
10451044
if err != nil {
10461045
t.Fatal(err)
10471046
}
10481047
defer f2.Close()
1049-
defer os.Remove(f2.Name())
10501048

10511049
for i := 0; i < 2; i += 1 {
10521050
// Make the 2nd block different

unix/syscall_solaris_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"os"
1313
"os/exec"
14+
"path/filepath"
1415
"runtime"
1516
"strings"
1617
"testing"
@@ -83,12 +84,12 @@ func TestSysconf(t *testing.T) {
8384
// Event Ports
8485

8586
func TestBasicEventPort(t *testing.T) {
86-
tmpfile, err := os.CreateTemp("", "eventport")
87+
tmpfile, err := os.Create(filepath.Join(t.TempDir(), "eventport"))
8788
if err != nil {
88-
t.Fatalf("unable to create a tempfile: %v", err)
89+
t.Fatal(err)
8990
}
91+
defer tmpfile.Close()
9092
path := tmpfile.Name()
91-
defer os.Remove(path)
9293

9394
stat, err := os.Stat(path)
9495
if err != nil {

unix/syscall_unix_test.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,10 @@ func TestSignalNum(t *testing.T) {
126126

127127
func TestFcntlInt(t *testing.T) {
128128
t.Parallel()
129-
file, err := ioutil.TempFile("", "TestFcntlInt")
129+
file, err := os.Create(filepath.Join(t.TempDir(), t.Name()))
130130
if err != nil {
131131
t.Fatal(err)
132132
}
133-
defer os.Remove(file.Name())
134133
defer file.Close()
135134
f := file.Fd()
136135
flags, err := unix.FcntlInt(f, unix.F_GETFD, 0)
@@ -293,9 +292,9 @@ func passFDChild() {
293292
// We make it in tempDir, which our parent will clean up.
294293
flag.Parse()
295294
tempDir := flag.Arg(0)
296-
f, err := ioutil.TempFile(tempDir, "")
295+
f, err := os.Create(filepath.Join(tempDir, "file"))
297296
if err != nil {
298-
fmt.Printf("TempFile: %v", err)
297+
fmt.Println(err)
299298
return
300299
}
301300

@@ -452,11 +451,10 @@ func TestSetsockoptString(t *testing.T) {
452451
}
453452

454453
func TestDup(t *testing.T) {
455-
file, err := ioutil.TempFile("", "TestDup")
454+
file, err := os.Create(filepath.Join(t.TempDir(), t.Name()))
456455
if err != nil {
457-
t.Fatalf("Tempfile failed: %v", err)
456+
t.Fatal(err)
458457
}
459-
defer os.Remove(file.Name())
460458
defer file.Close()
461459
f := int(file.Fd())
462460

unix/syscall_zos_test.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,10 @@ func TestSignalNum(t *testing.T) {
124124

125125
func TestFcntlInt(t *testing.T) {
126126
t.Parallel()
127-
file, err := ioutil.TempFile("", "TestFnctlInt")
127+
file, err := os.Create(filepath.Join(t.TempDir(), "TestFnctlInt"))
128128
if err != nil {
129129
t.Fatal(err)
130130
}
131-
defer os.Remove(file.Name())
132131
defer file.Close()
133132
f := file.Fd()
134133
flags, err := unix.FcntlInt(f, unix.F_GETFD, 0)
@@ -291,9 +290,9 @@ func passFDChild() {
291290
// We make it in tempDir, which our parent will clean up.
292291
flag.Parse()
293292
tempDir := flag.Arg(0)
294-
f, err := ioutil.TempFile(tempDir, "")
293+
f, err := os.Create(filepath.Join(tempDir, "file"))
295294
if err != nil {
296-
fmt.Printf("TempFile: %v", err)
295+
fmt.Println(err)
297296
return
298297
}
299298

@@ -424,11 +423,10 @@ func TestSetsockoptString(t *testing.T) {
424423
}
425424

426425
func TestDup(t *testing.T) {
427-
file, err := ioutil.TempFile("", "TestDup")
426+
file, err := os.Create(filepath.Join(t.TempDir(), "TestDup"))
428427
if err != nil {
429-
t.Fatalf("Tempfile failed: %v", err)
428+
t.Fatal(err)
430429
}
431-
defer os.Remove(file.Name())
432430
defer file.Close()
433431
f := int(file.Fd())
434432

@@ -615,9 +613,9 @@ func TestMountUnmount(t *testing.T) {
615613
func TestChroot(t *testing.T) {
616614
// create temp dir and tempfile 1
617615
tempDir := t.TempDir()
618-
f, err := ioutil.TempFile(tempDir, "chroot_test_file")
616+
f, err := os.Create(filepath.Join(tempDir, "chroot_test_file"))
619617
if err != nil {
620-
t.Fatalf("TempFile: %s", err.Error())
618+
t.Fatal(err)
621619
}
622620
// chroot temp dir
623621
err = unix.Chroot(tempDir)
@@ -667,9 +665,9 @@ func TestFlock(t *testing.T) {
667665
}
668666
} else {
669667
// create temp dir and tempfile 1
670-
f, err := ioutil.TempFile(t.TempDir(), "flock_test_file")
668+
f, err := os.Create(filepath.Join(t.TempDir(), "flock_test_file"))
671669
if err != nil {
672-
t.Fatalf("TempFile: %s", err.Error())
670+
t.Fatal(err)
673671
}
674672
fd := int(f.Fd())
675673

unix/xattr_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
package unix_test
99

1010
import (
11-
"io/ioutil"
1211
"os"
12+
"path/filepath"
1313
"runtime"
1414
"strings"
1515
"testing"
@@ -127,11 +127,10 @@ func TestXattr(t *testing.T) {
127127
}
128128

129129
func TestFdXattr(t *testing.T) {
130-
file, err := ioutil.TempFile("", "TestFdXattr")
130+
file, err := os.Create(filepath.Join(t.TempDir(), "TestFdXattr"))
131131
if err != nil {
132132
t.Fatal(err)
133133
}
134-
defer os.Remove(file.Name())
135134
defer file.Close()
136135

137136
fd := int(file.Fd())

0 commit comments

Comments
 (0)