Skip to content

Commit 8d9dcc4

Browse files
kolyshkingopherbot
authored andcommitted
unix: modernize test helpers
Test helper functions mktmpfifo and chtmpdir were written before t.Cleanup was available, which necessitated returning of a cleanup function and the use of defer. Let's use t.Cleanup (available since Go 1.14) and simplify the callers. While at it, - use t.Helper (added in Go 1.9); - simplify some error messages (errors that come from os package usually doesn't need to be wrapped). Change-Id: Id981ae1c85fa2642a76358a31fc18a9af2f78711 Reviewed-on: https://go-review.googlesource.com/c/sys/+/526695 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent a26c6de commit 8d9dcc4

File tree

6 files changed

+33
-32
lines changed

6 files changed

+33
-32
lines changed

unix/syscall_aix_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestTime(t *testing.T) {
6060
}
6161

6262
func TestUtime(t *testing.T) {
63-
defer chtmpdir(t)()
63+
chtmpdir(t)
6464

6565
touch(t, "file1")
6666

unix/syscall_linux_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,8 @@ func TestPpoll(t *testing.T) {
277277
t.Skip("mkfifo syscall is not available on android, skipping test")
278278
}
279279

280-
defer chtmpdir(t)()
281-
f, cleanup := mktmpfifo(t)
282-
defer cleanup()
280+
chtmpdir(t)
281+
f := mktmpfifo(t)
283282

284283
const timeout = 100 * time.Millisecond
285284

@@ -335,7 +334,7 @@ func TestTime(t *testing.T) {
335334
}
336335

337336
func TestUtime(t *testing.T) {
338-
defer chtmpdir(t)()
337+
chtmpdir(t)
339338

340339
touch(t, "file1")
341340

@@ -548,7 +547,7 @@ func TestStatx(t *testing.T) {
548547
t.Fatalf("Statx: %v", err)
549548
}
550549

551-
defer chtmpdir(t)()
550+
chtmpdir(t)
552551
touch(t, "file1")
553552

554553
var st unix.Stat_t
@@ -636,7 +635,7 @@ func stringsFromByteSlice(buf []byte) []string {
636635
}
637636

638637
func TestFaccessat(t *testing.T) {
639-
defer chtmpdir(t)()
638+
chtmpdir(t)
640639
touch(t, "file1")
641640

642641
err := unix.Faccessat(unix.AT_FDCWD, "file1", unix.R_OK, 0)

unix/syscall_netbsd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestIoctlPtmget(t *testing.T) {
4242
}
4343

4444
func TestStatvfs(t *testing.T) {
45-
defer chtmpdir(t)()
45+
chtmpdir(t)
4646
touch(t, "file1")
4747

4848
var statvfs1, statvfs2 unix.Statvfs_t

unix/syscall_openbsd_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import (
1212
)
1313

1414
func TestPpoll(t *testing.T) {
15-
defer chtmpdir(t)()
16-
f, cleanup := mktmpfifo(t)
17-
defer cleanup()
15+
chtmpdir(t)
16+
f := mktmpfifo(t)
1817

1918
const timeout = 100 * time.Millisecond
2019

unix/syscall_unix_test.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,8 @@ func TestPoll(t *testing.T) {
502502
t.Skip("mkfifo syscall is not available on android and iOS, skipping test")
503503
}
504504

505-
defer chtmpdir(t)()
506-
f, cleanup := mktmpfifo(t)
507-
defer cleanup()
505+
chtmpdir(t)
506+
f := mktmpfifo(t)
508507

509508
const timeout = 100
510509

@@ -710,7 +709,7 @@ func compareStat_t(t *testing.T, otherStat string, st1, st2 *unix.Stat_t) {
710709
}
711710

712711
func TestFstatat(t *testing.T) {
713-
defer chtmpdir(t)()
712+
chtmpdir(t)
714713

715714
touch(t, "file1")
716715

@@ -747,7 +746,7 @@ func TestFstatat(t *testing.T) {
747746
}
748747

749748
func TestFchmodat(t *testing.T) {
750-
defer chtmpdir(t)()
749+
chtmpdir(t)
751750

752751
touch(t, "file1")
753752
err := os.Symlink("file1", "symlink1")
@@ -857,7 +856,7 @@ func TestPipe(t *testing.T) {
857856
}
858857

859858
func TestRenameat(t *testing.T) {
860-
defer chtmpdir(t)()
859+
chtmpdir(t)
861860

862861
from, to := "renamefrom", "renameto"
863862

@@ -880,7 +879,7 @@ func TestRenameat(t *testing.T) {
880879
}
881880

882881
func TestUtimesNanoAt(t *testing.T) {
883-
defer chtmpdir(t)()
882+
chtmpdir(t)
884883

885884
symlink := "symlink1"
886885
os.Remove(symlink)
@@ -1111,8 +1110,9 @@ func TestRecvmsgControl(t *testing.T) {
11111110
defer unix.Close(cfds[0])
11121111
}
11131112

1114-
// mktmpfifo creates a temporary FIFO and provides a cleanup function.
1115-
func mktmpfifo(t *testing.T) (*os.File, func()) {
1113+
// mktmpfifo creates a temporary FIFO and sets up a cleanup function.
1114+
func mktmpfifo(t *testing.T) *os.File {
1115+
t.Helper()
11161116
err := unix.Mkfifo("fifo", 0666)
11171117
if err != nil {
11181118
t.Fatalf("mktmpfifo: failed to create FIFO: %v", err)
@@ -1121,18 +1121,20 @@ func mktmpfifo(t *testing.T) (*os.File, func()) {
11211121
f, err := os.OpenFile("fifo", os.O_RDWR, 0666)
11221122
if err != nil {
11231123
os.Remove("fifo")
1124-
t.Fatalf("mktmpfifo: failed to open FIFO: %v", err)
1124+
t.Fatal(err)
11251125
}
1126-
1127-
return f, func() {
1126+
t.Cleanup(func() {
11281127
f.Close()
11291128
os.Remove("fifo")
1130-
}
1129+
})
1130+
1131+
return f
11311132
}
11321133

11331134
// utilities taken from os/os_test.go
11341135

11351136
func touch(t *testing.T, name string) {
1137+
t.Helper()
11361138
f, err := os.Create(name)
11371139
if err != nil {
11381140
t.Fatal(err)
@@ -1143,18 +1145,19 @@ func touch(t *testing.T, name string) {
11431145
}
11441146

11451147
// chtmpdir changes the working directory to a new temporary directory and
1146-
// provides a cleanup function. Used when PWD is read-only.
1147-
func chtmpdir(t *testing.T) func() {
1148+
// sets up a cleanup function. Used when PWD is read-only.
1149+
func chtmpdir(t *testing.T) {
1150+
t.Helper()
11481151
oldwd, err := os.Getwd()
11491152
if err != nil {
1150-
t.Fatalf("chtmpdir: %v", err)
1153+
t.Fatal(err)
11511154
}
11521155
if err := os.Chdir(t.TempDir()); err != nil {
1153-
t.Fatalf("chtmpdir: %v", err)
1156+
t.Fatal(err)
11541157
}
1155-
return func() {
1158+
t.Cleanup(func() {
11561159
if err := os.Chdir(oldwd); err != nil {
1157-
t.Fatalf("chtmpdir: %v", err)
1160+
t.Fatal(err)
11581161
}
1159-
}
1162+
})
11601163
}

unix/xattr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
)
1919

2020
func TestXattr(t *testing.T) {
21-
defer chtmpdir(t)()
21+
chtmpdir(t)
2222

2323
f := "xattr1"
2424
touch(t, f)

0 commit comments

Comments
 (0)