diff --git a/cli/command/image/remove.go b/cli/command/image/remove.go index 0a4bef3caf..894d30574b 100644 --- a/cli/command/image/remove.go +++ b/cli/command/image/remove.go @@ -59,9 +59,6 @@ func runRemove(dockerCli command.Cli, opts removeOptions, images []string) error for _, img := range images { dels, err := client.ImageRemove(ctx, img, options) if err != nil { - if opts.force { - fmt.Fprintf(dockerCli.Out(), "NotFound: %s\n", img) - } errs = append(errs, err.Error()) } else { for _, del := range dels { @@ -74,8 +71,12 @@ func runRemove(dockerCli command.Cli, opts removeOptions, images []string) error } } - if !opts.force && len(errs) > 0 { - return errors.Errorf("%s", strings.Join(errs, "\n")) + if len(errs) > 0 { + msg := strings.Join(errs, "\n") + if !opts.force { + return errors.New(msg) + } + fmt.Fprintf(dockerCli.Err(), msg) } return nil } diff --git a/cli/command/image/remove_test.go b/cli/command/image/remove_test.go index 4420b29d4b..9b02b24a90 100644 --- a/cli/command/image/remove_test.go +++ b/cli/command/image/remove_test.go @@ -58,6 +58,7 @@ func TestNewRemoveCommandSuccess(t *testing.T) { name string args []string imageRemoveFunc func(image string, options types.ImageRemoveOptions) ([]types.ImageDeleteResponseItem, error) + expectedErrMsg string }{ { name: "Image Deleted", @@ -74,6 +75,7 @@ func TestNewRemoveCommandSuccess(t *testing.T) { assert.Equal(t, "image1", image) return []types.ImageDeleteResponseItem{}, errors.Errorf("error removing image") }, + expectedErrMsg: "error removing image", }, { name: "Image Untagged", @@ -96,12 +98,16 @@ func TestNewRemoveCommandSuccess(t *testing.T) { } for _, tc := range testCases { buf := new(bytes.Buffer) - cmd := NewRemoveCommand(test.NewFakeCli(&fakeClient{ - imageRemoveFunc: tc.imageRemoveFunc, - }, buf)) + errBuf := new(bytes.Buffer) + fakeCli := test.NewFakeCli(&fakeClient{imageRemoveFunc: tc.imageRemoveFunc}, buf) + fakeCli.SetErr(errBuf) + 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()) + } actual := buf.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/testdata/remove-command-success.Image Deleted with force option.golden b/cli/command/image/testdata/remove-command-success.Image Deleted with force option.golden index 0d3cae73f7..e69de29bb2 100644 --- a/cli/command/image/testdata/remove-command-success.Image Deleted with force option.golden +++ b/cli/command/image/testdata/remove-command-success.Image Deleted with force option.golden @@ -1 +0,0 @@ -NotFound:image1 \ No newline at end of file