diff --git a/cli/command/config/ls_test.go b/cli/command/config/ls_test.go index 465cf12e91..9bae76f19e 100644 --- a/cli/command/config/ls_test.go +++ b/cli/command/config/ls_test.go @@ -68,7 +68,6 @@ func TestConfigList(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newConfigListCommand(cli) cmd.SetOutput(buf) assert.NoError(t, cmd.Execute()) @@ -89,7 +88,6 @@ func TestConfigListWithQuietOption(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newConfigListCommand(cli) cmd.Flags().Set("quiet", "true") assert.NoError(t, cmd.Execute()) @@ -110,7 +108,7 @@ func TestConfigListWithConfigFormat(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{ + cli.SetConfigFile(&configfile.ConfigFile{ ConfigFormat: "{{ .Name }} {{ .Labels }}", }) cmd := newConfigListCommand(cli) @@ -162,7 +160,6 @@ func TestConfigListWithFilter(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newConfigListCommand(cli) cmd.Flags().Set("filter", "name=foo") cmd.Flags().Set("filter", "label=lbl1=Label-bar") diff --git a/cli/command/container/exec_test.go b/cli/command/container/exec_test.go index 783a17a606..0eed2ff0f7 100644 --- a/cli/command/container/exec_test.go +++ b/cli/command/container/exec_test.go @@ -1,15 +1,14 @@ package container import ( - "bytes" "io/ioutil" "testing" - "github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/testutil" "github.com/pkg/errors" + "github.com/stretchr/testify/require" ) type arguments struct { @@ -79,9 +78,7 @@ func TestParseExec(t *testing.T) { for valid, expectedExecConfig := range valids { execConfig, err := parseExec(&valid.options, valid.execCmd) - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) if !compareExecConfig(expectedExecConfig, execConfig) { t.Fatalf("Expected [%v] for %v, got [%v]", expectedExecConfig, valid, execConfig) } @@ -138,10 +135,7 @@ func TestNewExecCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - conf := configfile.ConfigFile{} - cli := test.NewFakeCliWithOutput(&fakeClient{containerInspectFunc: tc.containerInspectFunc}, buf) - cli.SetConfigfile(&conf) + cli := test.NewFakeCli(&fakeClient{containerInspectFunc: tc.containerInspectFunc}) cmd := NewExecCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) diff --git a/cli/command/image/list_test.go b/cli/command/image/list_test.go index 76c8731d91..a95ef99e74 100644 --- a/cli/command/image/list_test.go +++ b/cli/command/image/list_test.go @@ -82,7 +82,7 @@ func TestNewImagesCommandSuccess(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cli := test.NewFakeCliWithOutput(&fakeClient{imageListFunc: tc.imageListFunc}, buf) - cli.SetConfigfile(&configfile.ConfigFile{ImagesFormat: tc.imageFormat}) + cli.SetConfigFile(&configfile.ConfigFile{ImagesFormat: tc.imageFormat}) cmd := NewImagesCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) @@ -95,7 +95,7 @@ func TestNewImagesCommandSuccess(t *testing.T) { } func TestNewListCommandAlias(t *testing.T) { - cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{}, new(bytes.Buffer))) + cmd := newListCommand(test.NewFakeCli(&fakeClient{})) assert.True(t, cmd.HasAlias("images")) assert.True(t, cmd.HasAlias("list")) assert.False(t, cmd.HasAlias("other")) diff --git a/cli/command/image/pull_test.go b/cli/command/image/pull_test.go index 26d0b74460..c23484f832 100644 --- a/cli/command/image/pull_test.go +++ b/cli/command/image/pull_test.go @@ -1,12 +1,10 @@ package image import ( - "bytes" "fmt" "io/ioutil" "testing" - "github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/pkg/testutil" "github.com/docker/docker/pkg/testutil/golden" @@ -41,9 +39,7 @@ func TestNewPullCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{}, buf) - cli.SetConfigfile(configfile.New("filename")) + cli := test.NewFakeCli(&fakeClient{}) cmd := NewPullCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) @@ -66,15 +62,13 @@ func TestNewPullCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{}, buf) - cli.SetConfigfile(configfile.New("filename")) + cli := test.NewFakeCli(&fakeClient{}) cmd := NewPullCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() assert.NoError(t, err) - actual := buf.String() + actual := cli.OutBuffer().String() expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("pull-command-success.%s.golden", tc.name))[:]) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) } diff --git a/cli/command/image/push_test.go b/cli/command/image/push_test.go index 2e0c58e642..2c824c46e2 100644 --- a/cli/command/image/push_test.go +++ b/cli/command/image/push_test.go @@ -1,13 +1,11 @@ package image import ( - "bytes" "io" "io/ioutil" "strings" "testing" - "github.com/docker/cli/cli/config/configfile" "github.com/docker/cli/cli/internal/test" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/testutil" @@ -47,9 +45,7 @@ func TestNewPushCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{imagePushFunc: tc.imagePushFunc}, buf) - cli.SetConfigfile(configfile.New("filename")) + cli := test.NewFakeCli(&fakeClient{imagePushFunc: tc.imagePushFunc}) cmd := NewPushCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) @@ -68,13 +64,11 @@ func TestNewPushCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) { return ioutil.NopCloser(strings.NewReader("")), nil }, - }, buf) - cli.SetConfigfile(configfile.New("filename")) + }) cmd := NewPushCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) diff --git a/cli/command/node/list_test.go b/cli/command/node/list_test.go index dd906d7144..eb58d7709e 100644 --- a/cli/command/node/list_test.go +++ b/cli/command/node/list_test.go @@ -42,12 +42,10 @@ func TestNodeListErrorOnAPIFailure(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ nodeListFunc: tc.nodeListFunc, infoFunc: tc.infoFunc, - }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) + }) cmd := newListCommand(cli) cmd.SetOutput(ioutil.Discard) assert.EqualError(t, cmd.Execute(), tc.expectedError) @@ -55,8 +53,7 @@ func TestNodeListErrorOnAPIFailure(t *testing.T) { } func TestNodeList(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), @@ -71,13 +68,14 @@ func TestNodeList(t *testing.T) { }, }, nil }, - }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) + }) + cmd := newListCommand(cli) assert.NoError(t, cmd.Execute()) - assert.Contains(t, buf.String(), `nodeID1 * nodeHostname1 Ready Active Leader`) - assert.Contains(t, buf.String(), `nodeID2 nodeHostname2 Ready Active Reachable`) - assert.Contains(t, buf.String(), `nodeID3 nodeHostname3 Ready Active`) + out := cli.OutBuffer().String() + assert.Contains(t, out, `nodeID1 * nodeHostname1 Ready Active Leader`) + assert.Contains(t, out, `nodeID2 nodeHostname2 Ready Active Reachable`) + assert.Contains(t, out, `nodeID3 nodeHostname3 Ready Active`) } func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) { @@ -89,7 +87,6 @@ func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newListCommand(cli) cmd.Flags().Set("quiet", "true") assert.NoError(t, cmd.Execute()) @@ -100,7 +97,6 @@ func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) { func TestNodeListContainsHostname(t *testing.T) { buf := new(bytes.Buffer) cli := test.NewFakeCliWithOutput(&fakeClient{}, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newListCommand(cli) assert.NoError(t, cmd.Execute()) assert.Contains(t, buf.String(), "HOSTNAME") @@ -124,7 +120,7 @@ func TestNodeListDefaultFormat(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{ + cli.SetConfigFile(&configfile.ConfigFile{ NodesFormat: "{{.ID}}: {{.Hostname}} {{.Status}}/{{.ManagerStatus}}", }) cmd := newListCommand(cli) @@ -151,7 +147,7 @@ func TestNodeListFormat(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{ + cli.SetConfigFile(&configfile.ConfigFile{ NodesFormat: "{{.ID}}: {{.Hostname}} {{.Status}}/{{.ManagerStatus}}", }) cmd := newListCommand(cli) diff --git a/cli/command/secret/ls_test.go b/cli/command/secret/ls_test.go index a07f9c8cbb..fb033efea6 100644 --- a/cli/command/secret/ls_test.go +++ b/cli/command/secret/ls_test.go @@ -68,7 +68,6 @@ func TestSecretList(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newSecretListCommand(cli) cmd.SetOutput(buf) assert.NoError(t, cmd.Execute()) @@ -89,7 +88,6 @@ func TestSecretListWithQuietOption(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newSecretListCommand(cli) cmd.Flags().Set("quiet", "true") assert.NoError(t, cmd.Execute()) @@ -110,7 +108,7 @@ func TestSecretListWithConfigFormat(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{ + cli.SetConfigFile(&configfile.ConfigFile{ SecretFormat: "{{ .Name }} {{ .Labels }}", }) cmd := newSecretListCommand(cli) @@ -162,7 +160,6 @@ func TestSecretListWithFilter(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newSecretListCommand(cli) cmd.Flags().Set("filter", "name=foo") cmd.Flags().Set("filter", "label=lbl1=Label-bar") diff --git a/cli/command/stack/ps_test.go b/cli/command/stack/ps_test.go index b67adb06be..072d3abea7 100644 --- a/cli/command/stack/ps_test.go +++ b/cli/command/stack/ps_test.go @@ -73,7 +73,6 @@ func TestStackPsWithQuietOption(t *testing.T) { return []swarm.Task{*Task(TaskID("id-foo"))}, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) cmd.Flags().Set("quiet", "true") @@ -91,7 +90,6 @@ func TestStackPsWithNoTruncOption(t *testing.T) { return []swarm.Task{*Task(TaskID("xn4cypcov06f2w8gsbaf2lst3"))}, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) cmd.Flags().Set("no-trunc", "true") @@ -114,7 +112,6 @@ func TestStackPsWithNoResolveOption(t *testing.T) { return *Node(NodeName("node-name-bar")), nil, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) cmd.Flags().Set("no-resolve", "true") @@ -132,7 +129,6 @@ func TestStackPsWithFormat(t *testing.T) { return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) cmd.Flags().Set("format", "{{ .Name }}") @@ -149,7 +145,7 @@ func TestStackPsWithConfigFormat(t *testing.T) { return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{ + cli.SetConfigFile(&configfile.ConfigFile{ TasksFormat: "{{ .Name }}", }) cmd := newPsCommand(cli) @@ -177,7 +173,6 @@ func TestStackPsWithoutFormat(t *testing.T) { return *Node(NodeName("node-name-bar")), nil, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) diff --git a/cli/command/stack/services_test.go b/cli/command/stack/services_test.go index 0225822fdb..9dc4a9509e 100644 --- a/cli/command/stack/services_test.go +++ b/cli/command/stack/services_test.go @@ -66,12 +66,11 @@ func TestStackServicesErrors(t *testing.T) { } for _, tc := range testCases { - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ serviceListFunc: tc.serviceListFunc, nodeListFunc: tc.nodeListFunc, taskListFunc: tc.taskListFunc, - }, &bytes.Buffer{}) - cli.SetConfigfile(&configfile.ConfigFile{}) + }) cmd := newServicesCommand(cli) cmd.SetArgs(tc.args) for key, value := range tc.flags { @@ -102,7 +101,6 @@ func TestStackServicesWithQuietOption(t *testing.T) { return []swarm.Service{*Service(ServiceID("id-foo"))}, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newServicesCommand(cli) cmd.Flags().Set("quiet", "true") cmd.SetArgs([]string{"foo"}) @@ -121,7 +119,6 @@ func TestStackServicesWithFormat(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newServicesCommand(cli) cmd.SetArgs([]string{"foo"}) cmd.Flags().Set("format", "{{ .Name }}") @@ -140,7 +137,7 @@ func TestStackServicesWithConfigFormat(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{ + cli.SetConfigFile(&configfile.ConfigFile{ ServicesFormat: "{{ .Name }}", }) cmd := newServicesCommand(cli) @@ -169,7 +166,6 @@ func TestStackServicesWithoutFormat(t *testing.T) { )}, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newServicesCommand(cli) cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) diff --git a/cli/command/volume/list_test.go b/cli/command/volume/list_test.go index 1338915dc4..92cd62b597 100644 --- a/cli/command/volume/list_test.go +++ b/cli/command/volume/list_test.go @@ -67,7 +67,6 @@ func TestVolumeListWithoutFormat(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newListCommand(cli) assert.NoError(t, cmd.Execute()) actual := buf.String() @@ -90,7 +89,7 @@ func TestVolumeListWithConfigFormat(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{ + cli.SetConfigFile(&configfile.ConfigFile{ VolumesFormat: "{{ .Name }} {{ .Driver }} {{ .Labels }}", }) cmd := newListCommand(cli) @@ -115,7 +114,6 @@ func TestVolumeListWithFormat(t *testing.T) { }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newListCommand(cli) cmd.Flags().Set("format", "{{ .Name }} {{ .Driver }} {{ .Labels }}") assert.NoError(t, cmd.Execute()) diff --git a/cli/internal/test/cli.go b/cli/internal/test/cli.go index de765742ba..54b3bc83b1 100644 --- a/cli/internal/test/cli.go +++ b/cli/internal/test/cli.go @@ -55,8 +55,8 @@ func (c *FakeCli) SetErr(err *bytes.Buffer) { c.err = err } -// SetConfigfile sets the "fake" config file -func (c *FakeCli) SetConfigfile(configfile *configfile.ConfigFile) { +// SetConfigFile sets the "fake" config file +func (c *FakeCli) SetConfigFile(configfile *configfile.ConfigFile) { c.configfile = configfile }