cli/command: remove deprecated io/ioutil and use t.TempDir()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-02-25 14:35:28 +01:00
parent cca73bff41
commit 3f7e7bf9d2
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 20 additions and 37 deletions

View File

@ -3,7 +3,6 @@ package command
import ( import (
"context" "context"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -275,7 +274,7 @@ func NewAPIClientFromFlags(opts *cliflags.CommonOptions, configFile *configfile.
store := &ContextStoreWithDefault{ store := &ContextStoreWithDefault{
Store: store.New(cliconfig.ContextStoreDir(), storeConfig), Store: store.New(cliconfig.ContextStoreDir(), storeConfig),
Resolver: func() (*DefaultContext, error) { Resolver: func() (*DefaultContext, error) {
return ResolveDefaultContext(opts, configFile, storeConfig, ioutil.Discard) return ResolveDefaultContext(opts, configFile, storeConfig, io.Discard)
}, },
} }
contextName, err := resolveContextName(opts, configFile, store) contextName, err := resolveContextName(opts, configFile, store)

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@ -225,23 +225,23 @@ func TestNewDockerCliAndOperators(t *testing.T) {
outbuf := bytes.NewBuffer(nil) outbuf := bytes.NewBuffer(nil)
errbuf := bytes.NewBuffer(nil) errbuf := bytes.NewBuffer(nil)
err = cli.Apply( err = cli.Apply(
WithInputStream(ioutil.NopCloser(inbuf)), WithInputStream(io.NopCloser(inbuf)),
WithOutputStream(outbuf), WithOutputStream(outbuf),
WithErrorStream(errbuf), WithErrorStream(errbuf),
) )
assert.NilError(t, err) assert.NilError(t, err)
// Check input stream // Check input stream
inputStream, err := ioutil.ReadAll(cli.In()) inputStream, err := io.ReadAll(cli.In())
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, string(inputStream), "input") assert.Equal(t, string(inputStream), "input")
// Check output stream // Check output stream
fmt.Fprintf(cli.Out(), "output") fmt.Fprintf(cli.Out(), "output")
outputStream, err := ioutil.ReadAll(outbuf) outputStream, err := io.ReadAll(outbuf)
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, string(outputStream), "output") assert.Equal(t, string(outputStream), "output")
// Check error stream // Check error stream
fmt.Fprintf(cli.Err(), "error") fmt.Fprintf(cli.Err(), "error")
errStream, err := ioutil.ReadAll(errbuf) errStream, err := io.ReadAll(errbuf)
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, string(errStream), "error") assert.Equal(t, string(errStream), "error")
} }

View File

