Skip to content

Commit ae3b0b3

Browse files
authored
Merge pull request #3443 from thaJeztah/remove_ioutil
Remove uses of deprecated io/ioutil, and use new t.TempDir() and t.Cleanup() in tests
2 parents df7adf4 + 8c3ae38 commit ae3b0b3

144 files changed

Lines changed: 584 additions & 911 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.

.golangci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ linters:
22
enable:
33
- bodyclose
44
- deadcode
5+
- depguard
56
- dogsled
67
- gocyclo
78
- goimports
@@ -33,6 +34,13 @@ run:
3334
- .*generated.*
3435

3536
linters-settings:
37+
depguard:
38+
list-type: blacklist
39+
include-go-root: true
40+
packages:
41+
# The io/ioutil package has been deprecated.
42+
# https://go.dev/doc/go1.16#ioutil
43+
- io/ioutil
3644
gocyclo:
3745
min-complexity: 16
3846
govet:

cli-plugins/manager/manager.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package manager
22

33
import (
4-
"io/ioutil"
54
"os"
65
"path/filepath"
76
"sort"
@@ -57,12 +56,12 @@ func getPluginDirs(dockerCli command.Cli) ([]string, error) {
5756
}
5857

5958
func addPluginCandidatesFromDir(res map[string][]string, d string) error {
60-
dentries, err := ioutil.ReadDir(d)
59+
dentries, err := os.ReadDir(d)
6160
if err != nil {
6261
return err
6362
}
6463
for _, dentry := range dentries {
65-
switch dentry.Mode() & os.ModeType {
64+
switch dentry.Type() & os.ModeType {
6665
case 0, os.ModeSymlink:
6766
// Regular file or symlink, keep going
6867
default:

cli/command/checkpoint/create_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package checkpoint
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"strings"
66
"testing"
77

@@ -41,7 +41,7 @@ func TestCheckpointCreateErrors(t *testing.T) {
4141
})
4242
cmd := newCreateCommand(cli)
4343
cmd.SetArgs(tc.args)
44-
cmd.SetOut(ioutil.Discard)
44+
cmd.SetOut(io.Discard)
4545
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
4646
}
4747
}

cli/command/checkpoint/list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package checkpoint
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"testing"
66

77
"github.com/docker/cli/internal/test"
@@ -41,7 +41,7 @@ func TestCheckpointListErrors(t *testing.T) {
4141
})
4242
cmd := newListCommand(cli)
4343
cmd.SetArgs(tc.args)
44-
cmd.SetOut(ioutil.Discard)
44+
cmd.SetOut(io.Discard)
4545
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
4646
}
4747
}

cli/command/checkpoint/remove_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package checkpoint
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"testing"
66

77
"github.com/docker/cli/internal/test"
@@ -40,7 +40,7 @@ func TestCheckpointRemoveErrors(t *testing.T) {
4040
})
4141
cmd := newRemoveCommand(cli)
4242
cmd.SetArgs(tc.args)
43-
cmd.SetOut(ioutil.Discard)
43+
cmd.SetOut(io.Discard)
4444
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
4545
}
4646
}

cli/command/cli.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package command
33
import (
44
"context"
55
"io"
6-
"io/ioutil"
76
"os"
87
"path/filepath"
98
"runtime"
@@ -275,7 +274,7 @@ func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.
275274
store := &ContextStoreWithDefault{
276275
Store: store.New(cliconfig.ContextStoreDir(), storeConfig),
277276
Resolver: func() (*DefaultContext, error) {
278-
return ResolveDefaultContext(opts, configFile, storeConfig, ioutil.Discard)
277+
return ResolveDefaultContext(opts, configFile, storeConfig, io.Discard)
279278
},
280279
}
281280
contextName, err := resolveContextName(opts, configFile, store)

cli/command/cli_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"net/http"
99
"net/http/httptest"
1010
"os"
@@ -225,23 +225,23 @@ func TestNewDockerCliAndOperators(t *testing.T) {
225225
outbuf := bytes.NewBuffer(nil)
226226
errbuf := bytes.NewBuffer(nil)
227227
err = cli.Apply(
228-
WithInputStream(ioutil.NopCloser(inbuf)),
228+
WithInputStream(io.NopCloser(inbuf)),
229229
WithOutputStream(outbuf),
230230
WithErrorStream(errbuf),
231231
)
232232
assert.NilError(t, err)
233233
// Check input stream
234-
inputStream, err := ioutil.ReadAll(cli.In())
234+
inputStream, err := io.ReadAll(cli.In())
235235
assert.NilError(t, err)
236236
assert.Equal(t, string(inputStream), "input")
237237
// Check output stream
238238
fmt.Fprintf(cli.Out(), "output")
239-
outputStream, err := ioutil.ReadAll(outbuf)
239+
outputStream, err := io.ReadAll(outbuf)
240240
assert.NilError(t, err)
241241
assert.Equal(t, string(outputStream), "output")
242242
// Check error stream
243243
fmt.Fprintf(cli.Err(), "error")
244-
errStream, err := ioutil.ReadAll(errbuf)
244+
errStream, err := io.ReadAll(errbuf)
245245
assert.NilError(t, err)
246246
assert.Equal(t, string(errStream), "error")
247247
}

cli/command/config/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
"io/ioutil"
87

98
"github.com/docker/cli/cli"
109
"github.com/docker/cli/cli/command"
@@ -61,7 +60,7 @@ func RunConfigCreate(dockerCli command.Cli, options CreateOptions) error {
6160
defer file.Close()
6261
}
6362

64-
configData, err := ioutil.ReadAll(in)
63+
configData, err := io.ReadAll(in)
6564
if err != nil {
6665
return errors.Errorf("Error reading content from %q: %v", options.File, err)
6766
}

cli/command/config/create_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package config
22

33
import (
4-
"io/ioutil"
4+
"io"
5+
"os"
56
"path/filepath"
67
"reflect"
78
"strings"
@@ -46,7 +47,7 @@ func TestConfigCreateErrors(t *testing.T) {
4647
}),
4748
)
4849
cmd.SetArgs(tc.args)
49-
cmd.SetOut(ioutil.Discard)
50+
cmd.SetOut(io.Discard)
5051
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
5152
}
5253
}
@@ -82,7 +83,7 @@ func TestConfigCreateWithLabels(t *testing.T) {
8283
}
8384
name := "foo"
8485

85-
data, err := ioutil.ReadFile(filepath.Join("testdata", configDataFile))
86+
data, err := os.ReadFile(filepath.Join("testdata", configDataFile))
8687
assert.NilError(t, err)
8788

8889
expected := swarm.ConfigSpec{

cli/command/config/inspect_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package config
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"testing"
77
"time"
88

@@ -59,7 +59,7 @@ func TestConfigInspectErrors(t *testing.T) {
5959
for key, value := range tc.flags {
6060
cmd.Flags().Set(key, value)
6161
}
62-
cmd.SetOut(ioutil.Discard)
62+
cmd.SetOut(io.Discard)
6363
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
6464
}
6565
}

0 commit comments

Comments
 (0)