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-06 19:08:34 -07:00
committed by Gopher Robot
parent fc717d344a
commit fdc7ef4071
12 changed files with 31 additions and 38 deletions

View File

@@ -7,7 +7,6 @@ package execabs
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@@ -63,8 +62,8 @@ func TestCommand(t *testing.T) {
if runtime.GOOS == "windows" {
executable += ".exe"
}
if err := ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
t.Fatalf("ioutil.WriteFile failed: %s", err)
if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
t.Fatal(err)
}
cwd, err := os.Getwd()
if err != nil {
@@ -98,8 +97,8 @@ func TestLookPath(t *testing.T) {
if runtime.GOOS == "windows" {
executable += ".exe"
}
if err := ioutil.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
t.Fatalf("ioutil.WriteFile failed: %s", err)
if err := os.WriteFile(filepath.Join(tmpDir, executable), []byte{1, 2, 3}, 0111); err != nil {
t.Fatal(err)
}
cwd, err := os.Getwd()
if err != nil {

View File

@@ -10,7 +10,7 @@ package unix_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
@@ -33,9 +33,9 @@ func TestDirent(t *testing.T) {
for i, c := range []byte("0123456789") {
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 {
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))
}
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 {
t.Fatalf("writefile: %v", err)
t.Fatal(err)
}
}

View File

@@ -9,7 +9,6 @@ package unix_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
@@ -39,9 +38,9 @@ func testGetdirentries(t *testing.T, count int) {
// Make files in the temp directory
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 {
t.Fatalf("WriteFile: %v", err)
t.Fatal(err)
}
}

View File

@@ -12,7 +12,6 @@ package unix_test
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -63,7 +62,7 @@ func TestMmap(t *testing.T) {
}
// Read file from FS to ensure flag flipped after msync
buf, err := ioutil.ReadFile(filename)
buf, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("Could not read mmapped file from disc for test: %v", err)
}

View File

@@ -14,7 +14,6 @@ package unix_test
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@@ -92,7 +91,7 @@ func init() {
os.Exit(0)
},
func() error {
files, err := ioutil.ReadDir(".")
files, err := os.ReadDir(".")
if err != nil {
return err
}

View File

@@ -8,7 +8,7 @@
package unix_test
import (
"io/ioutil"
"io"
"net"
"os"
"path/filepath"
@@ -21,7 +21,7 @@ func TestSendfile(t *testing.T) {
// Set up source data file.
name := filepath.Join(t.TempDir(), "source")
const contents = "contents"
err := ioutil.WriteFile(name, []byte(contents), 0666)
err := os.WriteFile(name, []byte(contents), 0666)
if err != nil {
t.Fatal(err)
}
@@ -41,7 +41,7 @@ func TestSendfile(t *testing.T) {
return
}
defer conn.Close()
b, err := ioutil.ReadAll(conn)
b, err := io.ReadAll(conn)
if err != nil {
t.Errorf("failed to read: %v", err)
return

View File

@@ -6,7 +6,6 @@ package unix_test
import (
"bytes"
"io/ioutil"
"net"
"os"
"path/filepath"
@@ -51,7 +50,7 @@ func TestClonefile(t *testing.T) {
t.Fatal(err)
}
clonedData, err := ioutil.ReadFile(clonedName)
clonedData, err := os.ReadFile(clonedName)
if err != nil {
t.Fatal(err)
}
@@ -72,7 +71,7 @@ func TestClonefileatWithCwd(t *testing.T) {
t.Fatal(err)
}
clonedData, err := ioutil.ReadFile(clonedName)
clonedData, err := os.ReadFile(clonedName)
if err != nil {
t.Fatal(err)
}
@@ -116,7 +115,7 @@ func TestClonefileatWithRelativePaths(t *testing.T) {
t.Fatal(err)
}
clonedData, err := ioutil.ReadFile(dstFile.Name())
clonedData, err := os.ReadFile(dstFile.Name())
if err != nil {
t.Fatal(err)
}
@@ -149,7 +148,7 @@ func TestFclonefileat(t *testing.T) {
t.Fatal(err)
}
clonedData, err := ioutil.ReadFile(dstFile.Name())
clonedData, err := os.ReadFile(dstFile.Name())
if err != nil {
t.Fatal(err)
}

View File

@@ -12,7 +12,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"os"
"os/exec"
@@ -810,7 +810,7 @@ func TestOpenByHandleAt(t *testing.T) {
f := os.NewFile(uintptr(fd), "")
defer f.Close()
slurp, err := ioutil.ReadAll(f)
slurp, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}

View File

@@ -11,7 +11,7 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"io"
"net"
"os"
"os/exec"
@@ -260,7 +260,7 @@ func TestPassFD(t *testing.T) {
f := os.NewFile(uintptr(gotFds[0]), "fd-from-child")
defer f.Close()
got, err := ioutil.ReadAll(f)
got, err := io.ReadAll(f)
want := "Hello from child process!\n"
if string(got) != want {
t.Errorf("child process ReadAll: %q, %v; want %q", got, err, want)

View File

@@ -10,7 +10,7 @@ package unix_test
import (
"flag"
"fmt"
"io/ioutil"
"io"
"net"
"os"
"os/exec"
@@ -258,7 +258,7 @@ func TestPassFD(t *testing.T) {
f := os.NewFile(uintptr(gotFds[0]), "fd-from-child")
defer f.Close()
got, err := ioutil.ReadAll(f)
got, err := io.ReadAll(f)
want := "Hello from child process!\n"
if string(got) != 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())
}
// check if tempDir contains test file
files, err := ioutil.ReadDir("/")
files, err := os.ReadDir("/")
if err != nil {
t.Fatalf("ReadDir: %s", err.Error())
}

View File

@@ -9,7 +9,6 @@ package svc_test
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
@@ -202,7 +201,7 @@ func TestIsWindowsServiceWhenParentExits(t *testing.T) {
if isSvc {
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 {
// We cannot report this error. But main test will notice
// 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.")
}
}
childOutput, err := ioutil.ReadFile(childDumpPath)
childOutput, err := os.ReadFile(childDumpPath)
if err != nil {
t.Fatalf("reading child output failed: %v", err)
}

View File

@@ -10,7 +10,6 @@ import (
"debug/pe"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"os"
"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.
corruptedEvsignedfile := filepath.Join(t.TempDir(), "corrupted-file")
evsignedfileBytes, err := ioutil.ReadFile(evsignedfile)
evsignedfileBytes, err := os.ReadFile(evsignedfile)
if err != nil {
t.Fatalf("unable to read %s bytes: %v", evsignedfile, err)
}
if len(evsignedfileBytes) > 0 {
evsignedfileBytes[len(evsignedfileBytes)/2-1]++
}
err = ioutil.WriteFile(corruptedEvsignedfile, evsignedfileBytes, 0755)
err = os.WriteFile(corruptedEvsignedfile, evsignedfileBytes, 0755)
if err != nil {
t.Fatalf("unable to write corrupted ntoskrnl.exe bytes: %v", err)
}