@ -2,8 +2,6 @@ package command
import ( import (
"crypto/rand" "crypto/rand"
"io/ioutil"
"os"
"testing" "testing"
"github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/config/configfile"
@ -39,11 +37,10 @@ func testDefaultMetadata() store.Metadata {
} }
} }
func testStore(t *testing.T, meta store.Metadata, tls store.ContextTLSData) (store.Store, func()) { func testStore(t *testing.T, meta store.Metadata, tls store.ContextTLSData) store.Store {
testDir, err := ioutil.TempDir("", t.Name()) t.Helper()
assert.NilError(t, err) return &ContextStoreWithDefault{
s := &ContextStoreWithDefault{ Store: store.New(t.TempDir(), testCfg),
Store: store.New(testDir, testCfg),
Resolver: func() (*DefaultContext, error) { Resolver: func() (*DefaultContext, error) {
return &DefaultContext{ return &DefaultContext{
Meta: meta, Meta: meta,
@ -51,9 +48,6 @@ func testStore(t *testing.T, meta store.Metadata, tls store.ContextTLSData) (sto
}, nil }, nil
}, },
} }
return s, func() {
_ = os.RemoveAll(testDir)
}
} }
func TestDefaultContextInitializer(t *testing.T) { func TestDefaultContextInitializer(t *testing.T) {
@ -78,7 +72,7 @@ func TestExportDefaultImport(t *testing.T) {
rand.Read(file1) rand.Read(file1)
file2 := make([]byte, 3700) file2 := make([]byte, 3700)
rand.Read(file2) rand.Read(file2)
s, cleanup := testStore(t, testDefaultMetadata(), store.ContextTLSData{ s := testStore(t, testDefaultMetadata(), store.ContextTLSData{
Endpoints: map[string]store.EndpointTLSData{ Endpoints: map[string]store.EndpointTLSData{
"ep2": { "ep2": {
Files: map[string][]byte{ Files: map[string][]byte{
@ -88,7 +82,6 @@ func TestExportDefaultImport(t *testing.T) {
}, },
}, },
}) })
defer cleanup()
r := store.Export("default", s) r := store.Export("default", s)
defer r.Close() defer r.Close()
err := store.Import("dest", s, r) err := store.Import("dest", s, r)
@ -127,8 +120,7 @@ func TestExportDefaultImport(t *testing.T) {
func TestListDefaultContext(t *testing.T) { func TestListDefaultContext(t *testing.T) {
meta := testDefaultMetadata() meta := testDefaultMetadata()
s, cleanup := testStore(t, meta, store.ContextTLSData{}) s := testStore(t, meta, store.ContextTLSData{})
defer cleanup()
result, err := s.List() result, err := s.List()
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, 1, len(result)) assert.Equal(t, 1, len(result))
@ -136,8 +128,7 @@ func TestListDefaultContext(t *testing.T) {
} }
func TestGetDefaultContextStorageInfo(t *testing.T) { func TestGetDefaultContextStorageInfo(t *testing.T) {
s, cleanup := testStore(t, testDefaultMetadata(), store.ContextTLSData{}) s := testStore(t, testDefaultMetadata(), store.ContextTLSData{})
defer cleanup()
result := s.GetStorageInfo(DefaultContextName) result := s.GetStorageInfo(DefaultContextName)
assert.Equal(t, "<IN MEMORY>", result.MetadataPath) assert.Equal(t, "<IN MEMORY>", result.MetadataPath)
assert.Equal(t, "<IN MEMORY>", result.TLSPath) assert.Equal(t, "<IN MEMORY>", result.TLSPath)
@ -145,8 +136,7 @@ func TestGetDefaultContextStorageInfo(t *testing.T) {
func TestGetDefaultContextMetadata(t *testing.T) { func TestGetDefaultContextMetadata(t *testing.T) {
meta := testDefaultMetadata() meta := testDefaultMetadata()
s, cleanup := testStore(t, meta, store.ContextTLSData{}) s := testStore(t, meta, store.ContextTLSData{})
defer cleanup()
result, err := s.GetMetadata(DefaultContextName) result, err := s.GetMetadata(DefaultContextName)
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, DefaultContextName, result.Name) assert.Equal(t, DefaultContextName, result.Name)
@ -156,8 +146,7 @@ func TestGetDefaultContextMetadata(t *testing.T) {
func TestErrCreateDefault(t *testing.T) { func TestErrCreateDefault(t *testing.T) {
meta := testDefaultMetadata() meta := testDefaultMetadata()
s, cleanup := testStore(t, meta, store.ContextTLSData{}) s := testStore(t, meta, store.ContextTLSData{})
defer cleanup()
err := s.CreateOrUpdate(store.Metadata{ err := s.CreateOrUpdate(store.Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
"ep1": endpoint{Foo: "bar"}, "ep1": endpoint{Foo: "bar"},
@ -170,16 +159,14 @@ func TestErrCreateDefault(t *testing.T) {
func TestErrRemoveDefault(t *testing.T) { func TestErrRemoveDefault(t *testing.T) {
meta := testDefaultMetadata() meta := testDefaultMetadata()
s, cleanup := testStore(t, meta, store.ContextTLSData{}) s := testStore(t, meta, store.ContextTLSData{})
defer cleanup()
err := s.Remove("default") err := s.Remove("default")
assert.Error(t, err, "default context cannot be removed") assert.Error(t, err, "default context cannot be removed")
} }
func TestErrTLSDataError(t *testing.T) { func TestErrTLSDataError(t *testing.T) {
meta := testDefaultMetadata() meta := testDefaultMetadata()
s, cleanup := testStore(t, meta, store.ContextTLSData{}) s := testStore(t, meta, store.ContextTLSData{})
defer cleanup()
_, err := s.GetTLSData("default", "noop", "noop") _, err := s.GetTLSData("default", "noop", "noop")
assert.Check(t, store.IsErrTLSDataDoesNotExist(err)) assert.Check(t, store.IsErrTLSDataDoesNotExist(err))
} }

View File

@ -1,7 +1,6 @@
package command package command
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -37,15 +36,13 @@ func TestStringSliceReplaceAt(t *testing.T) {
} }
func TestValidateOutputPath(t *testing.T) { func TestValidateOutputPath(t *testing.T) {
basedir, err := ioutil.TempDir("", "TestValidateOutputPath") basedir := t.TempDir()
assert.NilError(t, err)
defer os.RemoveAll(basedir)
dir := filepath.Join(basedir, "dir") dir := filepath.Join(basedir, "dir")
notexist := filepath.Join(basedir, "notexist") notexist := filepath.Join(basedir, "notexist")
err = os.MkdirAll(dir, 0755) err := os.MkdirAll(dir, 0755)
assert.NilError(t, err) assert.NilError(t, err)
file := filepath.Join(dir, "file") file := filepath.Join(dir, "file")
err = ioutil.WriteFile(file, []byte("hi"), 0644) err = os.WriteFile(file, []byte("hi"), 0644)
assert.NilError(t, err) assert.NilError(t, err)
var testcases = []struct { var testcases = []struct {
path string path string