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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Kirill Kolyshkin <kolyshkin@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
This commit is contained in:
Kir Kolyshkin
2023-09-12 16:41:27 +00:00
committed by Gopher Robot
parent fc717d344a
commit fdc7ef4071
12 changed files with 31 additions and 38 deletions
+4 -5
View File
@@ -7,7 +7,6 @@ package execabs
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@@ -63,8 +62,8 @@ func TestCommand(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
executable += ".exe" executable += ".exe"
} }
if err := ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil { if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
t.Fatalf("ioutil.WriteFile failed: %s", err) t.Fatal(err)
} }
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
@@ -98,8 +97,8 @@ func TestLookPath(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
executable += ".exe" executable += ".exe"
} }
if err := ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil { if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
t.Fatalf("ioutil.WriteFile failed: %s", err) t.Fatal(err)
} }
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
+5 -5
View File
@@ -10,7 +10,7 @@ package unix_test
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort" "sort"
@@ -33,9 +33,9 @@ func TestDirent(t *testing.T) {
for i, c := range []byte("0123456789") { for i, c := range []byte("0123456789") {
name := string(bytes.Repeat([]byte{c}, filenameMinSize+i)) name := string(bytes.Repeat([]byte{c}, filenameMinSize+i))
err := ioutil.WriteFile(filepath.Join(d, name), nil, 0644) err := os.WriteFile(filepath.Join(d, name), nil, 0644)
if err != nil { if err != nil {
t.Fatalf("writefile: %v", err) t.Fatal(err)
} }
} }
@@ -100,9 +100,9 @@ func TestDirentRepeat(t *testing.T) {
files = append(files, fmt.Sprintf("file%d", i)) files = append(files, fmt.Sprintf("file%d", i))
} }
for _, file := range files { for _, file := range files {
err := ioutil.WriteFile(filepath.Join(d, file), []byte("contents"), 0644) err := os.WriteFile(filepath.Join(d, file), []byte("contents"), 0644)
if err != nil { if err != nil {
t.Fatalf("writefile: %v", err) t.Fatal(err)
} }
} }
+2 -3
View File
@@ -9,7 +9,6 @@ package unix_test
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@@ -39,9 +38,9 @@ func testGetdirentries(t *testing.T, count int) {
// Make files in the temp directory // Make files in the temp directory
for _, name := range names { for _, name := range names {
err := ioutil.WriteFile(filepath.Join(d, name), []byte("data"), 0) err := os.WriteFile(filepath.Join(d, name), []byte("data"), 0)
if err != nil { if err != nil {
t.Fatalf("WriteFile: %v", err) t.Fatal(err)
} }
} }
+1 -2
View File
@@ -12,7 +12,6 @@ package unix_test
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@@ -63,7 +62,7 @@ func TestMmap(t *testing.T) {
} }
// Read file from FS to ensure flag flipped after msync // Read file from FS to ensure flag flipped after msync
buf, err := ioutil.ReadFile(filename) buf, err := os.ReadFile(filename)
if err != nil { if err != nil {
t.Fatalf("Could not read mmapped file from disc for test: %v", err) t.Fatalf("Could not read mmapped file from disc for test: %v", err)
} }
+1 -2
View File
@@ -14,7 +14,6 @@ package unix_test
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@@ -92,7 +91,7 @@ func init() {
os.Exit(0) os.Exit(0)
}, },
func() error { func() error {
files, err := ioutil.ReadDir(".") files, err := os.ReadDir(".")
if err != nil { if err != nil {
return err return err
} }
+3 -3
View File
@@ -8,7 +8,7 @@
package unix_test package unix_test
import ( import (
"io/ioutil" "io"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
@@ -21,7 +21,7 @@ func TestSendfile(t *testing.T) {
// Set up source data file. // Set up source data file.
name := filepath.Join(t.TempDir(), "source") name := filepath.Join(t.TempDir(), "source")
const contents = "contents" const contents = "contents"
err := ioutil.WriteFile(name, []byte(contents), 0666) err := os.WriteFile(name, []byte(contents), 0666)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -41,7 +41,7 @@ func TestSendfile(t *testing.T) {
return return
} }
defer conn.Close() defer conn.Close()
b, err := ioutil.ReadAll(conn) b, err := io.ReadAll(conn)
if err != nil { if err != nil {
t.Errorf("failed to read: %v", err) t.Errorf("failed to read: %v", err)
return return
+4 -5
View File
@@ -6,7 +6,6 @@ package unix_test
import ( import (
"bytes" "bytes"
"io/ioutil"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
@@ -51,7 +50,7 @@ func TestClonefile(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
clonedData, err := ioutil.ReadFile(clonedName) clonedData, err := os.ReadFile(clonedName)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -72,7 +71,7 @@ func TestClonefileatWithCwd(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
clonedData, err := ioutil.ReadFile(clonedName) clonedData, err := os.ReadFile(clonedName)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -116,7 +115,7 @@ func TestClonefileatWithRelativePaths(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
clonedData, err := ioutil.ReadFile(dstFile.Name()) clonedData, err := os.ReadFile(dstFile.Name())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -149,7 +148,7 @@ func TestFclonefileat(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
clonedData, err := ioutil.ReadFile(dstFile.Name()) clonedData, err := os.ReadFile(dstFile.Name())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
+2 -2
View File
@@ -12,7 +12,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"os" "os"
"os/exec" "os/exec"
@@ -810,7 +810,7 @@ func TestOpenByHandleAt(t *testing.T) {
f := os.NewFile(uintptr(fd), "") f := os.NewFile(uintptr(fd), "")
defer f.Close() defer f.Close()
slurp, err := ioutil.ReadAll(f) slurp, err := io.ReadAll(f)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
+2 -2
View File
@@ -11,7 +11,7 @@ import (
"bytes" "bytes"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"os" "os"
"os/exec" "os/exec"
@@ -260,7 +260,7 @@ func TestPassFD(t *testing.T) {
f := os.NewFile(uintptr(gotFds[0]), "fd-from-child") f := os.NewFile(uintptr(gotFds[0]), "fd-from-child")
defer f.Close() defer f.Close()
got, err := ioutil.ReadAll(f) got, err := io.ReadAll(f)
want := "Hello from child process!\n" want := "Hello from child process!\n"
if string(got) != want { if string(got) != want {
t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want) t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want)
+3 -3
View File
@@ -10,7 +10,7 @@ package unix_test
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"os" "os"
"os/exec" "os/exec"
@@ -258,7 +258,7 @@ func TestPassFD(t *testing.T) {
f := os.NewFile(uintptr(gotFds[0]), "fd-from-child") f := os.NewFile(uintptr(gotFds[0]), "fd-from-child")
defer f.Close() defer f.Close()
got, err := ioutil.ReadAll(f) got, err := io.ReadAll(f)
want := "Hello from child process!\n" want := "Hello from child process!\n"
if string(got) != want { if string(got) != want {
t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want) t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want)
@@ -628,7 +628,7 @@ func TestChroot(t *testing.T) {
t.Fatalf("Chroot: %s", err.Error()) t.Fatalf("Chroot: %s", err.Error())
} }
// check if tempDir contains test file // check if tempDir contains test file
files, err := ioutil.ReadDir("/") files, err := os.ReadDir("/")
if err != nil { if err != nil {
t.Fatalf("ReadDir: %s", err.Error()) t.Fatalf("ReadDir: %s", err.Error())
} }
+2 -3
View File
@@ -9,7 +9,6 @@ package svc_test
import ( import (
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"os/exec" "os/exec"
@@ -202,7 +201,7 @@ func TestIsWindowsServiceWhenParentExits(t *testing.T) {
if isSvc { if isSvc {
msg = "IsWindowsService returns true when not running in a service." msg = "IsWindowsService returns true when not running in a service."
} }
err = ioutil.WriteFile(dumpPath, []byte(msg), 0644) err = os.WriteFile(dumpPath, []byte(msg), 0644)
if err != nil { if err != nil {
// We cannot report this error. But main test will notice // We cannot report this error. But main test will notice
// that we did not create dump file. // that we did not create dump file.
@@ -232,7 +231,7 @@ func TestIsWindowsServiceWhenParentExits(t *testing.T) {
t.Fatal("timed out waiting for child output file to be created.") t.Fatal("timed out waiting for child output file to be created.")
} }
} }
childOutput, err := ioutil.ReadFile(childDumpPath) childOutput, err := os.ReadFile(childDumpPath)
if err != nil { if err != nil {
t.Fatalf("reading child output failed: %v", err) t.Fatalf("reading child output failed: %v", err)
} }
+2 -3
View File
@@ -10,7 +10,6 @@ import (
"debug/pe" "debug/pe"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
@@ -667,14 +666,14 @@ func TestWinVerifyTrust(t *testing.T) {
// Now that we've verified the legitimate file verifies, let's corrupt it and see if it correctly fails. // Now that we've verified the legitimate file verifies, let's corrupt it and see if it correctly fails.
corruptedEvsignedfile := filepath.Join(t.TempDir(), "corrupted-file") corruptedEvsignedfile := filepath.Join(t.TempDir(), "corrupted-file")
evsignedfileBytes, err := ioutil.ReadFile(evsignedfile) evsignedfileBytes, err := os.ReadFile(evsignedfile)
if err != nil { if err != nil {
t.Fatalf("unable to read %s bytes: %v", evsignedfile, err) t.Fatalf("unable to read %s bytes: %v", evsignedfile, err)
} }
if len(evsignedfileBytes) > 0 { if len(evsignedfileBytes) > 0 {
evsignedfileBytes[len(evsignedfileBytes)/2-1]++ evsignedfileBytes[len(evsignedfileBytes)/2-1]++
} }
err = ioutil.WriteFile(corruptedEvsignedfile, evsignedfileBytes, 0755) err = os.WriteFile(corruptedEvsignedfile, evsignedfileBytes, 0755)
if err != nil { if err != nil {
t.Fatalf("unable to write corrupted ntoskrnl.exe bytes: %v", err) t.Fatalf("unable to write corrupted ntoskrnl.exe bytes: %v", err)
} }