Skip to content

Commit 8a7c5ae

Browse files
authored
Merge pull request #5542 from thaJeztah/base_completion_tests
cmd/docker: add tests for flag-completions, and refactor
2 parents da9e984 + 670f818 commit 8a7c5ae

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

cmd/docker/completions.go

+7-11
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

+49
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

+5-1
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)