Skip to content

Commit 181575b

Browse files
authored
Merge pull request #4888 from Benehiko/fix-prompt-termination
fix: cli prompt termination exit code
2 parents 310daf2 + 10bf91a commit 181575b

33 files changed

+735
-59
lines changed

cli/command/builder/client_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package builder
2+
3+
import (
4+
"context"
5+
6+
"github.com/docker/docker/api/types"
7+
"github.com/docker/docker/client"
8+
)
9+
10+
type fakeClient struct {
11+
client.Client
12+
builderPruneFunc func(ctx context.Context, opts types.BuildCachePruneOptions) (*types.BuildCachePruneReport, error)
13+
}
14+
15+
func (c *fakeClient) BuildCachePrune(ctx context.Context, opts types.BuildCachePruneOptions) (*types.BuildCachePruneReport, error) {
16+
if c.builderPruneFunc != nil {
17+
return c.builderPruneFunc(ctx, opts)
18+
}
19+
return nil, nil
20+
}

cli/command/builder/prune.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
6666
if options.all {
6767
warning = allCacheWarning
6868
}
69-
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
70-
return 0, "", nil
69+
if !options.force {
70+
if r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), warning); !r || err != nil {
71+
return 0, "", err
72+
}
7173
}
7274

7375
report, err := dockerCli.Client().BuildCachePrune(ctx, types.BuildCachePruneOptions{

cli/command/builder/prune_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package builder
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
8+
"github.com/docker/cli/cli/command"
9+
"github.com/docker/cli/internal/test"
10+
"github.com/docker/docker/api/types"
11+
"gotest.tools/v3/assert"
12+
)
13+
14+
func TestBuilderPromptTermination(t *testing.T) {
15+
ctx, cancel := context.WithCancel(context.Background())
16+
t.Cleanup(cancel)
17+
18+
cli := test.NewFakeCli(&fakeClient{
19+
builderPruneFunc: func(ctx context.Context, opts types.BuildCachePruneOptions) (*types.BuildCachePruneReport, error) {
20+
return nil, errors.New("fakeClient builderPruneFunc should not be called")
21+
},
22+
})
23+
cmd := NewPruneCommand(cli)
24+
test.TerminatePrompt(ctx, t, cmd, cli, func(t *testing.T, err error) {
25+
t.Helper()
26+
assert.ErrorIs(t, err, command.ErrPromptTerminated)
27+
})
28+
}

cli/command/container/client_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/docker/docker/api/types"
88
"github.com/docker/docker/api/types/container"
9+
"github.com/docker/docker/api/types/filters"
910
"github.com/docker/docker/api/types/image"
1011
"github.com/docker/docker/api/types/network"
1112
"github.com/docker/docker/api/types/system"
@@ -35,6 +36,7 @@ type fakeClient struct {
3536
containerExecResizeFunc func(id string, options container.ResizeOptions) error
3637
containerRemoveFunc func(ctx context.Context, containerID string, options container.RemoveOptions) error
3738
containerKillFunc func(ctx context.Context, containerID, signal string) error
39+
containerPruneFunc func(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error)
3840
Version string
3941
}
4042

@@ -164,3 +166,10 @@ func (f *fakeClient) ContainerKill(ctx context.Context, containerID, signal stri
164166
}
165167
return nil
166168
}
169+
170+
func (f *fakeClient) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error) {
171+
if f.containerPruneFunc != nil {
172+
return f.containerPruneFunc(ctx, pruneFilters)
173+
}
174+
return types.ContainersPruneReport{}, nil
175+
}

