Skip to content

Commit fdc7ef4

Browse files
kolyshkingopherbot
authored andcommitted
all: remove ioutil usage from tests
This removes the remaining (and trivial) use of deprecated ioutil package from test files. Replacements are easy: ioutil.ReadAll -> io.ReadAll ioutil.ReadDir -> os.ReadDir ioutil.ReadFile -> os.ReadFile ioutil.WriteFile -> os.WriteFile While at it, simplify some error reporting. Change-Id: I60a242fd3c08d8fe571a18f16716439a9acdd59d Reviewed-on: https://go-review.googlesource.com/c/sys/+/526299 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Kirill Kolyshkin <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Tobias Klauser <[email protected]>
1 parent fc717d3 commit fdc7ef4

12 files changed

+31
-38
lines changed

execabs/execabs_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package execabs
77
import (
88
"context"
99
"fmt"
10-
"io/ioutil"
1110
"os"
1211
"os/exec"
1312
"path/filepath"
@@ -63,8 +62,8 @@ func TestCommand(t *testing.T) {
6362
if runtime.GOOS == "windows" {
6463
executable += ".exe"
6564
}
66-
if err := ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
67-
t.Fatalf("ioutil.WriteFile failed: %s", err)
65+
if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
66+
t.Fatal(err)
6867
}
6968
cwd, err := os.Getwd()
7069
if err != nil {
@@ -98,8 +97,8 @@ func TestLookPath(t *testing.T) {
9897
if runtime.GOOS == "windows" {
9998
executable += ".exe"
10099
}
101-
if err := ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
102-
t.Fatalf("ioutil.WriteFile failed: %s", err)
100+
if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
101+
t.Fatal(err)
103102
}
104103
cwd, err := os.Getwd()
105104
if err != nil {

unix/dirent_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package unix_test
1010
import (
1111
"bytes"
1212
"fmt"
13-
"io/ioutil"
13+
"os"
1414
"path/filepath"
1515
"runtime"
1616
"sort"
@@ -33,9 +33,9 @@ func TestDirent(t *testing.T) {
3333

3434
for i, c := range []byte("0123456789") {
3535
name := string(bytes.Repeat([]byte{c}, filenameMinSize+i))
36-
err := ioutil.WriteFile(filepath.Join(d, name), nil, 0644)
36+
err := os.WriteFile(filepath.Join(d, name), nil, 0644)
3737
if err != nil {
38-
t.Fatalf("writefile: %v", err)
38+
t.Fatal(err)
3939
}
4040
}
4141

@@ -100,9 +100,9 @@ func TestDirentRepeat(t *testing.T) {
100100
files = append(files, fmt.Sprintf("file%d", i))
101101
}
102102
for _, file := range files {
103-
err := ioutil.WriteFile(filepath.Join(d, file), []byte("contents"), 0644)
103+
err := os.WriteFile(filepath.Join(d, file), []byte("contents"), 0644)
104104
if err != nil {
105-
t.Fatalf("writefile: %v", err)
105+
t.Fatal(err)
106106
}
107107
}
108108

unix/getdirentries_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package unix_test
99

1010
import (
1111
"fmt"
12-
"io/ioutil"
1312
"os"
1413
"path/filepath"
1514
"sort"
@@ -39,9 +38,9 @@ func testGetdirentries(t *testing.T, count int) {
3938

4039
// Make files in the temp directory
4140
for _, name := range names {
42-
err := ioutil.WriteFile(filepath.Join(d, name), []byte("data"), 0)
41+
err := os.WriteFile(filepath.Join(d, name), []byte("data"), 0)
4342
if err != nil {
44-
t.Fatalf("WriteFile: %v", err)
43+
t.Fatal(err)
4544
}
4645
}
4746

unix/mmap_zos_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ package unix_test
1212

1313
import (
1414
"fmt"
15-
"io/ioutil"
1615
"os"
1716
"path/filepath"
1817
"testing"
@@ -63,7 +62,7 @@ func TestMmap(t *testing.T) {
6362
}
6463

6564
// Read file from FS to ensure flag flipped after msync
66-
buf, err := ioutil.ReadFile(filename)
65+
buf, err := os.ReadFile(filename)
6766
if err != nil {
6867
t.Fatalf("Could not read mmapped file from disc for test: %v", err)
6968
}

unix/openbsd_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ package unix_test
1414
import (
1515
"flag"
1616
"fmt"
17-
"io/ioutil"
1817
"os"
1918
"os/exec"
2019
"path/filepath"
@@ -92,7 +91,7 @@ func init() {
9291
os.Exit(0)
9392
},
9493
func() error {
95-
files, err := ioutil.ReadDir(".")
94+
files, err := os.ReadDir(".")
9695
if err != nil {
9796
return err
9897
}

unix/sendfile_test.go

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

1010
import (
11-
"io/ioutil"
11+
"io"
1212
"net"
1313
"os"
1414
"path/filepath"
@@ -21,7 +21,7 @@ func TestSendfile(t *testing.T) {
2121
// Set up source data file.
2222
name := filepath.Join(t.TempDir(), "source")
2323
const contents = "contents"
24-
err := ioutil.WriteFile(name, []byte(contents), 0666)
24+
err := os.WriteFile(name, []byte(contents), 0666)
2525
if err != nil {
2626
t.Fatal(err)
2727
}
@@ -41,7 +41,7 @@ func TestSendfile(t *testing.T) {
4141
return
4242
}
4343
defer conn.Close()
44-
b, err := ioutil.ReadAll(conn)
44+
b, err := io.ReadAll(conn)
4545
if err != nil {
4646
t.Errorf("failed to read: %v", err)
4747
return

unix/syscall_darwin_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package unix_test
66

77
import (
88
"bytes"
9-
"io/ioutil"
109
"net"
1110
"os"
1211
"path/filepath"
@@ -51,7 +50,7 @@ func TestClonefile(t *testing.T) {
5150
t.Fatal(err)
5251
}
5352

54-
clonedData, err := ioutil.ReadFile(clonedName)
53+
clonedData, err := os.ReadFile(clonedName)
5554
if err != nil {
5655
t.Fatal(err)
5756
}
@@ -72,7 +71,7 @@ func TestClonefileatWithCwd(t *testing.T) {
7271
t.Fatal(err)
7372
}
7473

75-
clonedData, err := ioutil.ReadFile(clonedName)
74+
clonedData, err := os.ReadFile(clonedName)
7675
if err != nil {
7776
t.Fatal(err)
7877
}
@@ -116,7 +115,7 @@ func TestClonefileatWithRelativePaths(t *testing.T) {
116115
t.Fatal(err)
117116
}
118117

119-
clonedData, err := ioutil.ReadFile(dstFile.Name())
118+
clonedData, err := os.ReadFile(dstFile.Name())
120119
if err != nil {
121120
t.Fatal(err)
122121
}
@@ -149,7 +148,7 @@ func TestFclonefileat(t *testing.T) {
149148
t.Fatal(err)
150149
}
151150

152-
clonedData, err := ioutil.ReadFile(dstFile.Name())
151+
clonedData, err := os.ReadFile(dstFile.Name())
153152
if err != nil {
154153
t.Fatal(err)
155154
}

unix/syscall_linux_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"bytes"
1313
"errors"
1414
"fmt"
15-
"io/ioutil"
15+
"io"
1616
"net"
1717
"os"
1818
"os/exec"
@@ -810,7 +810,7 @@ func TestOpenByHandleAt(t *testing.T) {
810810
f := os.NewFile(uintptr(fd), "")
811811
defer f.Close()
812812

813-
slurp, err := ioutil.ReadAll(f)
813+
slurp, err := io.ReadAll(f)
814814
if err != nil {
815815
t.Fatal(err)
816816
}

unix/syscall_unix_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"bytes"
1212
"flag"
1313
"fmt"
14-
"io/ioutil"
14+
"io"
1515
"net"
1616
"os"
1717
"os/exec"
@@ -260,7 +260,7 @@ func TestPassFD(t *testing.T) {
260260
f := os.NewFile(uintptr(gotFds[0]), "fd-from-child")
261261
defer f.Close()
262262

263-
got, err := ioutil.ReadAll(f)
263+
got, err := io.ReadAll(f)
264264
want := "Hello from child process!\n"
265265
if string(got) != want {
266266
t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want)

unix/syscall_zos_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package unix_test
1010
import (
1111
"flag"
1212
"fmt"
13-
"io/ioutil"
13+
"io"
1414
"net"
1515
"os"
1616
"os/exec"
@@ -258,7 +258,7 @@ func TestPassFD(t *testing.T) {
258258
f := os.NewFile(uintptr(gotFds[0]), "fd-from-child")
259259
defer f.Close()
260260

261-
got, err := ioutil.ReadAll(f)
261+
got, err := io.ReadAll(f)
262262
want := "Hello from child process!\n"
263263
if string(got) != want {
264264
t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want)
@@ -628,7 +628,7 @@ func TestChroot(t *testing.T) {
628628
t.Fatalf("Chroot: %s", err.Error())
629629
}
630630
// check if tempDir contains test file
631-
files, err := ioutil.ReadDir("/")
631+
files, err := os.ReadDir("/")
632632
if err != nil {
633633
t.Fatalf("ReadDir: %s", err.Error())
634634
}

windows/svc/svc_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package svc_test
99

1010
import (
1111
"fmt"
12-
"io/ioutil"
1312
"math/rand"
1413
"os"
1514
"os/exec"
@@ -202,7 +201,7 @@ func TestIsWindowsServiceWhenParentExits(t *testing.T) {
202201
if isSvc {
203202
msg = "IsWindowsService returns true when not running in a service."
204203
}
205-
err = ioutil.WriteFile(dumpPath, []byte(msg), 0644)
204+
err = os.WriteFile(dumpPath, []byte(msg), 0644)
206205
if err != nil {
207206
// We cannot report this error. But main test will notice
208207
// that we did not create dump file.
@@ -232,7 +231,7 @@ func TestIsWindowsServiceWhenParentExits(t *testing.T) {
232231
t.Fatal("timed out waiting for child output file to be created.")
233232
}
234233
}
235-
childOutput, err := ioutil.ReadFile(childDumpPath)
234+
childOutput, err := os.ReadFile(childDumpPath)
236235
if err != nil {
237236
t.Fatalf("reading child output failed: %v", err)
238237
}

windows/syscall_windows_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"debug/pe"
1111
"errors"
1212
"fmt"
13-
"io/ioutil"
1413
"math/rand"
1514
"os"
1615
"path/filepath"
@@ -667,14 +666,14 @@ func TestWinVerifyTrust(t *testing.T) {
667666
// Now that we've verified the legitimate file verifies, let's corrupt it and see if it correctly fails.
668667

669668
corruptedEvsignedfile := filepath.Join(t.TempDir(), "corrupted-file")
670-
evsignedfileBytes, err := ioutil.ReadFile(evsignedfile)
669+
evsignedfileBytes, err := os.ReadFile(evsignedfile)
671670
if err != nil {
672671
t.Fatalf("unable to read %s bytes: %v", evsignedfile, err)
673672
}
674673
if len(evsignedfileBytes) > 0 {
675674
evsignedfileBytes[len(evsignedfileBytes)/2-1]++
676675
}
677-
err = ioutil.WriteFile(corruptedEvsignedfile, evsignedfileBytes, 0755)
676+
err = os.WriteFile(corruptedEvsignedfile, evsignedfileBytes, 0755)
678677
if err != nil {
679678
t.Fatalf("unable to write corrupted ntoskrnl.exe bytes: %v", err)
680679
}

0 commit comments

Comments
 (0)