diff --git a/cli/command/volume/prune.go b/cli/command/volume/prune.go index f86cf56f9c..0756f8fbb7 100644 --- a/cli/command/volume/prune.go +++ b/cli/command/volume/prune.go @@ -32,6 +32,10 @@ func NewPruneCommand(dockerCli command.Cli) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { spaceReclaimed, output, err := runPrune(cmd.Context(), dockerCli, options) if err != nil { + if errdefs.IsCancelled(err) { + fmt.Fprintln(dockerCli.Out(), output) + return nil + } return err } if output != "" { @@ -77,7 +81,7 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions) warning = allVolumesWarning } if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) { - return 0, "", nil + return 0, "", errdefs.Cancelled(errors.New("user cancelled operation")) } report, err := dockerCli.Client().VolumesPrune(ctx, pruneFilters) diff --git a/cli/command/volume/prune_test.go b/cli/command/volume/prune_test.go index 5a0a2ba5b3..ec0b188894 100644 --- a/cli/command/volume/prune_test.go +++ b/cli/command/volume/prune_test.go @@ -73,13 +73,15 @@ func TestVolumePruneSuccess(t *testing.T) { testCases := []struct { name string args []string + input string volumePruneFunc func(args filters.Args) (types.VolumesPruneReport, error) }{ { - name: "all", - args: []string{"--all"}, + name: "all", + args: []string{"--all"}, + input: "y", volumePruneFunc: func(pruneFilter filters.Args) (types.VolumesPruneReport, error) { - assert.Check(t, is.Equal([]string{"true"}, pruneFilter.Get("all"))) + assert.Check(t, is.DeepEqual([]string{"true"}, pruneFilter.Get("all"))) return types.VolumesPruneReport{}, nil }, }, @@ -91,10 +93,11 @@ func TestVolumePruneSuccess(t *testing.T) { }, }, { - name: "label-filter", - args: []string{"--filter", "label=foobar"}, + name: "label-filter", + args: []string{"--filter", "label=foobar"}, + input: "y", volumePruneFunc: func(pruneFilter filters.Args) (types.VolumesPruneReport, error) { - assert.Check(t, is.Equal([]string{"foobar"}, pruneFilter.Get("label"))) + assert.Check(t, is.DeepEqual([]string{"foobar"}, pruneFilter.Get("label"))) return types.VolumesPruneReport{}, nil }, }, @@ -104,6 +107,9 @@ func TestVolumePruneSuccess(t *testing.T) { t.Run(tc.name, func(t *testing.T) { cli := test.NewFakeCli(&fakeClient{volumePruneFunc: tc.volumePruneFunc}) cmd := NewPruneCommand(cli) + if tc.input != "" { + cli.SetIn(streams.NewIn(io.NopCloser(strings.NewReader(tc.input)))) + } cmd.SetOut(io.Discard) cmd.SetArgs(tc.args) err := cmd.Execute() diff --git a/cli/command/volume/testdata/volume-prune-no.golden b/cli/command/volume/testdata/volume-prune-no.golden index 5ae0766b00..8e918f9c9e 100644 --- a/cli/command/volume/testdata/volume-prune-no.golden +++ b/cli/command/volume/testdata/volume-prune-no.golden @@ -1,2 +1,2 @@ WARNING! This will remove anonymous local volumes not used by at least one container. -Are you sure you want to continue? [y/N] Total reclaimed space: 0B +Are you sure you want to continue? [y/N]