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 (
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -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)

View File

@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"os"
@ -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")
}

View File

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

View File

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