Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os package
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.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee committed Sep 21, 2021
1 parent c16be1a commit 50da673
Show file tree
Hide file tree
Showing 126 changed files with 291 additions and 399 deletions.
11 changes: 5 additions & 6 deletions archive/compression/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"compress/gzip"
"context"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
Expand Down Expand Up @@ -80,7 +79,7 @@ func testCompressDecompress(t *testing.T, size int, compression Compression) Dec
if err != nil {
t.Fatal(err)
}
decompressed, err := ioutil.ReadAll(decompressor)
decompressed, err := io.ReadAll(decompressor)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -123,7 +122,7 @@ func TestCompressDecompressUncompressed(t *testing.T) {

func TestDetectPigz(t *testing.T) {
// Create fake PATH with unpigz executable, make sure detectPigz can find it
tempPath, err := ioutil.TempDir("", "containerd_temp_")
tempPath, err := os.MkdirTemp("", "containerd_temp_")
if err != nil {
t.Fatal(err)
}
Expand All @@ -135,7 +134,7 @@ func TestDetectPigz(t *testing.T) {

fullPath := filepath.Join(tempPath, filename)

if err := ioutil.WriteFile(fullPath, []byte(""), 0111); err != nil {
if err := os.WriteFile(fullPath, []byte(""), 0111); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -165,7 +164,7 @@ func TestCmdStream(t *testing.T) {
t.Fatal(err)
}

buf, err := ioutil.ReadAll(out)
buf, err := io.ReadAll(out)
if err != nil {
t.Fatalf("failed to read from stdout: %s", err)
}
Expand All @@ -181,7 +180,7 @@ func TestCmdStreamBad(t *testing.T) {
t.Fatalf("failed to start command: %v", err)
}

if buf, err := ioutil.ReadAll(out); err == nil {
if buf, err := io.ReadAll(out); err == nil {
t.Fatal("command should have failed")
} else if err.Error() != "exit status 1: bad result\n" {
t.Fatalf("wrong error: %s", err.Error())
Expand Down
3 changes: 1 addition & 2 deletions archive/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package archive
import (
"bytes"
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -37,7 +36,7 @@ func TestPrefixHeaderReadable(t *testing.T) {
// https://gist.github.com/stevvooe/e2a790ad4e97425896206c0816e1a882#file-out-go
var testFile = []byte("\x1f\x8b\x08\x08\x44\x21\x68\x59\x00\x03\x74\x2e\x74\x61\x72\x00\x4b\xcb\xcf\x67\xa0\x35\x30\x80\x00\x86\x06\x10\x47\x01\xc1\x37\x40\x00\x54\xb6\xb1\xa1\xa9\x99\x09\x48\x25\x1d\x40\x69\x71\x49\x62\x91\x02\xe5\x76\xa1\x79\x84\x21\x91\xd6\x80\x72\xaf\x8f\x82\x51\x30\x0a\x46\x36\x00\x00\xf0\x1c\x1e\x95\x00\x06\x00\x00")

tmpDir, err := ioutil.TempDir("", "prefix-test")
tmpDir, err := os.MkdirTemp("", "prefix-test")
if err != nil {
t.Fatal(err)
}
Expand Down
11 changes: 5 additions & 6 deletions archive/tar_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
Expand All @@ -38,7 +37,7 @@ import (
func TestOverlayApply(t *testing.T) {
testutil.RequiresRoot(t)

base, err := ioutil.TempDir("", "test-ovl-diff-apply-")
base, err := os.MkdirTemp("", "test-ovl-diff-apply-")
if err != nil {
t.Fatalf("unable to create temp dir: %+v", err)
}
Expand All @@ -57,7 +56,7 @@ func TestOverlayApply(t *testing.T) {
func TestOverlayApplyNoParents(t *testing.T) {
testutil.RequiresRoot(t)

base, err := ioutil.TempDir("", "test-ovl-diff-apply-")
base, err := os.MkdirTemp("", "test-ovl-diff-apply-")
if err != nil {
t.Fatalf("unable to create temp dir: %+v", err)
}
Expand Down Expand Up @@ -96,7 +95,7 @@ type overlayContext struct {
type contextKey struct{}

func (d overlayDiffApplier) TestContext(ctx context.Context) (context.Context, func(), error) {
merged, err := ioutil.TempDir(d.tmp, "merged")
merged, err := os.MkdirTemp(d.tmp, "merged")
if err != nil {
return ctx, nil, errors.Wrap(err, "failed to make merged dir")
}
Expand All @@ -117,7 +116,7 @@ func (d overlayDiffApplier) TestContext(ctx context.Context) (context.Context, f
func (d overlayDiffApplier) Apply(ctx context.Context, a fstest.Applier) (string, func(), error) {
oc := ctx.Value(contextKey{}).(*overlayContext)

applyCopy, err := ioutil.TempDir(d.tmp, "apply-copy-")
applyCopy, err := os.MkdirTemp(d.tmp, "apply-copy-")
if err != nil {
return "", nil, errors.Wrap(err, "failed to create temp dir")
}
Expand Down Expand Up @@ -149,7 +148,7 @@ func (d overlayDiffApplier) Apply(ctx context.Context, a fstest.Applier) (string
oc.mounted = false
}

next, err := ioutil.TempDir(d.tmp, "lower-")
next, err := os.MkdirTemp(d.tmp, "lower-")
if err != nil {
return "", nil, errors.Wrap(err, "failed to create temp dir")
}
Expand Down
43 changes: 21 additions & 22 deletions archive/tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -234,7 +233,7 @@ func TestBreakouts(t *testing.T) {
tc := tartest.TarContext{}.WithUIDGID(os.Getuid(), os.Getgid()).WithModTime(time.Now().UTC())
expected := "unbroken"
unbrokenCheck := func(root string) error {
b, err := ioutil.ReadFile(filepath.Join(root, "etc", "unbroken"))
b, err := os.ReadFile(filepath.Join(root, "etc", "unbroken"))
if err != nil {
return errors.Wrap(err, "failed to read unbroken")
}
Expand All @@ -244,7 +243,7 @@ func TestBreakouts(t *testing.T) {
return nil
}
errFileDiff := errors.New("files differ")
td, err := ioutil.TempDir("", "test-breakouts-")
td, err := os.MkdirTemp("", "test-breakouts-")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -331,7 +330,7 @@ func TestBreakouts(t *testing.T) {
}
fileValue := func(f1 string, content []byte) func(string) error {
return func(root string) error {
b, err := ioutil.ReadFile(filepath.Join(root, f1))
b, err := os.ReadFile(filepath.Join(root, f1))
if err != nil {
return err
}
Expand Down Expand Up @@ -421,7 +420,7 @@ func TestBreakouts(t *testing.T) {
tc.File("/localetc/emptied", []byte{}, 0644),
),
validator: func(root string) error {
b, err := ioutil.ReadFile(filepath.Join(root, "etc", "emptied"))
b, err := os.ReadFile(filepath.Join(root, "etc", "emptied"))
if err != nil {
return errors.Wrap(err, "failed to read unbroken")
}
Expand Down Expand Up @@ -755,11 +754,11 @@ func TestBreakouts(t *testing.T) {
name: "HardlinkSymlinkChmod",
w: func() tartest.WriterToTar {
p := filepath.Join(td, "perm400")
if err := ioutil.WriteFile(p, []byte("..."), 0400); err != nil {
if err := os.WriteFile(p, []byte("..."), 0400); err != nil {
t.Fatal(err)
}
ep := filepath.Join(td, "also-exists-outside-root")
if err := ioutil.WriteFile(ep, []byte("..."), 0640); err != nil {
if err := os.WriteFile(ep, []byte("..."), 0640); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -843,12 +842,12 @@ func TestApplyTar(t *testing.T) {
}

func testApply(a fstest.Applier) error {
td, err := ioutil.TempDir("", "test-apply-")
td, err := os.MkdirTemp("", "test-apply-")
if err != nil {
return errors.Wrap(err, "failed to create temp dir")
}
defer os.RemoveAll(td)
dest, err := ioutil.TempDir("", "test-apply-dest-")
dest, err := os.MkdirTemp("", "test-apply-dest-")
if err != nil {
return errors.Wrap(err, "failed to create temp dir")
}
Expand Down Expand Up @@ -884,12 +883,12 @@ func testApply(a fstest.Applier) error {
}

func testBaseDiff(a fstest.Applier) error {
td, err := ioutil.TempDir("", "test-base-diff-")
td, err := os.MkdirTemp("", "test-base-diff-")
if err != nil {
return errors.Wrap(err, "failed to create temp dir")
}
defer os.RemoveAll(td)
dest, err := ioutil.TempDir("", "test-base-diff-dest-")
dest, err := os.MkdirTemp("", "test-base-diff-dest-")
if err != nil {
return errors.Wrap(err, "failed to create temp dir")
}
Expand All @@ -911,12 +910,12 @@ func testBaseDiff(a fstest.Applier) error {
}

func testDiffApply(appliers ...fstest.Applier) error {
td, err := ioutil.TempDir("", "test-diff-apply-")
td, err := os.MkdirTemp("", "test-diff-apply-")
if err != nil {
return errors.Wrap(err, "failed to create temp dir")
}
defer os.RemoveAll(td)
dest, err := ioutil.TempDir("", "test-diff-apply-dest-")
dest, err := os.MkdirTemp("", "test-diff-apply-dest-")
if err != nil {
return errors.Wrap(err, "failed to create temp dir")
}
Expand All @@ -937,7 +936,7 @@ func testDiffApply(appliers ...fstest.Applier) error {
}
}

diffBytes, err := ioutil.ReadAll(Diff(context.Background(), dest, td))
diffBytes, err := io.ReadAll(Diff(context.Background(), dest, td))
if err != nil {
return errors.Wrap(err, "failed to create diff")
}
Expand All @@ -951,7 +950,7 @@ func testDiffApply(appliers ...fstest.Applier) error {

func makeWriterToTarTest(wt tartest.WriterToTar, a fstest.Applier, validate func(string) error, applyErr error) func(*testing.T) {
return func(t *testing.T) {
td, err := ioutil.TempDir("", "test-writer-to-tar-")
td, err := os.MkdirTemp("", "test-writer-to-tar-")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
Expand Down Expand Up @@ -1255,7 +1254,7 @@ func whiteoutEntry(name string) tarEntryValidator {

func makeDiffTarTest(validators []tarEntryValidator, a, b fstest.Applier) func(*testing.T) {
return func(t *testing.T) {
ad, err := ioutil.TempDir("", "test-make-diff-tar-")
ad, err := os.MkdirTemp("", "test-make-diff-tar-")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
Expand All @@ -1264,7 +1263,7 @@ func makeDiffTarTest(validators []tarEntryValidator, a, b fstest.Applier) func(*
t.Fatalf("failed to apply a: %v", err)
}

bd, err := ioutil.TempDir("", "test-make-diff-tar-")
bd, err := os.MkdirTemp("", "test-make-diff-tar-")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
Expand All @@ -1290,7 +1289,7 @@ func makeDiffTarTest(validators []tarEntryValidator, a, b fstest.Applier) func(*
}
var b []byte
if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 {
b, err = ioutil.ReadAll(tr)
b, err = io.ReadAll(tr)
if err != nil {
t.Fatalf("tar read file error: %v", err)
}
Expand All @@ -1308,7 +1307,7 @@ func makeDiffTarTest(validators []tarEntryValidator, a, b fstest.Applier) func(*
type diffApplier struct{}

func (d diffApplier) TestContext(ctx context.Context) (context.Context, func(), error) {
base, err := ioutil.TempDir("", "test-diff-apply-")
base, err := os.MkdirTemp("", "test-diff-apply-")
if err != nil {
return ctx, nil, errors.Wrap(err, "failed to create temp dir")
}
Expand All @@ -1320,7 +1319,7 @@ func (d diffApplier) TestContext(ctx context.Context) (context.Context, func(),
func (d diffApplier) Apply(ctx context.Context, a fstest.Applier) (string, func(), error) {
base := ctx.Value(d).(string)

applyCopy, err := ioutil.TempDir("", "test-diffapply-apply-copy-")
applyCopy, err := os.MkdirTemp("", "test-diffapply-apply-copy-")
if err != nil {
return "", nil, errors.Wrap(err, "failed to create temp dir")
}
Expand All @@ -1332,7 +1331,7 @@ func (d diffApplier) Apply(ctx context.Context, a fstest.Applier) (string, func(
return "", nil, errors.Wrap(err, "failed to apply changes to copy of base")
}

diffBytes, err := ioutil.ReadAll(Diff(ctx, base, applyCopy))
diffBytes, err := io.ReadAll(Diff(ctx, base, applyCopy))
if err != nil {
return "", nil, errors.Wrap(err, "failed to create diff")
}
Expand All @@ -1345,7 +1344,7 @@ func (d diffApplier) Apply(ctx context.Context, a fstest.Applier) (string, func(
}

func readDirNames(p string) ([]string, error) {
fis, err := ioutil.ReadDir(p)
fis, err := os.ReadDir(p)
if err != nil {
return nil, err
}
Expand Down
21 changes: 10 additions & 11 deletions cio/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
Expand All @@ -50,7 +49,7 @@ func TestNewFIFOSetInDir(t *testing.T) {
t.Skip("NewFIFOSetInDir has different behaviour on windows")
}

root, err := ioutil.TempDir("", "test-new-fifo-set")
root, err := os.MkdirTemp("", "test-new-fifo-set")
assert.NilError(t, err)
defer os.RemoveAll(root)

Expand All @@ -69,12 +68,12 @@ func TestNewFIFOSetInDir(t *testing.T) {
}
assert.Assert(t, is.DeepEqual(fifos, expected, cmpFIFOSet))

files, err := ioutil.ReadDir(root)
files, err := os.ReadDir(root)
assert.NilError(t, err)
assert.Check(t, is.Len(files, 1))

assert.NilError(t, fifos.Close())
files, err = ioutil.ReadDir(root)
files, err = os.ReadDir(root)
assert.NilError(t, err)
assert.Check(t, is.Len(files, 0))
}
Expand Down Expand Up @@ -102,19 +101,19 @@ func TestNewAttach(t *testing.T) {
fifos, err := NewFIFOSetInDir("", "theid", false)
assert.NilError(t, err)

io, err := attacher(fifos)
attachedFifos, err := attacher(fifos)
assert.NilError(t, err)
defer io.Close()
defer attachedFifos.Close()

producers := setupFIFOProducers(t, io.Config())
producers := setupFIFOProducers(t, attachedFifos.Config())
initProducers(t, producers, expectedStdout, expectedStderr)

actualStdin, err := ioutil.ReadAll(producers.Stdin)
actualStdin, err := io.ReadAll(producers.Stdin)
assert.NilError(t, err)

io.Wait()
io.Cancel()
assert.NilError(t, io.Close())
attachedFifos.Wait()
attachedFifos.Cancel()
assert.NilError(t, attachedFifos.Close())

assert.Check(t, is.Equal(expectedStdout, stdout.String()))
assert.Check(t, is.Equal(expectedStderr, stderr.String()))
Expand Down
3 changes: 1 addition & 2 deletions cio/io_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package cio
import (
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand All @@ -39,7 +38,7 @@ func NewFIFOSetInDir(root, id string, terminal bool) (*FIFOSet, error) {
return nil, err
}
}
dir, err := ioutil.TempDir(root, "")
dir, err := os.MkdirTemp(root, "")
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions cio/io_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package cio
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -66,7 +65,7 @@ func TestOpenFifosWithTerminal(t *testing.T) {
var ctx, cancel = context.WithCancel(context.Background())
defer cancel()

ioFifoDir, err := ioutil.TempDir("", fmt.Sprintf("cio-%s", t.Name()))
ioFifoDir, err := os.MkdirTemp("", fmt.Sprintf("cio-%s", t.Name()))
if err != nil {
t.Fatalf("unexpected error during creating temp dir: %v", err)
}
Expand Down
Loading

0 comments on commit 50da673

Please sign in to comment.