Skip to content

Commit 50da673

Browse files
committed
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 <[email protected]>
1 parent c16be1a commit 50da673

126 files changed

Lines changed: 291 additions & 399 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

archive/compression/compression_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"compress/gzip"
2222
"context"
2323
"io"
24-
"io/ioutil"
2524
"math/rand"
2625
"os"
2726
"path/filepath"
@@ -80,7 +79,7 @@ func testCompressDecompress(t *testing.T, size int, compression Compression) Dec
8079
if err != nil {
8180
t.Fatal(err)
8281
}
83-
decompressed, err := ioutil.ReadAll(decompressor)
82+
decompressed, err := io.ReadAll(decompressor)
8483
if err != nil {
8584
t.Fatal(err)
8685
}
@@ -123,7 +122,7 @@ func TestCompressDecompressUncompressed(t *testing.T) {
123122

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

136135
fullPath := filepath.Join(tempPath, filename)
137136

138-
if err := ioutil.WriteFile(fullPath, []byte(""), 0111); err != nil {
137+
if err := os.WriteFile(fullPath, []byte(""), 0111); err != nil {
139138
t.Fatal(err)
140139
}
141140

@@ -165,7 +164,7 @@ func TestCmdStream(t *testing.T) {
165164
t.Fatal(err)
166165
}
167166

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

184-
if buf, err := ioutil.ReadAll(out); err == nil {
183+
if buf, err := io.ReadAll(out); err == nil {
185184
t.Fatal("command should have failed")
186185
} else if err.Error() != "exit status 1: bad result\n" {
187186
t.Fatalf("wrong error: %s", err.Error())

archive/issues_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package archive
1919
import (
2020
"bytes"
2121
"context"
22-
"io/ioutil"
2322
"os"
2423
"path/filepath"
2524
"strings"
@@ -37,7 +36,7 @@ func TestPrefixHeaderReadable(t *testing.T) {
3736
// https://gist.github.com/stevvooe/e2a790ad4e97425896206c0816e1a882#file-out-go
3837
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")
3938

40-
tmpDir, err := ioutil.TempDir("", "prefix-test")
39+
tmpDir, err := os.MkdirTemp("", "prefix-test")
4140
if err != nil {
4241
t.Fatal(err)
4342
}

archive/tar_linux_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"context"
2222
"fmt"
2323
"io"
24-
"io/ioutil"
2524
"os"
2625
"strings"
2726
"testing"
@@ -38,7 +37,7 @@ import (
3837
func TestOverlayApply(t *testing.T) {
3938
testutil.RequiresRoot(t)
4039

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

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

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

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

152-
next, err := ioutil.TempDir(d.tmp, "lower-")
151+
next, err := os.MkdirTemp(d.tmp, "lower-")
153152
if err != nil {
154153
return "", nil, errors.Wrap(err, "failed to create temp dir")
155154
}

archive/tar_test.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"context"
2626
"fmt"
2727
"io"
28-
"io/ioutil"
2928
"os"
3029
"path/filepath"
3130
"testing"
@@ -234,7 +233,7 @@ func TestBreakouts(t *testing.T) {
234233
tc := tartest.TarContext{}.WithUIDGID(os.Getuid(), os.Getgid()).WithModTime(time.Now().UTC())
235234
expected := "unbroken"
236235
unbrokenCheck := func(root string) error {
237-
b, err := ioutil.ReadFile(filepath.Join(root, "etc", "unbroken"))
236+
b, err := os.ReadFile(filepath.Join(root, "etc", "unbroken"))
238237
if err != nil {
239238
return errors.Wrap(err, "failed to read unbroken")
240239
}
@@ -244,7 +243,7 @@ func TestBreakouts(t *testing.T) {
244243
return nil
245244
}
246245
errFileDiff := errors.New("files differ")
247-
td, err := ioutil.TempDir("", "test-breakouts-")
246+
td, err := os.MkdirTemp("", "test-breakouts-")
248247
if err != nil {
249248
t.Fatal(err)
250249
}
@@ -331,7 +330,7 @@ func TestBreakouts(t *testing.T) {
331330
}
332331
fileValue := func(f1 string, content []byte) func(string) error {
333332
return func(root string) error {
334-
b, err := ioutil.ReadFile(filepath.Join(root, f1))
333+
b, err := os.ReadFile(filepath.Join(root, f1))
335334
if err != nil {
336335
return err
337336
}
@@ -421,7 +420,7 @@ func TestBreakouts(t *testing.T) {
421420
tc.File("/localetc/emptied", []byte{}, 0644),
422421
),
423422
validator: func(root string) error {
424-
b, err := ioutil.ReadFile(filepath.Join(root, "etc", "emptied"))
423+
b, err := os.ReadFile(filepath.Join(root, "etc", "emptied"))
425424
if err != nil {
426425
return errors.Wrap(err, "failed to read unbroken")
427426
}
@@ -755,11 +754,11 @@ func TestBreakouts(t *testing.T) {
755754
name: "HardlinkSymlinkChmod",
756755
w: func() tartest.WriterToTar {
757756
p := filepath.Join(td, "perm400")
758-
if err := ioutil.WriteFile(p, []byte("..."), 0400); err != nil {
757+
if err := os.WriteFile(p, []byte("..."), 0400); err != nil {
759758
t.Fatal(err)
760759
}
761760
ep := filepath.Join(td, "also-exists-outside-root")
762-
if err := ioutil.WriteFile(ep, []byte("..."), 0640); err != nil {
761+
if err := os.WriteFile(ep, []byte("..."), 0640); err != nil {
763762
t.Fatal(err)
764763
}
765764

@@ -843,12 +842,12 @@ func TestApplyTar(t *testing.T) {
843842
}
844843

845844
func testApply(a fstest.Applier) error {
846-
td, err := ioutil.TempDir("", "test-apply-")
845+
td, err := os.MkdirTemp("", "test-apply-")
847846
if err != nil {
848847
return errors.Wrap(err, "failed to create temp dir")
849848
}
850849
defer os.RemoveAll(td)
851-
dest, err := ioutil.TempDir("", "test-apply-dest-")
850+
dest, err := os.MkdirTemp("", "test-apply-dest-")
852851
if err != nil {
853852
return errors.Wrap(err, "failed to create temp dir")
854853
}
@@ -884,12 +883,12 @@ func testApply(a fstest.Applier) error {
884883
}
885884

886885
func testBaseDiff(a fstest.Applier) error {
887-
td, err := ioutil.TempDir("", "test-base-diff-")
886+
td, err := os.MkdirTemp("", "test-base-diff-")
888887
if err != nil {
889888
return errors.Wrap(err, "failed to create temp dir")
890889
}
891890
defer os.RemoveAll(td)
892-
dest, err := ioutil.TempDir("", "test-base-diff-dest-")
891+
dest, err := os.MkdirTemp("", "test-base-diff-dest-")
893892
if err != nil {
894893
return errors.Wrap(err, "failed to create temp dir")
895894
}
@@ -911,12 +910,12 @@ func testBaseDiff(a fstest.Applier) error {
911910
}
912911

913912
func testDiffApply(appliers ...fstest.Applier) error {
914-
td, err := ioutil.TempDir("", "test-diff-apply-")
913+
td, err := os.MkdirTemp("", "test-diff-apply-")
915914
if err != nil {
916915
return errors.Wrap(err, "failed to create temp dir")
917916
}
918917
defer os.RemoveAll(td)
919-
dest, err := ioutil.TempDir("", "test-diff-apply-dest-")
918+
dest, err := os.MkdirTemp("", "test-diff-apply-dest-")
920919
if err != nil {
921920
return errors.Wrap(err, "failed to create temp dir")
922921
}
@@ -937,7 +936,7 @@ func testDiffApply(appliers ...fstest.Applier) error {
937936
}
938937
}
939938

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

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

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

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

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

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

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

13471346
func readDirNames(p string) ([]string, error) {
1348-
fis, err := ioutil.ReadDir(p)
1347+
fis, err := os.ReadDir(p)
13491348
if err != nil {
13501349
return nil, err
13511350
}

cio/io_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"bytes"
2424
"context"
2525
"io"
26-
"io/ioutil"
2726
"net/url"
2827
"os"
2928
"path/filepath"
@@ -50,7 +49,7 @@ func TestNewFIFOSetInDir(t *testing.T) {
5049
t.Skip("NewFIFOSetInDir has different behaviour on windows")
5150
}
5251

53-
root, err := ioutil.TempDir("", "test-new-fifo-set")
52+
root, err := os.MkdirTemp("", "test-new-fifo-set")
5453
assert.NilError(t, err)
5554
defer os.RemoveAll(root)
5655

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

72-
files, err := ioutil.ReadDir(root)
71+
files, err := os.ReadDir(root)
7372
assert.NilError(t, err)
7473
assert.Check(t, is.Len(files, 1))
7574

7675
assert.NilError(t, fifos.Close())
77-
files, err = ioutil.ReadDir(root)
76+
files, err = os.ReadDir(root)
7877
assert.NilError(t, err)
7978
assert.Check(t, is.Len(files, 0))
8079
}
@@ -102,19 +101,19 @@ func TestNewAttach(t *testing.T) {
102101
fifos, err := NewFIFOSetInDir("", "theid", false)
103102
assert.NilError(t, err)
104103

105-
io, err := attacher(fifos)
104+
attachedFifos, err := attacher(fifos)
106105
assert.NilError(t, err)
107-
defer io.Close()
106+
defer attachedFifos.Close()
108107

109-
producers := setupFIFOProducers(t, io.Config())
108+
producers := setupFIFOProducers(t, attachedFifos.Config())
110109
initProducers(t, producers, expectedStdout, expectedStderr)
111110

112-
actualStdin, err := ioutil.ReadAll(producers.Stdin)
111+
actualStdin, err := io.ReadAll(producers.Stdin)
113112
assert.NilError(t, err)
114113

115-
io.Wait()
116-
io.Cancel()
117-
assert.NilError(t, io.Close())
114+
attachedFifos.Wait()
115+
attachedFifos.Cancel()
116+
assert.NilError(t, attachedFifos.Close())
118117

119118
assert.Check(t, is.Equal(expectedStdout, stdout.String()))
120119
assert.Check(t, is.Equal(expectedStderr, stderr.String()))

cio/io_unix.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package cio
2222
import (
2323
"context"
2424
"io"
25-
"io/ioutil"
2625
"os"
2726
"path/filepath"
2827
"sync"
@@ -39,7 +38,7 @@ func NewFIFOSetInDir(root, id string, terminal bool) (*FIFOSet, error) {
3938
return nil, err
4039
}
4140
}
42-
dir, err := ioutil.TempDir(root, "")
41+
dir, err := os.MkdirTemp(root, "")
4342
if err != nil {
4443
return nil, err
4544
}

cio/io_unix_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package cio
2222
import (
2323
"context"
2424
"fmt"
25-
"io/ioutil"
2625
"os"
2726
"path/filepath"
2827
"testing"
@@ -66,7 +65,7 @@ func TestOpenFifosWithTerminal(t *testing.T) {
6665
var ctx, cancel = context.WithCancel(context.Background())
6766
defer cancel()
6867

69-
ioFifoDir, err := ioutil.TempDir("", fmt.Sprintf("cio-%s", t.Name()))
68+
ioFifoDir, err := os.MkdirTemp("", fmt.Sprintf("cio-%s", t.Name()))
7069
if err != nil {
7170
t.Fatalf("unexpected error during creating temp dir: %v", err)
7271
}

0 commit comments

Comments
 (0)