cli/command/container/prune.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ Are you sure you want to continue?`
5353
func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions) (spaceReclaimed uint64, output string, err error) {
5454
pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
5555

56-
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
57-
return 0, "", nil
56+
if !options.force {
57+
if r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), warning); !r || err != nil {
58+
return 0, "", err
59+
}
5860
}
5961

6062
report, err := dockerCli.Client().ContainersPrune(ctx, pruneFilters)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package container
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/docker/cli/cli/command"
8+
"github.com/docker/cli/internal/test"
9+
"github.com/docker/docker/api/types"
10+
"github.com/docker/docker/api/types/filters"
11+
"github.com/pkg/errors"
12+
"gotest.tools/v3/assert"
13+
)
14+
15+
func TestContainerPrunePromptTermination(t *testing.T) {
16+
ctx, cancel := context.WithCancel(context.Background())
17+
t.Cleanup(cancel)
18+
19+
cli := test.NewFakeCli(&fakeClient{
20+
containerPruneFunc: func(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error) {
21+
return types.ContainersPruneReport{}, errors.New("fakeClient containerPruneFunc should not be called")
22+
},
23+
})
24+
cmd := NewPruneCommand(cli)
25+
test.TerminatePrompt(ctx, t, cmd, cli, func(t *testing.T, err error) {
26+
t.Helper()
27+
assert.ErrorIs(t, err, command.ErrPromptTerminated)
28+
})
29+
}

cli/command/image/prune.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
6767
if options.all {
6868
warning = allImageWarning
6969
}
70-
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
71-
return 0, "", nil
70+
if !options.force {
71+
if r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), warning); !r || err != nil {
72+
return 0, "", err
73+
}
7274
}
7375

7476
report, err := dockerCli.Client().ImagesPrune(ctx, pruneFilters)

cli/command/image/prune_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package image
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"testing"
78

9+
"github.com/docker/cli/cli/command"
810
"github.com/docker/cli/internal/test"
911
"github.com/docker/docker/api/types"
1012
"github.com/docker/docker/api/types/filters"
@@ -101,3 +103,19 @@ func TestNewPruneCommandSuccess(t *testing.T) {
101103
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("prune-command-success.%s.golden", tc.name))
102104
}
103105
}
106+
107+
func TestPrunePromptTermination(t *testing.T) {
108+
ctx, cancel := context.WithCancel(context.Background())
109+
t.Cleanup(cancel)
110+
111+
cli := test.NewFakeCli(&fakeClient{
112+
imagesPruneFunc: func(pruneFilter filters.Args) (types.ImagesPruneReport, error) {
113+
return types.ImagesPruneReport{}, errors.New("fakeClient imagesPruneFunc should not be called")
114+
},
115+
})
116+
cmd := NewPruneCommand(cli)
117+
test.TerminatePrompt(ctx, t, cmd, cli, func(t *testing.T, err error) {
118+
t.Helper()
119+
assert.ErrorIs(t, err, command.ErrPromptTerminated)
120+
})
121+
}

cli/command/network/client_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/docker/docker/api/types"
7+
"github.com/docker/docker/api/types/filters"
78
"github.com/docker/docker/api/types/network"
89
"github.com/docker/docker/client"
910
)
@@ -15,6 +16,8 @@ type fakeClient struct {
1516
networkDisconnectFunc func(ctx context.Context, networkID, container string, force bool) error
1617
networkRemoveFunc func(ctx context.Context, networkID string) error
1718
networkListFunc func(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error)
19+
networkPruneFunc func(ctx context.Context, pruneFilters filters.Args) (types.NetworksPruneReport, error)
20+
networkInspectFunc func(ctx context.Context, networkID string, options types.NetworkInspectOptions) (types.NetworkResource, []byte, error)
1821
}
1922

2023
func (c *fakeClient) NetworkCreate(ctx context.Context, name string, options types.NetworkCreate) (types.NetworkCreateResponse, error) {
@@ -52,6 +55,16 @@ func (c *fakeClient) NetworkRemove(ctx context.Context, networkID string) error
5255
return nil
5356
}
5457

55-
func (c *fakeClient) NetworkInspectWithRaw(context.Context, string, types.NetworkInspectOptions) (types.NetworkResource, []byte, error) {
58+
func (c *fakeClient) NetworkInspectWithRaw(ctx context.Context, networkID string, opts types.NetworkInspectOptions) (types.NetworkResource, []byte, error) {
59+
if c.networkInspectFunc != nil {
60+
return c.networkInspectFunc(ctx, networkID, opts)
61+
}
5662
return types.NetworkResource{}, nil, nil
5763
}
64+
65+
func (c *fakeClient) NetworksPrune(ctx context.Context, pruneFilter filters.Args) (types.NetworksPruneReport, error) {
66+
if c.networkPruneFunc != nil {
67+
return c.networkPruneFunc(ctx, pruneFilter)
68+
}
69+
return types.NetworksPruneReport{}, nil
70+
}

cli/command/network/prune.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ Are you sure you want to continue?`
4949
func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions) (output string, err error) {
5050
pruneFilters := command.PruneFilters(dockerCli, options.filter.Value())
5151

52-
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), warning) {
53-
return "", nil
52+
if !options.force {
53+
if r, err := command.PromptForConfirmation(ctx, dockerCli.In(), dockerCli.Out(), warning); !r || err != nil {
54+
return "", err
55+
}
5456
}
5557

5658
report, err := dockerCli.Client().NetworksPrune(ctx, pruneFilters)

0 commit comments

Comments
 (0)