diff --git a/cli/command/checkpoint/create_test.go b/cli/command/checkpoint/create_test.go index dfbf6317a1..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" @@ -39,7 +38,7 @@ func TestCheckpointCreateErrors(t *testing.T) { for _, tc := range testCases { cli := test.NewFakeCli(&fakeClient{ checkpointCreateFunc: tc.checkpointCreateFunc, - }, &bytes.Buffer{}) + }) cmd := newCreateCommand(cli) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) @@ -50,7 +49,6 @@ func TestCheckpointCreateErrors(t *testing.T) { func TestCheckpointCreateWithOptions(t *testing.T) { var containerID, checkpointID, checkpointDir string var exit bool - buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{ checkpointCreateFunc: func(container string, options types.CheckpointCreateOptions) error { containerID = container @@ -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/checkpoint/list_test.go b/cli/command/checkpoint/list_test.go index 7d4f2e1b73..1c13f27755 100644 --- a/cli/command/checkpoint/list_test.go +++ b/cli/command/checkpoint/list_test.go @@ -37,7 +37,7 @@ func TestCheckpointListErrors(t *testing.T) { } for _, tc := range testCases { - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ checkpointListFunc: tc.checkpointListFunc, }, &bytes.Buffer{}) cmd := newListCommand(cli) @@ -50,7 +50,7 @@ func TestCheckpointListErrors(t *testing.T) { func TestCheckpointListWithOptions(t *testing.T) { var containerID, checkpointDir string buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ checkpointListFunc: func(container string, options types.CheckpointListOptions) ([]types.Checkpoint, error) { containerID = container checkpointDir = options.CheckpointDir diff --git a/cli/command/checkpoint/remove_test.go b/cli/command/checkpoint/remove_test.go index 2ca8e32d66..ecc66d8be5 100644 --- a/cli/command/checkpoint/remove_test.go +++ b/cli/command/checkpoint/remove_test.go @@ -36,7 +36,7 @@ func TestCheckpointRemoveErrors(t *testing.T) { } for _, tc := range testCases { - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ checkpointDeleteFunc: tc.checkpointDeleteFunc, }, &bytes.Buffer{}) cmd := newRemoveCommand(cli) @@ -48,7 +48,7 @@ func TestCheckpointRemoveErrors(t *testing.T) { func TestCheckpointRemoveWithOptions(t *testing.T) { var containerID, checkpointID, checkpointDir string - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ checkpointDeleteFunc: func(container string, options types.CheckpointDeleteOptions) error { containerID = container checkpointID = options.CheckpointID diff --git a/cli/command/config/create_test.go b/cli/command/config/create_test.go index 25b133836f..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.NewFakeCli(&fakeClient{ configCreateFunc: tc.configCreateFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) @@ -55,7 +53,6 @@ func TestConfigCreateErrors(t *testing.T) { func TestConfigCreateWithName(t *testing.T) { name := "foo" - buf := new(bytes.Buffer) var actual []byte cli := test.NewFakeCli(&fakeClient{ configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { @@ -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,7 +83,6 @@ func TestConfigCreateWithLabels(t *testing.T) { } name := "foo" - buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{ configCreateFunc: func(spec swarm.ConfigSpec) (types.ConfigCreateResponse, error) { if spec.Name != 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/inspect_test.go b/cli/command/config/inspect_test.go index 69d36cb7ad..e1af0b6b09 100644 --- a/cli/command/config/inspect_test.go +++ b/cli/command/config/inspect_test.go @@ -55,7 +55,7 @@ func TestConfigInspectErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configInspectFunc: tc.configInspectFunc, }, buf), ) @@ -97,7 +97,7 @@ func TestConfigInspectWithoutFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configInspectFunc: tc.configInspectFunc, }, buf), ) @@ -137,7 +137,7 @@ func TestConfigInspectWithFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configInspectFunc: tc.configInspectFunc, }, buf), ) @@ -174,7 +174,7 @@ func TestConfigInspectPretty(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configInspectFunc: tc.configInspectFunc, }, buf)) cmd.SetArgs([]string{"configID"}) diff --git a/cli/command/config/ls_test.go b/cli/command/config/ls_test.go index b43c764a4d..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.NewFakeCli(&fakeClient{ configListFunc: tc.configListFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) @@ -49,7 +47,6 @@ func TestConfigListErrors(t *testing.T) { } func TestConfigList(t *testing.T) { - buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ @@ -67,18 +64,16 @@ func TestConfigList(t *testing.T) { ), }, nil }, - }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) + }) 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.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ @@ -88,18 +83,16 @@ func TestConfigListWithQuietOption(t *testing.T) { })), }, nil }, - }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) + }) 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.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ @@ -109,19 +102,18 @@ func TestConfigListWithConfigFormat(t *testing.T) { })), }, nil }, - }, buf) - cli.SetConfigfile(&configfile.ConfigFile{ + }) + 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.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { return []swarm.Config{ @@ -131,17 +123,16 @@ 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.NewFakeCli(&fakeClient{ configListFunc: func(options types.ConfigListOptions) ([]swarm.Config, error) { assert.Equal(t, "foo", options.Filters.Get("name")[0]) @@ -161,13 +152,12 @@ 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") 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/config/remove_test.go b/cli/command/config/remove_test.go index c183a93c8b..85b188c41d 100644 --- a/cli/command/config/remove_test.go +++ b/cli/command/config/remove_test.go @@ -33,7 +33,7 @@ func TestConfigRemoveErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newConfigRemoveCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ configRemoveFunc: tc.configRemoveFunc, }, buf), ) @@ -47,7 +47,7 @@ func TestConfigRemoveWithName(t *testing.T) { names := []string{"foo", "bar"} buf := new(bytes.Buffer) var removedConfigs []string - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configRemoveFunc: func(name string) error { removedConfigs = append(removedConfigs, name) return nil @@ -65,7 +65,7 @@ func TestConfigRemoveContinueAfterError(t *testing.T) { buf := new(bytes.Buffer) var removedConfigs []string - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ configRemoveFunc: func(name string) error { removedConfigs = append(removedConfigs, name) if name == "foo" { diff --git a/cli/command/container/attach_test.go b/cli/command/container/attach_test.go index 14b8137dae..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.NewFakeCli(&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/container/exec_test.go b/cli/command/container/exec_test.go index b2df1c2b0c..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.NewFakeCli(&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/build_test.go b/cli/command/image/build_test.go index bf77292a85..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.NewFakeCli(&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 8582c09b62..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.NewFakeCli(&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.NewFakeCli(&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 134722f0f1..6a8e2e0fd1 100644 --- a/cli/command/image/import_test.go +++ b/cli/command/image/import_test.go @@ -37,7 +37,7 @@ func TestNewImportCommandErrors(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc}, buf)) + cmd := NewImportCommand(test.NewFakeCliWithOutput(&fakeClient{imageImportFunc: tc.imageImportFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -45,7 +45,7 @@ func TestNewImportCommandErrors(t *testing.T) { } func TestNewImportCommandInvalidFile(t *testing.T) { - cmd := NewImportCommand(test.NewFakeCli(&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") @@ -92,7 +92,7 @@ func TestNewImportCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc}, buf)) + cmd := NewImportCommand(test.NewFakeCliWithOutput(&fakeClient{imageImportFunc: tc.imageImportFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) assert.NoError(t, cmd.Execute()) diff --git a/cli/command/image/inspect_test.go b/cli/command/image/inspect_test.go index b3a2c2f5f5..2f336bdbad 100644 --- a/cli/command/image/inspect_test.go +++ b/cli/command/image/inspect_test.go @@ -27,7 +27,7 @@ func TestNewInspectCommandErrors(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := newInspectCommand(test.NewFakeCli(&fakeClient{}, buf)) + cmd := newInspectCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) @@ -79,7 +79,7 @@ func TestNewInspectCommandSuccess(t *testing.T) { for _, tc := range testCases { imageInspectInvocationCount = 0 buf := new(bytes.Buffer) - cmd := newInspectCommand(test.NewFakeCli(&fakeClient{imageInspectFunc: tc.imageInspectFunc}, buf)) + cmd := newInspectCommand(test.NewFakeCliWithOutput(&fakeClient{imageInspectFunc: tc.imageInspectFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() diff --git a/cli/command/image/list_test.go b/cli/command/image/list_test.go index 070b19efb1..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.NewFakeCli(&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) @@ -81,8 +81,8 @@ func TestNewImagesCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc}, buf) - cli.SetConfigfile(&configfile.ConfigFile{ImagesFormat: tc.imageFormat}) + cli := test.NewFakeCliWithOutput(&fakeClient{imageListFunc: tc.imageListFunc}, buf) + 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.NewFakeCli(&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/load_test.go b/cli/command/image/load_test.go index ebe1a0c7e3..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.NewFakeCli(&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.NewFakeCli(&fakeClient{}, new(bytes.Buffer))) + cmd := NewLoadCommand(test.NewFakeCli(&fakeClient{})) cmd.SetOutput(ioutil.Discard) cmd.SetArgs([]string{"--input", "*"}) err := cmd.Execute() @@ -93,7 +93,7 @@ func TestNewLoadCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewLoadCommand(test.NewFakeCli(&fakeClient{imageLoadFunc: tc.imageLoadFunc}, buf)) + cmd := NewLoadCommand(test.NewFakeCliWithOutput(&fakeClient{imageLoadFunc: tc.imageLoadFunc}, buf)) cmd.SetOutput(ioutil.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() diff --git a/cli/command/image/prune_test.go b/cli/command/image/prune_test.go index c17a75d224..be25826df0 100644 --- a/cli/command/image/prune_test.go +++ b/cli/command/image/prune_test.go @@ -38,7 +38,7 @@ func TestNewPruneCommandErrors(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewPruneCommand(test.NewFakeCli(&fakeClient{ + cmd := NewPruneCommand(test.NewFakeCliWithOutput(&fakeClient{ imagesPruneFunc: tc.imagesPruneFunc, }, buf)) cmd.SetOutput(ioutil.Discard) @@ -86,7 +86,7 @@ func TestNewPruneCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewPruneCommand(test.NewFakeCli(&fakeClient{ + cmd := NewPruneCommand(test.NewFakeCliWithOutput(&fakeClient{ imagesPruneFunc: tc.imagesPruneFunc, }, buf)) cmd.SetOutput(ioutil.Discard) diff --git a/cli/command/image/pull_test.go b/cli/command/image/pull_test.go index 690e6222eb..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.NewFakeCli(&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.NewFakeCli(&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 f2c35dee21..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.NewFakeCli(&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.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/image/remove_test.go b/cli/command/image/remove_test.go index 9b02b24a90..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.NewFakeCli(&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")) @@ -46,7 +45,7 @@ func TestNewRemoveCommandErrors(t *testing.T) { for _, tc := range testCases { 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.NewFakeCli(&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 d137dc92f9..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.NewFakeCli(&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) @@ -85,7 +85,7 @@ func TestNewSaveCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - cmd := NewSaveCommand(test.NewFakeCli(&fakeClient{ + cmd := NewSaveCommand(test.NewFakeCliWithOutput(&fakeClient{ imageSaveFunc: func(images []string) (io.ReadCloser, error) { return ioutil.NopCloser(strings.NewReader("")), nil }, diff --git a/cli/command/image/tag_test.go b/cli/command/image/tag_test.go index 8cf0c534b8..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.NewFakeCli(&fakeClient{}, buf)) + cmd := NewTagCommand(test.NewFakeCli(&fakeClient{})) cmd.SetArgs(args) cmd.SetOutput(ioutil.Discard) testutil.ErrorContains(t, cmd.Execute(), expectedError) @@ -27,7 +25,6 @@ func TestCliNewTagCommandErrors(t *testing.T) { } func TestCliNewTagCommand(t *testing.T) { - buf := new(bytes.Buffer) cmd := NewTagCommand( test.NewFakeCli(&fakeClient{ imageTagFunc: func(image string, ref string) error { @@ -35,7 +32,7 @@ func TestCliNewTagCommand(t *testing.T) { 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 7126dddd13..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.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,7 +51,6 @@ func TestNodeDemoteErrors(t *testing.T) { } func TestNodeDemoteNoChange(t *testing.T) { - buf := new(bytes.Buffer) cmd := newDemoteCommand( test.NewFakeCli(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { @@ -65,13 +62,12 @@ 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.NewFakeCli(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { @@ -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/inspect_test.go b/cli/command/node/inspect_test.go index 2a21e12a28..ca88371f8a 100644 --- a/cli/command/node/inspect_test.go +++ b/cli/command/node/inspect_test.go @@ -69,7 +69,7 @@ func TestNodeInspectErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, infoFunc: tc.infoFunc, }, buf)) @@ -111,7 +111,7 @@ func TestNodeInspectPretty(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, }, buf)) cmd.SetArgs([]string{"nodeID"}) diff --git a/cli/command/node/list_test.go b/cli/command/node/list_test.go index c4538ba141..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.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,7 +53,6 @@ func TestNodeListErrorOnAPIFailure(t *testing.T) { } func TestNodeList(t *testing.T) { - buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ @@ -71,25 +68,25 @@ 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) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ *Node(), }, nil }, }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) cmd := newListCommand(cli) cmd.Flags().Set("quiet", "true") assert.NoError(t, cmd.Execute()) @@ -99,8 +96,7 @@ func TestNodeListQuietShouldOnlyPrintIDs(t *testing.T) { // Test case for #24090 func TestNodeListContainsHostname(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{}, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) + cli := test.NewFakeCliWithOutput(&fakeClient{}, buf) cmd := newListCommand(cli) assert.NoError(t, cmd.Execute()) assert.Contains(t, buf.String(), "HOSTNAME") @@ -108,7 +104,7 @@ func TestNodeListContainsHostname(t *testing.T) { func TestNodeListDefaultFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), @@ -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) @@ -136,7 +132,7 @@ func TestNodeListDefaultFormat(t *testing.T) { func TestNodeListFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ nodeListFunc: func() ([]swarm.Node, error) { return []swarm.Node{ *Node(NodeID("nodeID1"), Hostname("nodeHostname1"), Manager(Leader())), @@ -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/node/promote_test.go b/cli/command/node/promote_test.go index 495bdfe60d..95e067eb15 100644 --- a/cli/command/node/promote_test.go +++ b/cli/command/node/promote_test.go @@ -42,7 +42,7 @@ func TestNodePromoteErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newPromoteCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: tc.nodeInspectFunc, nodeUpdateFunc: tc.nodeUpdateFunc, }, buf)) @@ -55,7 +55,7 @@ func TestNodePromoteErrors(t *testing.T) { func TestNodePromoteNoChange(t *testing.T) { buf := new(bytes.Buffer) cmd := newPromoteCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { return *Node(Manager()), []byte{}, nil }, @@ -73,7 +73,7 @@ func TestNodePromoteNoChange(t *testing.T) { func TestNodePromoteMultipleNode(t *testing.T) { buf := new(bytes.Buffer) cmd := newPromoteCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeInspectFunc: func() (swarm.Node, []byte, error) { return *Node(), []byte{}, nil }, diff --git a/cli/command/node/ps_test.go b/cli/command/node/ps_test.go index d25a55f0cf..d3ef6f30a2 100644 --- a/cli/command/node/ps_test.go +++ b/cli/command/node/ps_test.go @@ -52,7 +52,7 @@ func TestNodePsErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newPsCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ infoFunc: tc.infoFunc, nodeInspectFunc: tc.nodeInspectFunc, taskInspectFunc: tc.taskInspectFunc, @@ -116,7 +116,7 @@ func TestNodePs(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newPsCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ infoFunc: tc.infoFunc, nodeInspectFunc: tc.nodeInspectFunc, taskInspectFunc: tc.taskInspectFunc, diff --git a/cli/command/node/remove_test.go b/cli/command/node/remove_test.go index 7123288f47..81daa2f025 100644 --- a/cli/command/node/remove_test.go +++ b/cli/command/node/remove_test.go @@ -31,7 +31,7 @@ func TestNodeRemoveErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newRemoveCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ nodeRemoveFunc: tc.nodeRemoveFunc, }, buf)) cmd.SetArgs(tc.args) @@ -42,7 +42,7 @@ func TestNodeRemoveErrors(t *testing.T) { func TestNodeRemoveMultiple(t *testing.T) { buf := new(bytes.Buffer) - cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}, buf)) + cmd := newRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{}, 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 0085934b9c..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.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.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 f6adeb49f4..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.NewFakeCli(&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 197483cf70..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.NewFakeCli(&fakeClient{ secretCreateFunc: tc.secretCreateFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) @@ -57,7 +56,7 @@ func TestSecretCreateWithName(t *testing.T) { name := "foo" buf := new(bytes.Buffer) var actual []byte - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { if spec.Name != name { return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) @@ -87,7 +86,7 @@ func TestSecretCreateWithLabels(t *testing.T) { name := "foo" buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretCreateFunc: func(spec swarm.SecretSpec) (types.SecretCreateResponse, error) { if spec.Name != name { return types.SecretCreateResponse{}, errors.Errorf("expected name %q, got %q", name, spec.Name) diff --git a/cli/command/secret/inspect_test.go b/cli/command/secret/inspect_test.go index 07a3d7a313..093624ddfe 100644 --- a/cli/command/secret/inspect_test.go +++ b/cli/command/secret/inspect_test.go @@ -55,7 +55,7 @@ func TestSecretInspectErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretInspectFunc: tc.secretInspectFunc, }, buf), ) @@ -97,7 +97,7 @@ func TestSecretInspectWithoutFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretInspectFunc: tc.secretInspectFunc, }, buf), ) @@ -137,7 +137,7 @@ func TestSecretInspectWithFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretInspectFunc: tc.secretInspectFunc, }, buf), ) @@ -173,7 +173,7 @@ func TestSecretInspectPretty(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretInspectFunc: tc.secretInspectFunc, }, buf)) cmd.SetArgs([]string{"secretID"}) diff --git a/cli/command/secret/ls_test.go b/cli/command/secret/ls_test.go index 5d2103cb2a..fb033efea6 100644 --- a/cli/command/secret/ls_test.go +++ b/cli/command/secret/ls_test.go @@ -38,7 +38,7 @@ func TestSecretListErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretListCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: tc.secretListFunc, }, buf), ) @@ -50,7 +50,7 @@ func TestSecretListErrors(t *testing.T) { func TestSecretList(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), @@ -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()) @@ -79,7 +78,7 @@ func TestSecretList(t *testing.T) { func TestSecretListWithQuietOption(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), SecretName("foo")), @@ -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()) @@ -100,7 +98,7 @@ func TestSecretListWithQuietOption(t *testing.T) { func TestSecretListWithConfigFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), SecretName("foo")), @@ -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) @@ -122,7 +120,7 @@ func TestSecretListWithConfigFormat(t *testing.T) { func TestSecretListWithFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { return []swarm.Secret{ *Secret(SecretID("ID-foo"), SecretName("foo")), @@ -142,7 +140,7 @@ func TestSecretListWithFormat(t *testing.T) { func TestSecretListWithFilter(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretListFunc: func(options types.SecretListOptions) ([]swarm.Secret, error) { assert.Equal(t, "foo", options.Filters.Get("name")[0], "foo") assert.Equal(t, "lbl1=Label-bar", options.Filters.Get("label")[0]) @@ -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/secret/remove_test.go b/cli/command/secret/remove_test.go index 08443ec9ae..6e5ab5fb6c 100644 --- a/cli/command/secret/remove_test.go +++ b/cli/command/secret/remove_test.go @@ -33,7 +33,7 @@ func TestSecretRemoveErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newSecretRemoveCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ secretRemoveFunc: tc.secretRemoveFunc, }, buf), ) @@ -47,7 +47,7 @@ func TestSecretRemoveWithName(t *testing.T) { names := []string{"foo", "bar"} buf := new(bytes.Buffer) var removedSecrets []string - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretRemoveFunc: func(name string) error { removedSecrets = append(removedSecrets, name) return nil @@ -65,7 +65,7 @@ func TestSecretRemoveContinueAfterError(t *testing.T) { buf := new(bytes.Buffer) var removedSecrets []string - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ secretRemoveFunc: func(name string) error { removedSecrets = append(removedSecrets, name) if name == "foo" { diff --git a/cli/command/service/ps_test.go b/cli/command/service/ps_test.go index a53748d6d3..0e1bcb438b 100644 --- a/cli/command/service/ps_test.go +++ b/cli/command/service/ps_test.go @@ -107,7 +107,7 @@ func TestRunPSWarnsOnNotFound(t *testing.T) { } out := new(bytes.Buffer) - cli := test.NewFakeCli(client, out) + cli := test.NewFakeCliWithOutput(client, out) options := psOptions{ services: []string{"foo", "bar"}, filter: opts.NewFilterOpt(), diff --git a/cli/command/stack/deploy_test.go b/cli/command/stack/deploy_test.go index ffdd759699..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.NewFakeCli(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 4f258977ed..de1aa02365 100644 --- a/cli/command/stack/list_test.go +++ b/cli/command/stack/list_test.go @@ -50,7 +50,7 @@ func TestListErrors(t *testing.T) { for _, tc := range testCases { cmd := newListCommand(test.NewFakeCli(&fakeClient{ serviceListFunc: tc.serviceListFunc, - }, &bytes.Buffer{})) + })) cmd.SetArgs(tc.args) cmd.SetOutput(ioutil.Discard) for key, value := range tc.flags { @@ -62,7 +62,7 @@ func TestListErrors(t *testing.T) { func TestListWithFormat(t *testing.T) { buf := new(bytes.Buffer) - cmd := newListCommand(test.NewFakeCli(&fakeClient{ + cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service( @@ -81,7 +81,7 @@ func TestListWithFormat(t *testing.T) { func TestListWithoutFormat(t *testing.T) { buf := new(bytes.Buffer) - cmd := newListCommand(test.NewFakeCli(&fakeClient{ + cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service( @@ -99,7 +99,7 @@ func TestListWithoutFormat(t *testing.T) { func TestListOrder(t *testing.T) { buf := new(bytes.Buffer) - cmd := newListCommand(test.NewFakeCli(&fakeClient{ + cmd := newListCommand(test.NewFakeCliWithOutput(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service( diff --git a/cli/command/stack/ps_test.go b/cli/command/stack/ps_test.go index 7dc766609c..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" @@ -45,7 +44,7 @@ func TestStackPsErrors(t *testing.T) { for _, tc := range testCases { 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) @@ -53,60 +52,52 @@ func TestStackPsErrors(t *testing.T) { } func TestStackPsEmptyStack(t *testing.T) { - out := new(bytes.Buffer) - stderr := new(bytes.Buffer) fakeCli := test.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{}, nil }, - }, out) - fakeCli.SetErr(stderr) + }) cmd := newPsCommand(fakeCli) cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) - assert.Equal(t, "", out.String()) - assert.Equal(t, "Nothing found in stack: foo\n", stderr.String()) + assert.Equal(t, "", fakeCli.OutBuffer().String()) + assert.Equal(t, "Nothing found in stack: foo\n", fakeCli.ErrBuffer().String()) } func TestStackPsWithQuietOption(t *testing.T) { - buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { 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") 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.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { 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") 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.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task( @@ -116,55 +107,50 @@ func TestStackPsWithNoResolveOption(t *testing.T) { nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) { 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") 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.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { 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 }}") 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.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task(TaskServiceID("service-id-foo"))}, nil }, - }, buf) - cli.SetConfigfile(&configfile.ConfigFile{ + }) + 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.NewFakeCli(&fakeClient{ taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { return []swarm.Task{*Task( @@ -179,12 +165,11 @@ func TestStackPsWithoutFormat(t *testing.T) { nodeInspectWithRaw: func(ref string) (swarm.Node, []byte, error) { 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()) - 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 a924407af0..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.NewFakeCli(cli, &bytes.Buffer{})) + cmd := newRemoveCommand(test.NewFakeCli(cli)) cmd.SetArgs([]string{"foo", "bar"}) assert.NoError(t, cmd.Execute()) @@ -57,8 +56,6 @@ func TestRemoveStack(t *testing.T) { } func TestRemoveStackSkipEmpty(t *testing.T) { - out := new(bytes.Buffer) - stderr := new(bytes.Buffer) allServices := []string{objectName("bar", "service1"), objectName("bar", "service2")} allServiceIDs := buildObjectIDs(allServices) @@ -77,14 +74,13 @@ func TestRemoveStackSkipEmpty(t *testing.T) { secrets: allSecrets, configs: allConfigs, } - fakeCli := test.NewFakeCli(fakeClient, out) - fakeCli.SetErr(stderr) + fakeCli := test.NewFakeCli(fakeClient) cmd := newRemoveCommand(fakeCli) cmd.SetArgs([]string{"foo", "bar"}) assert.NoError(t, cmd.Execute()) - assert.Equal(t, "", out.String()) - assert.Contains(t, stderr.String(), "Nothing found in stack: foo\n") + assert.Equal(t, "", fakeCli.OutBuffer().String()) + assert.Contains(t, fakeCli.ErrBuffer().String(), "Nothing found in stack: foo\n") assert.Equal(t, allServiceIDs, fakeClient.removedServices) assert.Equal(t, allNetworkIDs, fakeClient.removedNetworks) assert.Equal(t, allSecretIDs, fakeClient.removedSecrets) @@ -120,7 +116,7 @@ func TestRemoveContinueAfterError(t *testing.T) { return nil }, } - cmd := newRemoveCommand(test.NewFakeCli(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 88408b4cd3..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" @@ -70,8 +69,7 @@ func TestStackServicesErrors(t *testing.T) { 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 { @@ -83,79 +81,70 @@ func TestStackServicesErrors(t *testing.T) { } func TestStackServicesEmptyServiceList(t *testing.T) { - out := new(bytes.Buffer) - stderr := new(bytes.Buffer) fakeCli := test.NewFakeCli(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{}, nil }, - }, out) - fakeCli.SetErr(stderr) + }) cmd := newServicesCommand(fakeCli) cmd.SetArgs([]string{"foo"}) assert.NoError(t, cmd.Execute()) - assert.Equal(t, "", out.String()) - assert.Equal(t, "Nothing found in stack: foo\n", stderr.String()) + assert.Equal(t, "", fakeCli.OutBuffer().String()) + assert.Equal(t, "Nothing found in stack: foo\n", fakeCli.ErrBuffer().String()) } func TestStackServicesWithQuietOption(t *testing.T) { - buf := new(bytes.Buffer) cli := test.NewFakeCli(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { 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"}) 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.NewFakeCli(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service(ServiceName("service-name-foo")), }, nil }, - }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) + }) 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.NewFakeCli(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{ *Service(ServiceName("service-name-foo")), }, nil }, - }, buf) - cli.SetConfigfile(&configfile.ConfigFile{ + }) + 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.NewFakeCli(&fakeClient{ serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{*Service( @@ -171,12 +160,11 @@ func TestStackServicesWithoutFormat(t *testing.T) { }), )}, nil }, - }, buf) - cli.SetConfigfile(&configfile.ConfigFile{}) + }) 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/init_test.go b/cli/command/swarm/init_test.go index 0fcdbb786d..f6969d1b0e 100644 --- a/cli/command/swarm/init_test.go +++ b/cli/command/swarm/init_test.go @@ -67,7 +67,7 @@ func TestSwarmInitErrorOnAPIFailure(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInitCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInitFunc: tc.swarmInitFunc, swarmInspectFunc: tc.swarmInspectFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, @@ -114,7 +114,7 @@ func TestSwarmInit(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInitCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInitFunc: tc.swarmInitFunc, swarmInspectFunc: tc.swarmInspectFunc, swarmGetUnlockKeyFunc: tc.swarmGetUnlockKeyFunc, diff --git a/cli/command/swarm/join_test.go b/cli/command/swarm/join_test.go index d4f1098ff9..331f2c1753 100644 --- a/cli/command/swarm/join_test.go +++ b/cli/command/swarm/join_test.go @@ -51,7 +51,7 @@ func TestSwarmJoinErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newJoinCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmJoinFunc: tc.swarmJoinFunc, infoFunc: tc.infoFunc, }, buf)) @@ -93,7 +93,7 @@ func TestSwarmJoin(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newJoinCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ infoFunc: tc.infoFunc, }, buf)) cmd.SetArgs([]string{"remote"}) diff --git a/cli/command/swarm/join_token_test.go b/cli/command/swarm/join_token_test.go index 5c095b4156..f8619a4bd6 100644 --- a/cli/command/swarm/join_token_test.go +++ b/cli/command/swarm/join_token_test.go @@ -92,7 +92,7 @@ func TestSwarmJoinTokenErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newJoinTokenCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInspectFunc: tc.swarmInspectFunc, swarmUpdateFunc: tc.swarmUpdateFunc, infoFunc: tc.infoFunc, @@ -200,7 +200,7 @@ func TestSwarmJoinToken(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newJoinTokenCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ swarmInspectFunc: tc.swarmInspectFunc, infoFunc: tc.infoFunc, nodeInspectFunc: tc.nodeInspectFunc, diff --git a/cli/command/swarm/leave_test.go b/cli/command/swarm/leave_test.go index dd7597efb5..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.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.NewFakeCli(&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 6b0246b199..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.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.NewFakeCli(&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 3dae5239f5..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.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,7 +78,6 @@ func TestSwarmUnlockErrors(t *testing.T) { func TestSwarmUnlock(t *testing.T) { input := "unlockKey" - buf := new(bytes.Buffer) dockerCli := test.NewFakeCli(&fakeClient{ infoFunc: func() (types.Info, error) { return types.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 a6372cfee9..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.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.NewFakeCli(&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/task/print_test.go b/cli/command/task/print_test.go index 93171016f2..174ad72cd7 100644 --- a/cli/command/task/print_test.go +++ b/cli/command/task/print_test.go @@ -24,7 +24,7 @@ func TestTaskPrintWithQuietOption(t *testing.T) { noResolve := true buf := new(bytes.Buffer) apiClient := &fakeClient{} - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task(TaskID("id-foo")), } @@ -41,7 +41,7 @@ func TestTaskPrintWithNoTruncOption(t *testing.T) { noResolve := true buf := new(bytes.Buffer) apiClient := &fakeClient{} - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task(TaskID("id-foo-yov6omdek8fg3k5stosyp2m50")), } @@ -58,7 +58,7 @@ func TestTaskPrintWithGlobalService(t *testing.T) { noResolve := true buf := new(bytes.Buffer) apiClient := &fakeClient{} - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task(TaskServiceID("service-id-foo"), TaskNodeID("node-id-bar"), TaskSlot(0)), } @@ -75,7 +75,7 @@ func TestTaskPrintWithReplicatedService(t *testing.T) { noResolve := true buf := new(bytes.Buffer) apiClient := &fakeClient{} - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task(TaskServiceID("service-id-foo"), TaskSlot(1)), } @@ -99,7 +99,7 @@ func TestTaskPrintWithIndentation(t *testing.T) { return *Node(NodeName("node-name-bar")), nil, nil }, } - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task( TaskID("id-foo"), @@ -138,7 +138,7 @@ func TestTaskPrintWithResolution(t *testing.T) { return *Node(NodeName("node-name-bar")), nil, nil }, } - cli := test.NewFakeCli(apiClient, buf) + cli := test.NewFakeCliWithOutput(apiClient, buf) tasks := []swarm.Task{ *Task(TaskServiceID("service-id-foo"), TaskSlot(1)), } diff --git a/cli/command/volume/create_test.go b/cli/command/volume/create_test.go index 74c81aba1f..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.NewFakeCli(&fakeClient{ volumeCreateFunc: tc.volumeCreateFunc, - }, buf), + }), ) cmd.SetArgs(tc.args) for key, value := range tc.flags { @@ -59,7 +58,7 @@ func TestVolumeCreateErrors(t *testing.T) { func TestVolumeCreateWithName(t *testing.T) { name := "foo" buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) { if body.Name != name { return types.Volume{}, errors.Errorf("expected name %q, got %q", name, body.Name) @@ -97,7 +96,7 @@ func TestVolumeCreateWithFlags(t *testing.T) { name := "banana" buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumeCreateFunc: func(body volumetypes.VolumesCreateBody) (types.Volume, error) { if body.Name != "" { return types.Volume{}, errors.Errorf("expected empty name, got %q", body.Name) diff --git a/cli/command/volume/inspect_test.go b/cli/command/volume/inspect_test.go index 3c16f125ab..8f452d7dee 100644 --- a/cli/command/volume/inspect_test.go +++ b/cli/command/volume/inspect_test.go @@ -56,7 +56,7 @@ func TestVolumeInspectErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeInspectFunc: tc.volumeInspectFunc, }, buf), ) @@ -98,7 +98,7 @@ func TestVolumeInspectWithoutFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeInspectFunc: tc.volumeInspectFunc, }, buf), ) @@ -138,7 +138,7 @@ func TestVolumeInspectWithFormat(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newInspectCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeInspectFunc: tc.volumeInspectFunc, }, buf), ) diff --git a/cli/command/volume/list_test.go b/cli/command/volume/list_test.go index 2dc11a8b8e..92cd62b597 100644 --- a/cli/command/volume/list_test.go +++ b/cli/command/volume/list_test.go @@ -39,7 +39,7 @@ func TestVolumeListErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newListCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeListFunc: tc.volumeListFunc, }, buf), ) @@ -54,7 +54,7 @@ func TestVolumeListErrors(t *testing.T) { func TestVolumeListWithoutFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { return volumetypes.VolumesListOKBody{ Volumes: []*types.Volume{ @@ -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() @@ -77,7 +76,7 @@ func TestVolumeListWithoutFormat(t *testing.T) { func TestVolumeListWithConfigFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { return volumetypes.VolumesListOKBody{ Volumes: []*types.Volume{ @@ -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) @@ -102,7 +101,7 @@ func TestVolumeListWithConfigFormat(t *testing.T) { func TestVolumeListWithFormat(t *testing.T) { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumeListFunc: func(filter filters.Args) (volumetypes.VolumesListOKBody, error) { return volumetypes.VolumesListOKBody{ Volumes: []*types.Volume{ @@ -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/command/volume/prune_test.go b/cli/command/volume/prune_test.go index 33a8d5dc19..cf26d283f4 100644 --- a/cli/command/volume/prune_test.go +++ b/cli/command/volume/prune_test.go @@ -41,7 +41,7 @@ func TestVolumePruneErrors(t *testing.T) { } for _, tc := range testCases { cmd := NewPruneCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumePruneFunc: tc.volumePruneFunc, }, ioutil.Discard), ) @@ -70,7 +70,7 @@ func TestVolumePruneForce(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := NewPruneCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumePruneFunc: tc.volumePruneFunc, }, buf), ) @@ -88,7 +88,7 @@ func TestVolumePrunePromptYes(t *testing.T) { } for _, input := range []string{"y", "Y"} { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumePruneFunc: simplePruneFunc, }, buf) @@ -110,7 +110,7 @@ func TestVolumePrunePromptNo(t *testing.T) { } for _, input := range []string{"n", "N", "no", "anything", "really"} { buf := new(bytes.Buffer) - cli := test.NewFakeCli(&fakeClient{ + cli := test.NewFakeCliWithOutput(&fakeClient{ volumePruneFunc: simplePruneFunc, }, buf) diff --git a/cli/command/volume/remove_test.go b/cli/command/volume/remove_test.go index 8916700cc6..3d01905681 100644 --- a/cli/command/volume/remove_test.go +++ b/cli/command/volume/remove_test.go @@ -31,7 +31,7 @@ func TestVolumeRemoveErrors(t *testing.T) { for _, tc := range testCases { buf := new(bytes.Buffer) cmd := newRemoveCommand( - test.NewFakeCli(&fakeClient{ + test.NewFakeCliWithOutput(&fakeClient{ volumeRemoveFunc: tc.volumeRemoveFunc, }, buf)) cmd.SetArgs(tc.args) @@ -42,7 +42,7 @@ func TestVolumeRemoveErrors(t *testing.T) { func TestNodeRemoveMultiple(t *testing.T) { buf := new(bytes.Buffer) - cmd := newRemoveCommand(test.NewFakeCli(&fakeClient{}, buf)) + cmd := newRemoveCommand(test.NewFakeCliWithOutput(&fakeClient{}, buf)) cmd.SetArgs([]string{"volume1", "volume2"}) assert.NoError(t, cmd.Execute()) } diff --git a/cli/internal/test/cli.go b/cli/internal/test/cli.go index 378ee9cdc2..54b3bc83b1 100644 --- a/cli/internal/test/cli.go +++ b/cli/internal/test/cli.go @@ -1,6 +1,7 @@ package test import ( + "bytes" "io" "io/ioutil" "strings" @@ -16,17 +17,29 @@ type FakeCli struct { client client.APIClient configfile *configfile.ConfigFile out *command.OutStream - err io.Writer + outBuffer *bytes.Buffer + err *bytes.Buffer in *command.InStream server command.ServerInfo } -// NewFakeCli returns a Cli backed by the fakeCli -func NewFakeCli(client client.APIClient, out io.Writer) *FakeCli { +// NewFakeCliWithOutput returns a Cli backed by the fakeCli +// Deprecated: Use NewFakeCli +func NewFakeCliWithOutput(client client.APIClient, out io.Writer) *FakeCli { + cli := NewFakeCli(client) + cli.out = command.NewOutStream(out) + return cli +} + +// NewFakeCli returns a fake for the command.Cli interface +func NewFakeCli(client client.APIClient) *FakeCli { + outBuffer := new(bytes.Buffer) + errBuffer := new(bytes.Buffer) return &FakeCli{ client: client, - out: command.NewOutStream(out), - err: ioutil.Discard, + out: command.NewOutStream(outBuffer), + outBuffer: outBuffer, + err: errBuffer, in: command.NewInStream(ioutil.NopCloser(strings.NewReader(""))), configfile: configfile.New("configfile"), } @@ -38,12 +51,12 @@ func (c *FakeCli) SetIn(in *command.InStream) { } // SetErr sets the stderr stream for the cli to the specified io.Writer -func (c *FakeCli) SetErr(err io.Writer) { +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 } @@ -76,3 +89,13 @@ func (c *FakeCli) ConfigFile() *configfile.ConfigFile { func (c *FakeCli) ServerInfo() command.ServerInfo { return c.server } + +// OutBuffer returns the stdout buffer +func (c *FakeCli) OutBuffer() *bytes.Buffer { + return c.outBuffer +} + +// ErrBuffer Buffer returns the stderr buffer +func (c *FakeCli) ErrBuffer() *bytes.Buffer { + return c.err +}