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 in Go 1.16. 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]>
  • Loading branch information
Juneezee committed Aug 27, 2021
1 parent 2b70006 commit c55a4ac
Show file tree
Hide file tree
Showing 397 changed files with 1,371 additions and 1,606 deletions.
3 changes: 1 addition & 2 deletions builder/builder-next/adapters/containerimage/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"path"
"runtime"
"sync"
Expand Down Expand Up @@ -640,7 +639,7 @@ func (ld *layerDescriptor) Download(ctx context.Context, progressOutput pkgprogr
return nil, 0, err
}

return ioutil.NopCloser(content.NewReader(ra)), ld.desc.Size, nil
return io.NopCloser(content.NewReader(ra)), ld.desc.Size, nil
}

func (ld *layerDescriptor) Close() {
Expand Down
3 changes: 1 addition & 2 deletions builder/builder-next/executor_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package buildkit

import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -35,7 +34,7 @@ func newExecutor(root, cgroupParent string, net libnetwork.NetworkController, dn
}

// make sure net state directory is cleared from previous state
fis, err := ioutil.ReadDir(netRoot)
fis, err := os.ReadDir(netRoot)
if err == nil {
for _, fi := range fis {
fp := filepath.Join(netRoot, fi.Name())
Expand Down
3 changes: 1 addition & 2 deletions builder/builder-next/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
nethttp "net/http"
"runtime"
"strings"
Expand Down Expand Up @@ -445,7 +444,7 @@ func (ld *layerDescriptor) Download(ctx context.Context, progressOutput pkgprogr
return nil, 0, err
}

return ioutil.NopCloser(content.NewReader(ra)), ld.desc.Size, nil
return io.NopCloser(content.NewReader(ra)), ld.desc.Size, nil
}

func (ld *layerDescriptor) Close() {
Expand Down
5 changes: 2 additions & 3 deletions builder/dockerfile/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"sort"
"strings"

Expand Down Expand Up @@ -346,8 +345,8 @@ func BuildFromConfig(config *container.Config, changes []string, os string) (*co
}
}

b.Stdout = ioutil.Discard
b.Stderr = ioutil.Discard
b.Stdout = io.Discard
b.Stderr = io.Discard
b.disableCommit = true

var commands []instructions.Command
Expand Down
5 changes: 2 additions & 3 deletions builder/dockerfile/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dockerfile // import "github.com/docker/docker/builder/dockerfile"

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -11,7 +10,7 @@ import (
// It returns the created path and a cleanup function which is meant to be used as deferred call.
// When an error occurs, it terminates the test.
func createTestTempDir(t *testing.T, dir, prefix string) (string, func()) {
path, err := ioutil.TempDir(dir, prefix)
path, err := os.MkdirTemp(dir, prefix)

if err != nil {
t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err)
Expand All @@ -30,7 +29,7 @@ func createTestTempDir(t *testing.T, dir, prefix string) (string, func()) {
// When an error occurs, it terminates the test
func createTestTempFile(t *testing.T, dir, filename, contents string, perm os.FileMode) string {
filePath := filepath.Join(dir, filename)
err := ioutil.WriteFile(filePath, []byte(contents), perm)
err := os.WriteFile(filePath, []byte(contents), perm)

if err != nil {
t.Fatalf("Error when creating %s file: %s", filename, err)
Expand Down
5 changes: 2 additions & 3 deletions builder/remotecontext/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package remotecontext // import "github.com/docker/docker/builder/remotecontext"

import (
"errors"
"io/ioutil"
"log"
"os"
"sort"
Expand All @@ -20,7 +19,7 @@ const (

const shouldStayFilename = "should_stay"

func extractFilenames(files []os.FileInfo) []string {
func extractFilenames(files []os.DirEntry) []string {
filenames := make([]string, len(files))

for i, file := range files {
Expand All @@ -31,7 +30,7 @@ func extractFilenames(files []os.FileInfo) []string {
}

func checkDirectory(t *testing.T, dir string, expectedFiles []string) {
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)

if err != nil {
t.Fatalf("Could not read directory: %s", err)
Expand Down
3 changes: 1 addition & 2 deletions builder/remotecontext/git/gitutils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package git // import "github.com/docker/docker/builder/remotecontext/git"

import (
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -34,7 +33,7 @@ func Clone(remoteURL string) (string, error) {
func cloneGitRepo(repo gitRepo) (checkoutDir string, err error) {
fetch := fetchArgs(repo.remote, repo.ref)

root, err := ioutil.TempDir("", "docker-build-git")
root, err := os.MkdirTemp("", "docker-build-git")
if err != nil {
return "", err
}
Expand Down
17 changes: 8 additions & 9 deletions builder/remotecontext/git/gitutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package git // import "github.com/docker/docker/builder/remotecontext/git"

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -171,7 +170,7 @@ func gitGetConfig(name string) string {
}

func TestCheckoutGit(t *testing.T) {
root, err := ioutil.TempDir("", "docker-build-git-checkout")
root, err := os.MkdirTemp("", "docker-build-git-checkout")
assert.NilError(t, err)
defer os.RemoveAll(root)

Expand All @@ -195,13 +194,13 @@ func TestCheckoutGit(t *testing.T) {
_, err = gitWithinDir(gitDir, "config", "user.name", "Docker test")
assert.NilError(t, err)

err = ioutil.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644)
err = os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch"), 0644)
assert.NilError(t, err)

subDir := filepath.Join(gitDir, "subdir")
assert.NilError(t, os.Mkdir(subDir, 0755))

err = ioutil.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 5000"), 0644)
err = os.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 5000"), 0644)
assert.NilError(t, err)

if runtime.GOOS != "windows" {
Expand All @@ -223,10 +222,10 @@ func TestCheckoutGit(t *testing.T) {
_, err = gitWithinDir(gitDir, "checkout", "-b", "test")
assert.NilError(t, err)

err = ioutil.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 3000"), 0644)
err = os.WriteFile(filepath.Join(gitDir, "Dockerfile"), []byte("FROM scratch\nEXPOSE 3000"), 0644)
assert.NilError(t, err)

err = ioutil.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM busybox\nEXPOSE 5000"), 0644)
err = os.WriteFile(filepath.Join(subDir, "Dockerfile"), []byte("FROM busybox\nEXPOSE 5000"), 0644)
assert.NilError(t, err)

_, err = gitWithinDir(gitDir, "add", "-A")
Expand All @@ -249,7 +248,7 @@ func TestCheckoutGit(t *testing.T) {
_, err = gitWithinDir(subrepoDir, "config", "user.name", "Docker test")
assert.NilError(t, err)

err = ioutil.WriteFile(filepath.Join(subrepoDir, "subfile"), []byte("subcontents"), 0644)
err = os.WriteFile(filepath.Join(subrepoDir, "subfile"), []byte("subcontents"), 0644)
assert.NilError(t, err)

_, err = gitWithinDir(subrepoDir, "add", "-A")
Expand Down Expand Up @@ -310,7 +309,7 @@ func TestCheckoutGit(t *testing.T) {
assert.NilError(t, err)
defer os.RemoveAll(r)
if c.submodule {
b, err := ioutil.ReadFile(filepath.Join(r, "sub/subfile"))
b, err := os.ReadFile(filepath.Join(r, "sub/subfile"))
assert.NilError(t, err)
assert.Check(t, is.Equal("subcontents", string(b)))
} else {
Expand All @@ -319,7 +318,7 @@ func TestCheckoutGit(t *testing.T) {
assert.Assert(t, os.IsNotExist(err))
}

b, err := ioutil.ReadFile(filepath.Join(r, "Dockerfile"))
b, err := os.ReadFile(filepath.Join(r, "Dockerfile"))
assert.NilError(t, err)
assert.Check(t, is.Equal(c.exp, string(b)))
}
Expand Down
3 changes: 1 addition & 2 deletions builder/remotecontext/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -58,7 +57,7 @@ func GetWithStatusError(address string) (resp *http.Response, err error) {
return resp, nil
}
msg := fmt.Sprintf("failed to GET %s with status %s", address, resp.Status)
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, errdefs.System(errors.New(msg + ": error reading body"))
Expand Down
29 changes: 14 additions & 15 deletions builder/remotecontext/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package remotecontext // import "github.com/docker/docker/builder/remotecontext"
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -52,15 +51,15 @@ func TestSelectAcceptableMIME(t *testing.T) {

func TestInspectEmptyResponse(t *testing.T) {
ct := "application/octet-stream"
br := ioutil.NopCloser(bytes.NewReader([]byte("")))
br := io.NopCloser(bytes.NewReader([]byte("")))
contentType, bReader, err := inspectResponse(ct, br, 0)
if err == nil {
t.Fatal("Should have generated an error for an empty response")
}
if contentType != "application/octet-stream" {
t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType)
}
body, err := ioutil.ReadAll(bReader)
body, err := io.ReadAll(bReader)
if err != nil {
t.Fatal(err)
}
Expand All @@ -71,15 +70,15 @@ func TestInspectEmptyResponse(t *testing.T) {

func TestInspectResponseBinary(t *testing.T) {
ct := "application/octet-stream"
br := ioutil.NopCloser(bytes.NewReader(binaryContext))
br := io.NopCloser(bytes.NewReader(binaryContext))
contentType, bReader, err := inspectResponse(ct, br, int64(len(binaryContext)))
if err != nil {
t.Fatal(err)
}
if contentType != "application/octet-stream" {
t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType)
}
body, err := ioutil.ReadAll(bReader)
body, err := io.ReadAll(bReader)
if err != nil {
t.Fatal(err)
}
Expand All @@ -96,7 +95,7 @@ func TestInspectResponseBinary(t *testing.T) {
func TestResponseUnsupportedContentType(t *testing.T) {
content := []byte(dockerfileContents)
ct := "application/json"
br := ioutil.NopCloser(bytes.NewReader(content))
br := io.NopCloser(bytes.NewReader(content))
contentType, bReader, err := inspectResponse(ct, br, int64(len(dockerfileContents)))

if err == nil {
Expand All @@ -105,7 +104,7 @@ func TestResponseUnsupportedContentType(t *testing.T) {
if contentType != ct {
t.Fatalf("Should not have altered content-type: orig: %s, altered: %s", ct, contentType)
}
body, err := ioutil.ReadAll(bReader)
body, err := io.ReadAll(bReader)
if err != nil {
t.Fatal(err)
}
Expand All @@ -117,15 +116,15 @@ func TestResponseUnsupportedContentType(t *testing.T) {
func TestInspectResponseTextSimple(t *testing.T) {
content := []byte(dockerfileContents)
ct := "text/plain"
br := ioutil.NopCloser(bytes.NewReader(content))
br := io.NopCloser(bytes.NewReader(content))
contentType, bReader, err := inspectResponse(ct, br, int64(len(content)))
if err != nil {
t.Fatal(err)
}
if contentType != "text/plain" {
t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
}
body, err := ioutil.ReadAll(bReader)
body, err := io.ReadAll(bReader)
if err != nil {
t.Fatal(err)
}
Expand All @@ -136,15 +135,15 @@ func TestInspectResponseTextSimple(t *testing.T) {

func TestInspectResponseEmptyContentType(t *testing.T) {
content := []byte(dockerfileContents)
br := ioutil.NopCloser(bytes.NewReader(content))
br := io.NopCloser(bytes.NewReader(content))
contentType, bodyReader, err := inspectResponse("", br, int64(len(content)))
if err != nil {
t.Fatal(err)
}
if contentType != "text/plain" {
t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
}
body, err := ioutil.ReadAll(bodyReader)
body, err := io.ReadAll(bodyReader)
if err != nil {
t.Fatal(err)
}
Expand All @@ -156,15 +155,15 @@ func TestInspectResponseEmptyContentType(t *testing.T) {
func TestUnknownContentLength(t *testing.T) {
content := []byte(dockerfileContents)
ct := "text/plain"
br := ioutil.NopCloser(bytes.NewReader(content))
br := io.NopCloser(bytes.NewReader(content))
contentType, bReader, err := inspectResponse(ct, br, -1)
if err != nil {
t.Fatal(err)
}
if contentType != "text/plain" {
t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
}
body, err := ioutil.ReadAll(bReader)
body, err := io.ReadAll(bReader)
if err != nil {
t.Fatal(err)
}
Expand All @@ -191,7 +190,7 @@ func TestDownloadRemote(t *testing.T) {
assert.NilError(t, err)

assert.Check(t, is.Equal(mimeTypes.TextPlain, contentType))
raw, err := ioutil.ReadAll(content)
raw, err := io.ReadAll(content)
assert.NilError(t, err)
assert.Check(t, is.Equal(dockerfileContents, string(raw)))
}
Expand Down Expand Up @@ -238,5 +237,5 @@ func TestGetWithStatusError(t *testing.T) {

func readBody(b io.ReadCloser) ([]byte, error) {
defer b.Close()
return ioutil.ReadAll(b)
return io.ReadAll(b)
}
3 changes: 1 addition & 2 deletions builder/remotecontext/tarsum_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package remotecontext // import "github.com/docker/docker/builder/remotecontext"

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -23,7 +22,7 @@ func init() {
}

func TestCloseRootDirectory(t *testing.T) {
contextDir, err := ioutil.TempDir("", "builder-tarsum-test")
contextDir, err := os.MkdirTemp("", "builder-tarsum-test")
defer os.RemoveAll(contextDir)
if err != nil {
t.Fatalf("Error with creating temporary directory: %s", err)
Expand Down
Loading

0 comments on commit c55a4ac

Please sign in to comment.