Skip to content

Commit

Permalink
Merge pull request #3443 from thaJeztah/remove_ioutil
Browse files Browse the repository at this point in the history
Remove uses of deprecated io/ioutil, and use new t.TempDir() and t.Cleanup() in tests
  • Loading branch information
thaJeztah authored Feb 25, 2022
2 parents df7adf4 + 8c3ae38 commit ae3b0b3
Show file tree
Hide file tree
Showing 144 changed files with 584 additions and 911 deletions.
8 changes: 8 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ linters:
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- gocyclo
- goimports
Expand Down Expand Up @@ -33,6 +34,13 @@ run:
- .*generated.*

linters-settings:
depguard:
list-type: blacklist
include-go-root: true
packages:
# The io/ioutil package has been deprecated.
# https://go.dev/doc/go1.16#ioutil
- io/ioutil
gocyclo:
min-complexity: 16
govet:
Expand Down
5 changes: 2 additions & 3 deletions cli-plugins/manager/manager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package manager

import (
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -57,12 +56,12 @@ func getPluginDirs(dockerCli command.Cli) ([]string, error) {
}

func addPluginCandidatesFromDir(res map[string][]string, d string) error {
dentries, err := ioutil.ReadDir(d)
dentries, err := os.ReadDir(d)
if err != nil {
return err
}
for _, dentry := range dentries {
switch dentry.Mode() & os.ModeType {
switch dentry.Type() & os.ModeType {
case 0, os.ModeSymlink:
// Regular file or symlink, keep going
default:
Expand Down
4 changes: 2 additions & 2 deletions cli/command/checkpoint/create_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package checkpoint

import (
"io/ioutil"
"io"
"strings"
"testing"

Expand Down Expand Up @@ -41,7 +41,7 @@ func TestCheckpointCreateErrors(t *testing.T) {
})
cmd := newCreateCommand(cli)
cmd.SetArgs(tc.args)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command/checkpoint/list_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package checkpoint

import (
"io/ioutil"
"io"
"testing"

"github.com/docker/cli/internal/test"
Expand Down Expand Up @@ -41,7 +41,7 @@ func TestCheckpointListErrors(t *testing.T) {
})
cmd := newListCommand(cli)
cmd.SetArgs(tc.args)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command/checkpoint/remove_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package checkpoint

import (
"io/ioutil"
"io"
"testing"

"github.com/docker/cli/internal/test"
Expand Down Expand Up @@ -40,7 +40,7 @@ func TestCheckpointRemoveErrors(t *testing.T) {
})
cmd := newRemoveCommand(cli)
cmd.SetArgs(tc.args)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
Expand Down
3 changes: 1 addition & 2 deletions cli/command/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package command
import (
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -275,7 +274,7 @@ func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.
store := &ContextStoreWithDefault{
Store: store.New(cliconfig.ContextStoreDir(), storeConfig),
Resolver: func() (*DefaultContext, error) {
return ResolveDefaultContext(opts, configFile, storeConfig, ioutil.Discard)
return ResolveDefaultContext(opts, configFile, storeConfig, io.Discard)
},
}
contextName, err := resolveContextName(opts, configFile, store)
Expand Down
10 changes: 5 additions & 5 deletions cli/command/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -225,23 +225,23 @@ func TestNewDockerCliAndOperators(t *testing.T) {
outbuf := bytes.NewBuffer(nil)
errbuf := bytes.NewBuffer(nil)
err = cli.Apply(
WithInputStream(ioutil.NopCloser(inbuf)),
WithInputStream(io.NopCloser(inbuf)),
WithOutputStream(outbuf),
WithErrorStream(errbuf),
)
assert.NilError(t, err)
// Check input stream
inputStream, err := ioutil.ReadAll(cli.In())
inputStream, err := io.ReadAll(cli.In())
assert.NilError(t, err)
assert.Equal(t, string(inputStream), "input")
// Check output stream
fmt.Fprintf(cli.Out(), "output")
outputStream, err := ioutil.ReadAll(outbuf)
outputStream, err := io.ReadAll(outbuf)
assert.NilError(t, err)
assert.Equal(t, string(outputStream), "output")
// Check error stream
fmt.Fprintf(cli.Err(), "error")
errStream, err := ioutil.ReadAll(errbuf)
errStream, err := io.ReadAll(errbuf)
assert.NilError(t, err)
assert.Equal(t, string(errStream), "error")
}
Expand Down
3 changes: 1 addition & 2 deletions cli/command/config/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
Expand Down Expand Up @@ -61,7 +60,7 @@ func RunConfigCreate(dockerCli command.Cli, options CreateOptions) error {
defer file.Close()
}

configData, err := ioutil.ReadAll(in)
configData, err := io.ReadAll(in)
if err != nil {
return errors.Errorf("Error reading content from %q: %v", options.File, err)
}
Expand Down
7 changes: 4 additions & 3 deletions cli/command/config/create_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package config

import (
"io/ioutil"
"io"
"os"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestConfigCreateErrors(t *testing.T) {
}),
)
cmd.SetArgs(tc.args)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestConfigCreateWithLabels(t *testing.T) {
}
name := "foo"

data, err := ioutil.ReadFile(filepath.Join("testdata", configDataFile))
data, err := os.ReadFile(filepath.Join("testdata", configDataFile))
assert.NilError(t, err)

expected := swarm.ConfigSpec{
Expand Down
4 changes: 2 additions & 2 deletions cli/command/config/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

import (
"fmt"
"io/ioutil"
"io"
"testing"
"time"

Expand Down Expand Up @@ -59,7 +59,7 @@ func TestConfigInspectErrors(t *testing.T) {
for key, value := range tc.flags {
cmd.Flags().Set(key, value)
}
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
Expand Down
4 changes: 2 additions & 2 deletions cli/command/config/ls_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"io/ioutil"
"io"
"testing"
"time"

Expand Down Expand Up @@ -40,7 +40,7 @@ func TestConfigListErrors(t *testing.T) {
}),
)
cmd.SetArgs(tc.args)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
Expand Down
6 changes: 3 additions & 3 deletions cli/command/config/remove_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"io/ioutil"
"io"
"strings"
"testing"

Expand Down Expand Up @@ -36,7 +36,7 @@ func TestConfigRemoveErrors(t *testing.T) {
}),
)
cmd.SetArgs(tc.args)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
}
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestConfigRemoveContinueAfterError(t *testing.T) {

cmd := newConfigRemoveCommand(cli)
cmd.SetArgs(names)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
assert.Error(t, cmd.Execute(), "error removing config: foo")
assert.Check(t, is.DeepEqual(names, removedConfigs))
}
4 changes: 2 additions & 2 deletions cli/command/container/attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package container

import (
"fmt"
"io/ioutil"
"io"
"testing"

"github.com/docker/cli/cli"
Expand Down Expand Up @@ -71,7 +71,7 @@ func TestNewAttachCommandErrors(t *testing.T) {
}
for _, tc := range testCases {
cmd := NewAttachCommand(test.NewFakeCli(&fakeClient{inspectFunc: tc.containerInspectFunc}))
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
cmd.SetArgs(tc.args)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
Expand Down
5 changes: 2 additions & 3 deletions cli/command/container/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package container

import (
"io"
"io/ioutil"
"os"
"runtime"
"strings"
Expand Down Expand Up @@ -54,7 +53,7 @@ func TestRunCopyFromContainerToStdout(t *testing.T) {
fakeClient := &fakeClient{
containerCopyFromFunc: func(container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
assert.Check(t, is.Equal("container", container))
return ioutil.NopCloser(strings.NewReader(tarContent)), types.ContainerPathStat{}, nil
return io.NopCloser(strings.NewReader(tarContent)), types.ContainerPathStat{}, nil
},
}
options := copyOptions{source: "container:/path", destination: "-"}
Expand Down Expand Up @@ -84,7 +83,7 @@ func TestRunCopyFromContainerToFilesystem(t *testing.T) {
assert.Check(t, is.Equal("", cli.OutBuffer().String()))
assert.Check(t, is.Equal("", cli.ErrBuffer().String()))

content, err := ioutil.ReadFile(destDir.Join("file1"))
content, err := os.ReadFile(destDir.Join("file1"))
assert.NilError(t, err)
assert.Check(t, is.Equal("content\n", string(content)))
}
Expand Down
11 changes: 5 additions & 6 deletions cli/command/container/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"runtime"
"sort"
Expand Down Expand Up @@ -67,7 +66,7 @@ func TestCIDFileCloseWithWrite(t *testing.T) {
content := "id"
assert.NilError(t, file.Write(content))

actual, err := ioutil.ReadFile(path)
actual, err := os.ReadFile(path)
assert.NilError(t, err)
assert.Check(t, is.Equal(content, string(actual)))

Expand Down Expand Up @@ -130,7 +129,7 @@ func TestCreateContainerImagePullPolicy(t *testing.T) {
},
imageCreateFunc: func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) {
defer func() { pullCounter++ }()
return ioutil.NopCloser(strings.NewReader("")), nil
return io.NopCloser(strings.NewReader("")), nil
},
infoFunc: func() (types.Info, error) {
return types.Info{IndexServerAddress: "https://indexserver.example.com"}, nil
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestNewCreateCommandWithContentTrustErrors(t *testing.T) {
}, test.EnableContentTrust)
cli.SetNotaryClient(tc.notaryFunc)
cmd := NewCreateCommand(cli)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
cmd.SetArgs(tc.args)
err := cmd.Execute()
assert.ErrorContains(t, err, tc.expectedError)
Expand Down Expand Up @@ -254,7 +253,7 @@ func TestNewCreateCommandWithWarnings(t *testing.T) {
},
})
cmd := NewCreateCommand(cli)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
cmd.SetArgs(tc.args)
err := cmd.Execute()
assert.NilError(t, err)
Expand Down Expand Up @@ -306,7 +305,7 @@ func TestCreateContainerWithProxyConfig(t *testing.T) {
},
})
cmd := NewCreateCommand(cli)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
cmd.SetArgs([]string{"image:tag"})
err := cmd.Execute()
assert.NilError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions cli/command/container/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package container

import (
"context"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -267,7 +267,7 @@ func TestNewExecCommandErrors(t *testing.T) {
for _, tc := range testCases {
cli := test.NewFakeCli(&fakeClient{inspectFunc: tc.containerInspectFunc})
cmd := NewExecCommand(cli)
cmd.SetOut(ioutil.Discard)
cmd.SetOut(io.Discard)
cmd.SetArgs(tc.args)
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
}
Expand Down
Loading

0 comments on commit ae3b0b3

Please sign in to comment.