Skip to content

Commit 670f818

Browse files
committed
cmd/docker: add tests for flag-completions, and refactor
Remove the registerCompletionFuncForGlobalFlags for now, as the error it returned was ignored, so it didn't add much benefit, other than abstracting things. Split the underlying completion-functions to separate functions, and add some basic tests for them. Remove the completions helper, as it now didn't add much, and it saved having the dependency on the package. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 3865327 commit 670f818

3 files changed

Lines changed: 61 additions & 12 deletions

File tree

cmd/docker/completions.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"github.com/docker/cli/cli/command/completion"
54
"github.com/docker/cli/cli/context/store"
65
"github.com/spf13/cobra"
76
)
@@ -10,18 +9,15 @@ type contextStoreProvider interface {
109
ContextStore() store.Store
1110
}
1211

13-
func registerCompletionFuncForGlobalFlags(dockerCLI contextStoreProvider, cmd *cobra.Command) error {
14-
err := cmd.RegisterFlagCompletionFunc("context", func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
12+
func completeContextNames(dockerCLI contextStoreProvider) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
13+
return func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
1514
names, _ := store.Names(dockerCLI.ContextStore())
1615
return names, cobra.ShellCompDirectiveNoFileComp
17-
})
18-
if err != nil {
19-
return err
20-
}
21-
err = cmd.RegisterFlagCompletionFunc("log-level", completion.FromList("debug", "info", "warn", "error", "fatal"))
22-
if err != nil {
23-
return err
2416
}
17+
}
18+
19+
var logLevels = []string{"debug", "info", "warn", "error", "fatal", "panic"}
2520

26-
return nil
21+
func completeLogLevels(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
22+
return cobra.FixedCompletions(logLevels, cobra.ShellCompDirectiveNoFileComp)(nil, nil, "")
2723
}

cmd/docker/completions_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/cli/cli/context/store"
7+
"github.com/spf13/cobra"
8+
"gotest.tools/v3/assert"
9+
is "gotest.tools/v3/assert/cmp"
10+
)
11+
12+
type fakeCLI struct {
13+
contextStore store.Store
14+
}
15+
16+
func (c *fakeCLI) ContextStore() store.Store {
17+
return c.contextStore
18+
}
19+
20+
type fakeContextStore struct {
21+
store.Store
22+
names []string
23+
}
24+
25+
func (f fakeContextStore) List() (c []store.Metadata, _ error) {
26+
for _, name := range f.names {
27+
c = append(c, store.Metadata{Name: name})
28+
}
29+
return c, nil
30+
}
31+
32+
func TestCompleteContextNames(t *testing.T) {
33+
expectedNames := []string{"context-b", "context-c", "context-a"}
34+
cli := &fakeCLI{
35+
contextStore: fakeContextStore{
36+
names: expectedNames,
37+
},
38+
}
39+
40+
values, directives := completeContextNames(cli)(nil, nil, "")
41+
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))
42+
assert.Check(t, is.DeepEqual(values, expectedNames))
43+
}
44+
45+
func TestCompleteLogLevels(t *testing.T) {
46+
values, directives := completeLogLevels(nil, nil, "")
47+
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))
48+
assert.Check(t, is.DeepEqual(values, logLevels))
49+
}

cmd/docker/docker.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ func newDockerCommand(dockerCli *command.DockerCli) *cli.TopLevelCommand {
100100
cmd.SetErr(dockerCli.Err())
101101

102102
opts, helpCmd = cli.SetupRootCommand(cmd)
103-
_ = registerCompletionFuncForGlobalFlags(dockerCli, cmd)
103+
104+
// TODO(thaJeztah): move configuring completion for these flags to where the flags are added.
105+
_ = cmd.RegisterFlagCompletionFunc("context", completeContextNames(dockerCli))
106+
_ = cmd.RegisterFlagCompletionFunc("log-level", completeLogLevels)
107+
104108
cmd.Flags().BoolP("version", "v", false, "Print version information and quit")
105109
setFlagErrorFunc(dockerCli, cmd)
106110

0 commit comments

Comments
 (0)