Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os packages
Browse files Browse the repository at this point in the history
The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

This commit also uses
`testing#T.TempDir` (https://pkg.go.dev/testing#T.TempDir) to create
temporary directory in tests instead of `ioutil.TempDir`.

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored and rjoleary committed Dec 2, 2021
1 parent 69bc44b commit 970fddf
Show file tree
Hide file tree
Showing 167 changed files with 449 additions and 1,003 deletions.
8 changes: 4 additions & 4 deletions cmds/boot/fbnetboot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"errors"
"flag"
"fmt"
"io/ioutil"
"io"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -253,7 +253,7 @@ func boot(ifname string, dhcp dhcpFunc) error {
if resp.StatusCode != 200 {
return fmt.Errorf("status code is not 200 OK: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("DHCP: cannot read boot file from the network: %v", err)
}
Expand All @@ -270,7 +270,7 @@ func boot(ifname string, dhcp dhcpFunc) error {
if filename == "." || filename == "" {
return fmt.Errorf("invalid empty file name extracted from file path %s", u.Path)
}
if err = ioutil.WriteFile(filename, body, 0o400); err != nil {
if err = os.WriteFile(filename, body, 0o400); err != nil {
return fmt.Errorf("DHCP: cannot write to file %s: %v", filename, err)
}
debug("DHCP: saved boot file to %s", filename)
Expand Down Expand Up @@ -315,7 +315,7 @@ func loadCaCerts() (*x509.CertPool, error) {
debug("certs: rootCAs == nil")
rootCAs = x509.NewCertPool()
}
caCerts, err := ioutil.ReadFile(*caCertFile)
caCerts, err := os.ReadFile(*caCertFile)
if err != nil {
return nil, fmt.Errorf("could not find cert file '%v' - %v", *caCertFile, err)
}
Expand Down
3 changes: 1 addition & 2 deletions cmds/boot/localboot/grub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"encoding/hex"
"io/ioutil"
"log"
"os"
"path"
Expand Down Expand Up @@ -230,7 +229,7 @@ func ScanGrubConfigs(devices block.BlockDevices, basedir string) []jsonboot.Boot
return nil
}
log.Printf("Parsing %s", currentPath)
data, err := ioutil.ReadFile(currentPath)
data, err := os.ReadFile(currentPath)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmds/contrib/flash/flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"sort"
Expand Down Expand Up @@ -160,7 +159,7 @@ func run(args []string, supportedProgrammers map[string]programmerInit) (reterr
if _, err := io.ReadFull(f, buf); err != nil {
return err
}
if leftover, err := io.Copy(ioutil.Discard, f); err != nil {
if leftover, err := io.Copy(io.Discard, f); err != nil {
return err
} else if leftover != 0 {
return fmt.Errorf("flash size (%#x) unequal to file size (%#x)", len(buf), int64(len(buf))+leftover)
Expand Down
3 changes: 1 addition & 2 deletions cmds/contrib/spidev/spidev.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package main
import (
"errors"
"io"
"io/ioutil"
"log"
"os"

Expand Down Expand Up @@ -71,7 +70,7 @@ func run(args []string, spiOpen spiOpenFunc, input io.Reader, output io.Writer)
switch fs.Args()[0] {
case "raw":
// Create transfer from stdin.
tx, err := ioutil.ReadAll(input)
tx, err := io.ReadAll(input)
if err != nil {
return err
}
Expand Down
9 changes: 2 additions & 7 deletions cmds/core/cat/cat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -19,14 +18,11 @@ import (
// setup writes a set of files, putting 1 byte in each file.
func setup(t *testing.T, data []byte) (string, error) {
t.Logf(":: Creating simulation data. ")
dir, err := ioutil.TempDir("", "cat.dir")
if err != nil {
return "", err
}
dir := t.TempDir()

for i, d := range data {
n := fmt.Sprintf("%v%d", filepath.Join(dir, "file"), i)
if err := ioutil.WriteFile(n, []byte{d}, 0o666); err != nil {
if err := os.WriteFile(n, []byte{d}, 0o666); err != nil {
return "", err
}
}
Expand All @@ -45,7 +41,6 @@ func TestCat(t *testing.T) {
if err != nil {
t.Fatalf("setup has failed, %v", err)
}
defer os.RemoveAll(dir)

for i := range someData {
files = append(files, fmt.Sprintf("%v%d", filepath.Join(dir, "file"), i))
Expand Down
36 changes: 7 additions & 29 deletions cmds/core/chmod/chmod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -29,13 +28,7 @@ func run(c *exec.Cmd) (string, string, error) {

func TestChmodSimple(t *testing.T) {
// Temporary directories.
tempDir, err := ioutil.TempDir("", "TestChmodSimple")
if err != nil {
t.Fatalf("cannot create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)

f, err := ioutil.TempFile(tempDir, "BLAH1")
f, err := os.CreateTemp(t.TempDir(), "BLAH1")
if err != nil {
t.Fatalf("cannot create temporary file: %v", err)
}
Expand Down Expand Up @@ -100,11 +93,7 @@ func checkPath(t *testing.T, path string, instruction string, v fileModeTrans) {

func TestChmodRecursive(t *testing.T) {
// Temporary directories.
tempDir, err := ioutil.TempDir("", "TestChmodRecursive")
if err != nil {
t.Fatalf("cannot create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

var targetDirectories []string
for _, dir := range []string{
Expand Down Expand Up @@ -148,8 +137,7 @@ func TestChmodRecursive(t *testing.T) {

// Set permissions using chmod.
c := testutil.Command(t, "-R", k, tempDir)
err = c.Run()
if err != nil {
if err := c.Run(); err != nil {
t.Fatalf("setting permissions failed: %v", err)
}

Expand All @@ -162,19 +150,15 @@ func TestChmodRecursive(t *testing.T) {

func TestChmodReference(t *testing.T) {
// Temporary directories.
tempDir, err := ioutil.TempDir("", "TestChmodReference")
if err != nil {
t.Fatalf("cannot create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

sourceFile, err := ioutil.TempFile(tempDir, "BLAH1")
sourceFile, err := os.CreateTemp(tempDir, "BLAH1")
if err != nil {
t.Fatalf("cannot create temporary file: %v", err)
}
defer sourceFile.Close()

targetFile, err := ioutil.TempFile(tempDir, "BLAH2")
targetFile, err := os.CreateTemp(tempDir, "BLAH2")
if err != nil {
t.Fatalf("cannot create temporary file: %v", err)
}
Expand Down Expand Up @@ -209,13 +193,7 @@ func TestChmodReference(t *testing.T) {
}

func TestInvocationErrors(t *testing.T) {
tempDir, err := ioutil.TempDir("", "TestInvocationErrors")
if err != nil {
t.Fatalf("cannot create temporary directory: %v", err)
}
defer os.RemoveAll(tempDir)

f, err := ioutil.TempFile(tempDir, "BLAH1")
f, err := os.CreateTemp(t.TempDir(), "BLAH1")
if err != nil {
t.Fatalf("cannot create temporary file: %v", err)
}
Expand Down
9 changes: 2 additions & 7 deletions cmds/core/comm/comm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -55,18 +54,14 @@ var commTests = []struct {

// TestComm implements a table-drivent test.
func TestComm(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "comm")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()

for _, test := range commTests {
// Write inputs into the two files
var files [2]string
for i, contents := range []string{test.in1, test.in2} {
files[i] = filepath.Join(tmpDir, fmt.Sprintf("txt%d", i))
if err := ioutil.WriteFile(files[i], []byte(contents), 0o600); err != nil {
if err := os.WriteFile(files[i], []byte(contents), 0o600); err != nil {
t.Fatalf("Failed to create test file %d: %v", i, err)
}
}
Expand Down
52 changes: 12 additions & 40 deletions cmds/core/cp/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
Expand Down Expand Up @@ -38,7 +37,7 @@ func resetFlags() {

// randomFile create a random file with random content
func randomFile(fpath, prefix string) (*os.File, error) {
f, err := ioutil.TempFile(fpath, prefix)
f, err := os.CreateTemp(fpath, prefix)
if err != nil {
return nil, err
}
Expand All @@ -56,7 +55,7 @@ func randomFile(fpath, prefix string) (*os.File, error) {
func createFilesTree(root string, maxDepth, depth int) error {
// create more one dir if don't achieve the maxDepth
if depth < maxDepth {
newDir, err := ioutil.TempDir(root, fmt.Sprintf("cpdir_%d_", depth))
newDir, err := os.MkdirTemp(root, fmt.Sprintf("cpdir_%d_", depth))
if err != nil {
return err
}
Expand All @@ -80,11 +79,7 @@ func createFilesTree(root string, maxDepth, depth int) error {
// TestCpsSimple make a simple test for copy file-to-file
// cmd-line equivalent: cp file file-copy
func TestCpSimple(t *testing.T) {
tempDir, err := ioutil.TempDir("", "TestCpSimple")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

f, err := randomFile(tempDir, "src-")
if err != nil {
Expand All @@ -109,17 +104,8 @@ func TestCpSrcDirectory(t *testing.T) {
flags.recursive = false
defer resetFlags()

tempDir, err := ioutil.TempDir("", "TestCpSrcDirectory")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)

tempDirTwo, err := ioutil.TempDir("", "TestCpSrcDirectoryTwo")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDirTwo)
tempDir := t.TempDir()
tempDirTwo := t.TempDir()

// capture log output to verify
var logBytes bytes.Buffer
Expand All @@ -144,11 +130,7 @@ func TestCpRecursive(t *testing.T) {
flags.recursive = true
defer resetFlags()

tempDir, err := ioutil.TempDir("", "TestCpRecursive")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

srcDir := filepath.Join(tempDir, "src")
if err := os.Mkdir(srcDir, 0o755); err != nil {
Expand All @@ -159,7 +141,7 @@ func TestCpRecursive(t *testing.T) {
t.Fatal(err)
}

if err = createFilesTree(srcDir, maxDirDepth, 0); err != nil {
if err := createFilesTree(srcDir, maxDirDepth, 0); err != nil {
t.Fatalf("cannot create files tree on directory %q: %v", srcDir, err)
}

Expand Down Expand Up @@ -198,11 +180,7 @@ func TestCpRecursive(t *testing.T) {
func TestCpRecursiveMultiple(t *testing.T) {
flags.recursive = true
defer resetFlags()
tempDir, err := ioutil.TempDir("", "TestCpRecursiveMultiple")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

dstTest := filepath.Join(tempDir, "destination")
if err := os.Mkdir(dstTest, 0o755); err != nil {
Expand All @@ -212,11 +190,9 @@ func TestCpRecursiveMultiple(t *testing.T) {
// create multiple random directories sources
srcDirs := []string{}
for i := 0; i < maxDirDepth; i++ {
srcTest, err := ioutil.TempDir(tempDir, "src-")
if err != nil {
t.Fatalf("Failed on build directory %v: %v\n", srcTest, err)
}
if err = createFilesTree(srcTest, maxDirDepth, 0); err != nil {
srcTest := t.TempDir()

if err := createFilesTree(srcTest, maxDirDepth, 0); err != nil {
t.Fatalf("cannot create files tree on directory %v: %v", srcTest, err)
}

Expand Down Expand Up @@ -247,11 +223,7 @@ func TestCpRecursiveMultiple(t *testing.T) {
// using -P don't follow symlinks, create other symlink
// cmd-line equivalent: $ cp -P symlink symlink-copy
func TestCpSymlink(t *testing.T) {
tempDir, err := ioutil.TempDir("", "TestCpSymlink")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

f, err := randomFile(tempDir, "src-")
if err != nil {
Expand Down
Loading

0 comments on commit 970fddf

Please sign in to comment.