refactor: move from io/ioutil to io and os package
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>
This commit is contained in:
@@ -21,7 +21,6 @@ import (
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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())
|
||||
|
||||
@@ -19,7 +19,6 @@ package archive
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user