From 42a3800783487177a58a90c3aa4bd00c454e9391 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 5 Jul 2017 14:43:39 -0400 Subject: [PATCH] Update some tests to remove unnecessary buffers. Signed-off-by: Daniel Nephin --- cli/command/checkpoint/create_test.go | 12 +++---- cli/command/config/create_test.go | 20 +++++------- cli/command/config/ls_test.go | 43 ++++++++++-------------- cli/command/container/attach_test.go | 4 +-- cli/command/image/build_test.go | 2 +- cli/command/image/history_test.go | 10 +++--- cli/command/image/import_test.go | 2 +- cli/command/image/list_test.go | 2 +- cli/command/image/load_test.go | 4 +-- cli/command/image/remove_test.go | 16 ++++----- cli/command/image/save_test.go | 2 +- cli/command/image/tag_test.go | 9 ++--- cli/command/network/create_test.go | 9 ++--- cli/command/node/demote_test.go | 16 ++++----- cli/command/node/update_test.go | 11 +++---- cli/command/registry_test.go | 8 ++--- cli/command/secret/create_test.go | 5 ++- cli/command/stack/deploy_test.go | 5 +-- cli/command/stack/list_test.go | 4 +-- cli/command/stack/ps_test.go | 47 ++++++++++++--------------- cli/command/stack/remove_test.go | 5 ++- cli/command/stack/services_test.go | 29 +++++++---------- cli/command/swarm/leave_test.go | 13 +++----- cli/command/swarm/unlock_key_test.go | 21 +++++------- cli/command/swarm/unlock_test.go | 11 +++---- cli/command/swarm/update_test.go | 23 ++++++------- cli/command/volume/create_test.go | 5 ++- 27 files changed, 135 insertions(+), 203 deletions(-) diff --git a/cli/command/checkpoint/create_test.go b/cli/command/checkpoint/create_test.go index fd1dec423a..d0d47f456a 100644 --- a/cli/command/checkpoint/create_test.go +++ b/cli/command/checkpoint/create_test.go @@ -1,7 +1,6 @@ package checkpoint import ( - "bytes" "io/ioutil" "strings" "testing" @@ -37,9 +36,9 @@ func TestCheckpointCreateErrors(t *testing.T) { } for _, tc := range testCases { - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ checkpointCreateFunc: tc.checkpointCreateFunc, - }, &bytes.Buffer{}) + }) cmd := newCreateCommand(cli) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) @@ -50,8 +49,7 @@ func TestCheckpointCreateErrors(t *testing.T) { func TestCheckpointCreateWithOptions(t *testing.T) { var containerID, checkpointID, checkpointDir string var exit bool - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ checkpointCreateFunc: func(container string, options types.CheckpointCreateOptions) error { containerID = container checkpointID = options.CheckpointID @@ -59,7 +57,7 @@ func TestCheckpointCreateWithOptions(t *testing.T) { exit = options.Exit return nil }, - }, buf) + }) cmd := newCreateCommand(cli) checkpoint := "checkpoint-bar" cmd.SetArgs([]string{"container-foo", checkpoint}) @@ -70,5 +68,5 @@ func TestCheckpointCreateWithOptions(t *testing.T) { assert.Equal(t, checkpoint, checkpointID) assert.Equal(t, "/dir/foo", checkpointDir) assert.Equal(t, false, exit) - assert.Equal(t, checkpoint, strings.TrimSpace(buf.String())) + assert.Equal(t, checkpoint, strings.TrimSpace(cli.OutBuffer().String())) } diff --git a/cli/command/config/create_test.go b/cli/command/config/create_test.go index 6a71bee955..5cdafbdbf4 100644 --- a/cli/command/config/create_test.go +++ b/cli/command/config/create_test.go @@ -1,7 +1,6 @@ package config import ( - "bytes" "io/ioutil" "path/filepath" "reflect" @@ -41,11 +40,10 @@ func TestConfigCreateErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newConfigCreateCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ configCreateFunc: tc.configCreateFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) @@ -55,9 +53,8 @@ func TestConfigCreateErrors(t *testing.T) { func TestConfigCreateWithName(t *testing.T) { name := "foo" - buf := new(bytes.Buffer) var actual []byte - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { if spec.Name != name { return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) @@ -69,14 +66,14 @@ func TestConfigCreateWithName(t *testing.T) { ID: "ID-" + spec.Name, }, nil }, - }, buf) + }) cmd := newConfigCreateCommand(cli) cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)}) assert.NoError(t, cmd.Execute()) expected := golden.Get(t, actual, configDataFile) assert.Equal(t, string(expected), string(actual)) - assert.Equal(t, "ID-"+name, strings.TrimSpace(buf.String())) + assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String())) } func TestConfigCreateWithLabels(t *testing.T) { @@ -86,8 +83,7 @@ func TestConfigCreateWithLabels(t *testing.T) { } name := "foo" - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { if spec.Name != name { return types.ConfigCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) @@ -101,12 +97,12 @@ func TestConfigCreateWithLabels(t *testing.T) { ID: "ID-" + spec.Name, }, nil }, - }, buf) + }) cmd := newConfigCreateCommand(cli) cmd.SetArgs([]string{name, filepath.Join("testdata", configDataFile)}) cmd.Flags().Set("label", "lbl1=Label-foo") cmd.Flags().Set("label", "lbl2=Label-bar") assert.NoError(t, cmd.Execute()) - assert.Equal(t, "ID-"+name, strings.TrimSpace(buf.String())) + assert.Equal(t, "ID-"+name, strings.TrimSpace(cli.OutBuffer().String())) } diff --git a/cli/command/config/ls_test.go b/cli/command/config/ls_test.go index 9bae76f19e..c8393caca5 100644 --- a/cli/command/config/ls_test.go +++ b/cli/command/config/ls_test.go @@ -1,7 +1,6 @@ package config import ( - "bytes" "io/ioutil" "testing" "time" @@ -36,11 +35,10 @@ func TestConfigListErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newConfigListCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ configListFunc: tc.configListFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) @@ -49,8 +47,7 @@ func TestConfigListErrors(t *testing.T) { } func TestConfigList(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ *Config(ConfigID("ID-foo"), @@ -67,18 +64,17 @@ func TestConfigList(t *testing.T) { ), }, nil }, - }, buf) + }) cmd := newConfigListCommand(cli) - cmd.SetOutput(buf) + cmd.SetOutput(cli.OutBuffer()) assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "config-list.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestConfigListWithQuietOption(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ *Config(ConfigID("ID-foo"), ConfigName("foo")), @@ -87,18 +83,17 @@ func TestConfigListWithQuietOption(t *testing.T) { })), }, nil }, - }, buf) + }) cmd := newConfigListCommand(cli) cmd.Flags().Set("quiet", "true") assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "config-list-with-quiet-option.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestConfigListWithConfigFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ *Config(ConfigID("ID-foo"), ConfigName("foo")), @@ -107,20 +102,19 @@ func TestConfigListWithConfigFormat(t *testing.T) { })), }, nil }, - }, buf) + }) cli.SetConfigFile(&configfile.ConfigFile{ ConfigFormat: "{{ .Name }} {{ .Labels }}", }) cmd := newConfigListCommand(cli) assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "config-list-with-config-format.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestConfigListWithFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ *Config(ConfigID("ID-foo"), ConfigName("foo")), @@ -129,18 +123,17 @@ func TestConfigListWithFormat(t *testing.T) { })), }, nil }, - }, buf) + }) cmd := newConfigListCommand(cli) cmd.Flags().Set("format", "{{ .Name }} {{ .Labels }}") assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "config-list-with-format.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestConfigListWithFilter(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { assert.Equal(t, "foo", options.Filters.Get("name")[0]) assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0]) @@ -159,12 +152,12 @@ func TestConfigListWithFilter(t *testing.T) { ), }, nil }, - }, buf) + }) cmd := newConfigListCommand(cli) cmd.Flags().Set("filter", "name=foo") cmd.Flags().Set("filter", "label=lbl1=Label-bar") assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "config-list-with-filter.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } diff --git a/cli/command/container/attach_test.go b/cli/command/container/attach_test.go index a8f089706e..a33eeeeb54 100644 --- a/cli/command/container/attach_test.go +++ b/cli/command/container/attach_test.go @@ -1,7 +1,6 @@ package container import ( - "bytes" "io/ioutil" "testing" @@ -68,8 +67,7 @@ func TestNewAttachCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := NewAttachCommand(test.NewFakeCliWithOutput(&fakeClient{containerInspectFunc: tc.containerInspectFunc}, buf)) + cmd := NewAttachCommand(test.NewFakeCli(&fakeClient{containerInspectFunc: tc.containerInspectFunc})) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) diff --git a/cli/command/image/build_test.go b/cli/command/image/build_test.go index 3e2507166f..b3fb3c6834 100644 --- a/cli/command/image/build_test.go +++ b/cli/command/image/build_test.go @@ -38,7 +38,7 @@ func TestRunBuildDockerfileFromStdinWithCompress(t *testing.T) { return types.ImageBuildResponse{Body: ioutil.NopCloser(body)}, nil } - cli := test.NewFakeCliWithOutput(&fakeClient{imageBuildFunc: fakeImageBuild}, ioutil.Discard) + cli := test.NewFakeCli(&fakeClient{imageBuildFunc: fakeImageBuild}) dockerfile := bytes.NewBufferString(` FROM alpine:3.6 COPY foo / diff --git a/cli/command/image/history_test.go b/cli/command/image/history_test.go index 0f3a605e9f..b3abd0bef7 100644 --- a/cli/command/image/history_test.go +++ b/cli/command/image/history_test.go @@ -1,7 +1,6 @@ package image import ( - "bytes" "fmt" "io/ioutil" "regexp" @@ -38,8 +37,7 @@ func TestNewHistoryCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := NewHistoryCommand(test.NewFakeCliWithOutput(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}, buf)) + cmd := NewHistoryCommand(test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc})) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -90,13 +88,13 @@ func TestNewHistoryCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := NewHistoryCommand(test.NewFakeCliWithOutput(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}, buf)) + cli := test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}) + cmd := NewHistoryCommand(cli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() assert.NoError(t, err) - actual := buf.String() + actual := cli.OutBuffer().String() if tc.outputRegex == "" { expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("history-command-success.%s.golden", tc.name))[:]) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) diff --git a/cli/command/image/import_test.go b/cli/command/image/import_test.go index 7e07310098..6a8e2e0fd1 100644 --- a/cli/command/image/import_test.go +++ b/cli/command/image/import_test.go @@ -45,7 +45,7 @@ func TestNewImportCommandErrors(t *testing.T) { } func TestNewImportCommandInvalidFile(t *testing.T) { - cmd := NewImportCommand(test.NewFakeCliWithOutput(&fakeClient{}, new(bytes.Buffer))) + cmd := NewImportCommand(test.NewFakeCli(&fakeClient{})) cmd.SetOutput(ioutil.Discard) cmd.SetArgs([]string{"testdata/import-command-success.unexistent-file"}) testutil.ErrorContains(t, cmd.Execute(), "testdata/import-command-success.unexistent-file") diff --git a/cli/command/image/list_test.go b/cli/command/image/list_test.go index a95ef99e74..b367ef1de1 100644 --- a/cli/command/image/list_test.go +++ b/cli/command/image/list_test.go @@ -36,7 +36,7 @@ func TestNewImagesCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - cmd := NewImagesCommand(test.NewFakeCliWithOutput(&fakeClient{imageListFunc: tc.imageListFunc}, new(bytes.Buffer))) + cmd := NewImagesCommand(test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc})) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) diff --git a/cli/command/image/load_test.go b/cli/command/image/load_test.go index d0f8d81c65..06446bf650 100644 --- a/cli/command/image/load_test.go +++ b/cli/command/image/load_test.go @@ -43,7 +43,7 @@ func TestNewLoadCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - cli := test.NewFakeCliWithOutput(&fakeClient{imageLoadFunc: tc.imageLoadFunc}, new(bytes.Buffer)) + cli := test.NewFakeCli(&fakeClient{imageLoadFunc: tc.imageLoadFunc}) cli.In().SetIsTerminal(tc.isTerminalIn) cmd := NewLoadCommand(cli) cmd.SetOutput(ioutil.Discard) @@ -54,7 +54,7 @@ func TestNewLoadCommandErrors(t *testing.T) { func TestNewLoadCommandInvalidInput(t *testing.T) { expectedError := "open *" - cmd := NewLoadCommand(test.NewFakeCliWithOutput(&fakeClient{}, new(bytes.Buffer))) + cmd := NewLoadCommand(test.NewFakeCli(&fakeClient{})) cmd.SetOutput(ioutil.Discard) cmd.SetArgs([]string{"--input", "*"}) err := cmd.Execute() diff --git a/cli/command/image/remove_test.go b/cli/command/image/remove_test.go index 195e56ade6..559427247d 100644 --- a/cli/command/image/remove_test.go +++ b/cli/command/image/remove_test.go @@ -1,7 +1,6 @@ package image import ( - "bytes" "fmt" "io/ioutil" "testing" @@ -15,7 +14,7 @@ import ( ) func TestNewRemoveCommandAlias(t *testing.T) { - cmd := newRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{}, new(bytes.Buffer))) + cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{})) assert.True(t, cmd.HasAlias("rmi")) assert.True(t, cmd.HasAlias("remove")) assert.False(t, cmd.HasAlias("other")) @@ -44,9 +43,9 @@ func TestNewRemoveCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - cmd := NewRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{ + cmd := NewRemoveCommand(test.NewFakeCli(&fakeClient{ imageRemoveFunc: tc.imageRemoveFunc, - }, new(bytes.Buffer))) + })) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -97,18 +96,15 @@ func TestNewRemoveCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - errBuf := new(bytes.Buffer) - fakeCli := test.NewFakeCliWithOutput(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc}, buf) - fakeCli.SetErr(errBuf) + fakeCli := test.NewFakeCli(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc}) cmd := NewRemoveCommand(fakeCli) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) assert.NoError(t, cmd.Execute()) if tc.expectedErrMsg != "" { - assert.Equal(t, tc.expectedErrMsg, errBuf.String()) + assert.Equal(t, tc.expectedErrMsg, fakeCli.ErrBuffer().String()) } - actual := buf.String() + actual := fakeCli.OutBuffer().String() expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("remove-command-success.%s.golden", tc.name))[:]) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected) } diff --git a/cli/command/image/save_test.go b/cli/command/image/save_test.go index 042c90b768..c63eeef41c 100644 --- a/cli/command/image/save_test.go +++ b/cli/command/image/save_test.go @@ -45,7 +45,7 @@ func TestNewSaveCommandErrors(t *testing.T) { }, } for _, tc := range testCases { - cli := test.NewFakeCliWithOutput(&fakeClient{imageSaveFunc: tc.imageSaveFunc}, new(bytes.Buffer)) + cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc}) cli.Out().SetIsTerminal(tc.isTerminal) cmd := NewSaveCommand(cli) cmd.SetOutput(ioutil.Discard) diff --git a/cli/command/image/tag_test.go b/cli/command/image/tag_test.go index f9748746f4..b537f9d627 100644 --- a/cli/command/image/tag_test.go +++ b/cli/command/image/tag_test.go @@ -1,7 +1,6 @@ package image import ( - "bytes" "io/ioutil" "testing" @@ -17,9 +16,8 @@ func TestCliNewTagCommandErrors(t *testing.T) { {"image1", "image2", "image3"}, } expectedError := "\"tag\" requires exactly 2 argument(s)." - buf := new(bytes.Buffer) for _, args := range testCases { - cmd := NewTagCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf)) + cmd := NewTagCommand(test.NewFakeCli(&fakeClient{})) cmd.SetArgs(args) cmd.SetOutput(ioutil.Discard) testutil.ErrorContains(t, cmd.Execute(), expectedError) @@ -27,15 +25,14 @@ func TestCliNewTagCommandErrors(t *testing.T) { } func TestCliNewTagCommand(t *testing.T) { - buf := new(bytes.Buffer) cmd := NewTagCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ imageTagFunc: func(image string, ref string) error { assert.Equal(t, "image1", image) assert.Equal(t, "image2", ref) return nil }, - }, buf)) + })) cmd.SetArgs([]string{"image1", "image2"}) cmd.SetOutput(ioutil.Discard) assert.NoError(t, cmd.Execute()) diff --git a/cli/command/network/create_test.go b/cli/command/network/create_test.go index c3c114ba9b..76c760d0ad 100644 --- a/cli/command/network/create_test.go +++ b/cli/command/network/create_test.go @@ -1,7 +1,6 @@ package network import ( - "bytes" "io/ioutil" "strings" "testing" @@ -130,11 +129,10 @@ func TestNetworkCreateErrors(t *testing.T) { } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newCreateCommand( test.NewFakeCli(&fakeClient{ networkCreateFunc: tc.networkCreateFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) for key, value := range tc.flags { @@ -155,7 +153,6 @@ func TestNetworkCreateWithFlags(t *testing.T) { map[string]string{}, }, } - buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{ networkCreateFunc: func(ctx context.Context, name string, createBody types.NetworkCreate) (types.NetworkCreateResponse, error) { assert.Equal(t, expectedDriver, createBody.Driver, "not expected driver error") @@ -164,7 +161,7 @@ func TestNetworkCreateWithFlags(t *testing.T) { ID: name, }, nil }, - }, buf) + }) args := []string{"banana"} cmd := newCreateCommand(cli) @@ -174,5 +171,5 @@ func TestNetworkCreateWithFlags(t *testing.T) { cmd.Flags().Set("gateway", "192.168.4.1/24") cmd.Flags().Set("subnet", "192.168.4.0/24") assert.NoError(t, cmd.Execute()) - assert.Equal(t, "banana", strings.TrimSpace(buf.String())) + assert.Equal(t, "banana", strings.TrimSpace(cli.OutBuffer().String())) } diff --git a/cli/command/node/demote_test.go b/cli/command/node/demote_test.go index df4eb62410..d786cd47a2 100644 --- a/cli/command/node/demote_test.go +++ b/cli/command/node/demote_test.go @@ -1,7 +1,6 @@ package node import ( - "bytes" "io/ioutil" "testing" @@ -40,12 +39,11 @@ func TestNodeDemoteErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newDemoteCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, nodeUpdateFunc: tc.nodeUpdateFunc, - }, buf)) + })) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -53,9 +51,8 @@ func TestNodeDemoteErrors(t *testing.T) { } func TestNodeDemoteNoChange(t *testing.T) { - buf := new(bytes.Buffer) cmd := newDemoteCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { return *Node(), []byte{}, nil }, @@ -65,15 +62,14 @@ func TestNodeDemoteNoChange(t *testing.T) { } return nil }, - }, buf)) + })) cmd.SetArgs([]string{"nodeID"}) assert.NoError(t, cmd.Execute()) } func TestNodeDemoteMultipleNode(t *testing.T) { - buf := new(bytes.Buffer) cmd := newDemoteCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { return *Node(Manager()), []byte{}, nil }, @@ -83,7 +79,7 @@ func TestNodeDemoteMultipleNode(t *testing.T) { } return nil }, - }, buf)) + })) cmd.SetArgs([]string{"nodeID1", "nodeID2"}) assert.NoError(t, cmd.Execute()) } diff --git a/cli/command/node/update_test.go b/cli/command/node/update_test.go index 5e5a764d9e..3abb720c1c 100644 --- a/cli/command/node/update_test.go +++ b/cli/command/node/update_test.go @@ -1,7 +1,6 @@ package node import ( - "bytes" "io/ioutil" "testing" @@ -57,12 +56,11 @@ func TestNodeUpdateErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newUpdateCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, nodeUpdateFunc: tc.nodeUpdateFunc, - }, buf)) + })) cmd.SetArgs(tc.args) for key, value := range tc.flags { cmd.Flags().Set(key, value) @@ -158,12 +156,11 @@ func TestNodeUpdate(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newUpdateCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, nodeUpdateFunc: tc.nodeUpdateFunc, - }, buf)) + })) cmd.SetArgs(tc.args) for key, value := range tc.flags { cmd.Flags().Set(key, value) diff --git a/cli/command/registry_test.go b/cli/command/registry_test.go index 75003a68b6..5e52e470a1 100644 --- a/cli/command/registry_test.go +++ b/cli/command/registry_test.go @@ -1,7 +1,6 @@ package command_test import ( - "bytes" "testing" "github.com/pkg/errors" @@ -63,13 +62,10 @@ func TestElectAuthServer(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{infoFunc: tc.infoFunc}, buf) - errBuf := new(bytes.Buffer) - cli.SetErr(errBuf) + cli := test.NewFakeCli(&fakeClient{infoFunc: tc.infoFunc}) server := ElectAuthServer(context.Background(), cli) assert.Equal(t, tc.expectedAuthServer, server) - actual := errBuf.String() + actual := cli.ErrBuffer().String() if tc.expectedWarning == "" { assert.Empty(t, actual) } else { diff --git a/cli/command/secret/create_test.go b/cli/command/secret/create_test.go index 1df9c7108f..2c4f9f5d9c 100644 --- a/cli/command/secret/create_test.go +++ b/cli/command/secret/create_test.go @@ -41,11 +41,10 @@ func TestSecretCreateErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newSecretCreateCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ secretCreateFunc: tc.secretCreateFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) diff --git a/cli/command/stack/deploy_test.go b/cli/command/stack/deploy_test.go index 6ad8a9bdac..c04f86c251 100644 --- a/cli/command/stack/deploy_test.go +++ b/cli/command/stack/deploy_test.go @@ -1,7 +1,6 @@ package stack import ( - "bytes" "testing" "github.com/docker/cli/cli/compose/convert" @@ -18,10 +17,8 @@ func TestPruneServices(t *testing.T) { "keep": {}, } client := &fakeClient{services: []string{objectName("foo", "keep"), objectName("foo", "remove")}} - dockerCli := test.NewFakeCliWithOutput(client, &bytes.Buffer{}) - dockerCli.SetErr(&bytes.Buffer{}) + dockerCli := test.NewFakeCli(client) pruneServices(ctx, dockerCli, namespace, services) - assert.Equal(t, buildObjectIDs([]string{objectName("foo", "remove")}), client.removedServices) } diff --git a/cli/command/stack/list_test.go b/cli/command/stack/list_test.go index e072e41325..de1aa02365 100644 --- a/cli/command/stack/list_test.go +++ b/cli/command/stack/list_test.go @@ -48,9 +48,9 @@ func TestListErrors(t *testing.T) { } for _, tc := range testCases { - cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{ + cmd := newListCommand(test.NewFakeCli(&fakeClient{ serviceListFunc: tc.serviceListFunc, - }, &bytes.Buffer{})) + })) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) for key, value := range tc.flags { diff --git a/cli/command/stack/ps_test.go b/cli/command/stack/ps_test.go index 072d3abea7..5387dd0347 100644 --- a/cli/command/stack/ps_test.go +++ b/cli/command/stack/ps_test.go @@ -1,7 +1,6 @@ package stack import ( - "bytes" "io/ioutil" "testing" "time" @@ -43,9 +42,9 @@ func TestStackPsErrors(t *testing.T) { } for _, tc := range testCases { - cmd := newPsCommand(test.NewFakeCliWithOutput(&fakeClient{ + cmd := newPsCommand(test.NewFakeCli(&fakeClient{ taskListFunc: tc.taskListFunc, - }, &bytes.Buffer{})) + })) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -67,42 +66,39 @@ func TestStackPsEmptyStack(t *testing.T) { } func TestStackPsWithQuietOption(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task(TaskID("id-foo"))}, nil }, - }, buf) + }) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) cmd.Flags().Set("quiet", "true") assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "stack-ps-with-quiet-option.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestStackPsWithNoTruncOption(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task(TaskID("xn4cypcov06f2w8gsbaf2lst3"))}, nil }, - }, buf) + }) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) cmd.Flags().Set("no-trunc", "true") cmd.Flags().Set("format", "{{ .ID }}") assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "stack-ps-with-no-trunc-option.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestStackPsWithNoResolveOption(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task( TaskNodeID("id-node-foo"), @@ -111,54 +107,51 @@ func TestStackPsWithNoResolveOption(t *testing.T) { nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) { return *Node(NodeName("node-name-bar")), nil, nil }, - }, buf) + }) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) cmd.Flags().Set("no-resolve", "true") cmd.Flags().Set("format", "{{ .Node }}") assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "stack-ps-with-no-resolve-option.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestStackPsWithFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil }, - }, buf) + }) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) cmd.Flags().Set("format", "{{ .Name }}") assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "stack-ps-with-format.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestStackPsWithConfigFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil }, - }, buf) + }) cli.SetConfigFile(&configfile.ConfigFile{ TasksFormat: "{{ .Name }}", }) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "stack-ps-with-config-format.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestStackPsWithoutFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task( TaskID("id-foo"), @@ -172,11 +165,11 @@ func TestStackPsWithoutFormat(t *testing.T) { nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) { return *Node(NodeName("node-name-bar")), nil, nil }, - }, buf) + }) cmd := newPsCommand(cli) cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "stack-ps-without-format.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } diff --git a/cli/command/stack/remove_test.go b/cli/command/stack/remove_test.go index 0bbeb2da15..a196bf1653 100644 --- a/cli/command/stack/remove_test.go +++ b/cli/command/stack/remove_test.go @@ -1,7 +1,6 @@ package stack import ( - "bytes" "errors" "io/ioutil" "strings" @@ -46,7 +45,7 @@ func TestRemoveStack(t *testing.T) { secrets: allSecrets, configs: allConfigs, } - cmd := newRemoveCommand(test.NewFakeCliWithOutput(cli, &bytes.Buffer{})) + cmd := newRemoveCommand(test.NewFakeCli(cli)) cmd.SetArgs([]string{"foo", "bar"}) assert.NoError(t, cmd.Execute()) @@ -117,7 +116,7 @@ func TestRemoveContinueAfterError(t *testing.T) { return nil }, } - cmd := newRemoveCommand(test.NewFakeCliWithOutput(cli, &bytes.Buffer{})) + cmd := newRemoveCommand(test.NewFakeCli(cli)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs([]string{"foo", "bar"}) diff --git a/cli/command/stack/services_test.go b/cli/command/stack/services_test.go index 9dc4a9509e..822660fbc2 100644 --- a/cli/command/stack/services_test.go +++ b/cli/command/stack/services_test.go @@ -1,7 +1,6 @@ package stack import ( - "bytes" "io/ioutil" "testing" @@ -95,62 +94,58 @@ func TestStackServicesEmptyServiceList(t *testing.T) { } func TestStackServicesWithQuietOption(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{*Service(ServiceID("id-foo"))}, nil }, - }, buf) + }) cmd := newServicesCommand(cli) cmd.Flags().Set("quiet", "true") cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "stack-services-with-quiet-option.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestStackServicesWithFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service(ServiceName("service-name-foo")), }, nil }, - }, buf) + }) cmd := newServicesCommand(cli) cmd.SetArgs([]string{"foo"}) cmd.Flags().Set("format", "{{ .Name }}") assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "stack-services-with-format.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestStackServicesWithConfigFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service(ServiceName("service-name-foo")), }, nil }, - }, buf) + }) cli.SetConfigFile(&configfile.ConfigFile{ ServicesFormat: "{{ .Name }}", }) cmd := newServicesCommand(cli) cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "stack-services-with-config-format.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } func TestStackServicesWithoutFormat(t *testing.T) { - buf := new(bytes.Buffer) - cli := test.NewFakeCliWithOutput(&fakeClient{ + cli := test.NewFakeCli(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{*Service( ServiceName("name-foo"), @@ -165,11 +160,11 @@ func TestStackServicesWithoutFormat(t *testing.T) { }), )}, nil }, - }, buf) + }) cmd := newServicesCommand(cli) cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), "stack-services-without-format.golden") testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } diff --git a/cli/command/swarm/leave_test.go b/cli/command/swarm/leave_test.go index 07c1945296..aecac28430 100644 --- a/cli/command/swarm/leave_test.go +++ b/cli/command/swarm/leave_test.go @@ -1,7 +1,6 @@ package swarm import ( - "bytes" "io/ioutil" "strings" "testing" @@ -33,11 +32,10 @@ func TestSwarmLeaveErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newLeaveCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ swarmLeaveFunc: tc.swarmLeaveFunc, - }, buf)) + })) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -45,9 +43,8 @@ func TestSwarmLeaveErrors(t *testing.T) { } func TestSwarmLeave(t *testing.T) { - buf := new(bytes.Buffer) - cmd := newLeaveCommand( - test.NewFakeCliWithOutput(&fakeClient{}, buf)) + cli := test.NewFakeCli(&fakeClient{}) + cmd := newLeaveCommand(cli) assert.NoError(t, cmd.Execute()) - assert.Equal(t, "Node left the swarm.", strings.TrimSpace(buf.String())) + assert.Equal(t, "Node left the swarm.", strings.TrimSpace(cli.OutBuffer().String())) } diff --git a/cli/command/swarm/unlock_key_test.go b/cli/command/swarm/unlock_key_test.go index f2f60706ff..9289a151f8 100644 --- a/cli/command/swarm/unlock_key_test.go +++ b/cli/command/swarm/unlock_key_test.go @@ -1,7 +1,6 @@ package swarm import ( - "bytes" "fmt" "io/ioutil" "testing" @@ -83,13 +82,12 @@ func TestSwarmUnlockKeyErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newUnlockKeyCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ swarmInspectFunc: tc.swarmInspectFunc, swarmUpdateFunc: tc.swarmUpdateFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, - }, buf)) + })) cmd.SetArgs(tc.args) for key, value := range tc.flags { cmd.Flags().Set(key, value) @@ -158,19 +156,18 @@ func TestSwarmUnlockKey(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newUnlockKeyCommand( - test.NewFakeCliWithOutput(&fakeClient{ - swarmInspectFunc: tc.swarmInspectFunc, - swarmUpdateFunc: tc.swarmUpdateFunc, - swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{ + swarmInspectFunc: tc.swarmInspectFunc, + swarmUpdateFunc: tc.swarmUpdateFunc, + swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, + }) + cmd := newUnlockKeyCommand(cli) cmd.SetArgs(tc.args) for key, value := range tc.flags { cmd.Flags().Set(key, value) } assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), fmt.Sprintf("unlockkeys-%s.golden", tc.name)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } diff --git a/cli/command/swarm/unlock_test.go b/cli/command/swarm/unlock_test.go index 63b47e472a..bf339faaf1 100644 --- a/cli/command/swarm/unlock_test.go +++ b/cli/command/swarm/unlock_test.go @@ -1,7 +1,6 @@ package swarm import ( - "bytes" "io/ioutil" "strings" "testing" @@ -66,12 +65,11 @@ func TestSwarmUnlockErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newUnlockCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ infoFunc: tc.infoFunc, swarmUnlockFunc: tc.swarmUnlockFunc, - }, buf)) + })) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -80,8 +78,7 @@ func TestSwarmUnlockErrors(t *testing.T) { func TestSwarmUnlock(t *testing.T) { input := "unlockKey" - buf := new(bytes.Buffer) - dockerCli := test.NewFakeCliWithOutput(&fakeClient{ + dockerCli := test.NewFakeCli(&fakeClient{ infoFunc: func() (types.Info, error) { return types.Info{ Swarm: swarm.Info{ @@ -95,7 +92,7 @@ func TestSwarmUnlock(t *testing.T) { } return nil }, - }, buf) + }) dockerCli.SetIn(command.NewInStream(ioutil.NopCloser(strings.NewReader(input)))) cmd := newUnlockCommand(dockerCli) assert.NoError(t, cmd.Execute()) diff --git a/cli/command/swarm/update_test.go b/cli/command/swarm/update_test.go index 926916f01f..2b8c4a8af8 100644 --- a/cli/command/swarm/update_test.go +++ b/cli/command/swarm/update_test.go @@ -1,7 +1,6 @@ package swarm import ( - "bytes" "fmt" "io/ioutil" "testing" @@ -68,13 +67,12 @@ func TestSwarmUpdateErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newUpdateCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ swarmInspectFunc: tc.swarmInspectFunc, swarmUpdateFunc: tc.swarmUpdateFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, - }, buf)) + })) cmd.SetArgs(tc.args) for key, value := range tc.flags { cmd.Flags().Set(key, value) @@ -164,20 +162,19 @@ func TestSwarmUpdate(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) - cmd := newUpdateCommand( - test.NewFakeCliWithOutput(&fakeClient{ - swarmInspectFunc: tc.swarmInspectFunc, - swarmUpdateFunc: tc.swarmUpdateFunc, - swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, - }, buf)) + cli := test.NewFakeCli(&fakeClient{ + swarmInspectFunc: tc.swarmInspectFunc, + swarmUpdateFunc: tc.swarmUpdateFunc, + swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, + }) + cmd := newUpdateCommand(cli) cmd.SetArgs(tc.args) for key, value := range tc.flags { cmd.Flags().Set(key, value) } - cmd.SetOutput(buf) + cmd.SetOutput(cli.OutBuffer()) assert.NoError(t, cmd.Execute()) - actual := buf.String() + actual := cli.OutBuffer().String() expected := golden.Get(t, []byte(actual), fmt.Sprintf("update-%s.golden", tc.name)) testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, string(expected)) } diff --git a/cli/command/volume/create_test.go b/cli/command/volume/create_test.go index ed85cd34c6..05b17eccdf 100644 --- a/cli/command/volume/create_test.go +++ b/cli/command/volume/create_test.go @@ -41,11 +41,10 @@ func TestVolumeCreateErrors(t *testing.T) { }, } for _, tc := range testCases { - buf := new(bytes.Buffer) cmd := newCreateCommand( - test.NewFakeCliWithOutput(&fakeClient{ + test.NewFakeCli(&fakeClient{ volumeCreateFunc: tc.volumeCreateFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) for key, value := range tc.flags {