MCPcopy Index your code
hub / github.com/docker/cli / TestCompleteContextNames

Function TestCompleteContextNames

cli/command/context/completion_test.go:36–80  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

34}
35
36func TestCompleteContextNames(t *testing.T) {
37 allNames := []string{"context-b", "context-c", "context-a"}
38 cli := &fakeContextProvider{
39 contextStore: fakeContextStore{
40 names: allNames,
41 },
42 }
43
44 t.Run("with limit", func(t *testing.T) {
45 compFunc := completeContextNames(cli, 1, false)
46 values, directives := compFunc(nil, nil, "")
47 assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))
48 assert.Check(t, is.DeepEqual(values, allNames))
49
50 values, directives = compFunc(nil, []string{"context-c"}, "")
51 assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))
52 assert.Check(t, is.Len(values, 0))
53 })
54
55 t.Run("with limit and file completion", func(t *testing.T) {
56 compFunc := completeContextNames(cli, 1, true)
57 values, directives := compFunc(nil, nil, "")
58 assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))
59 assert.Check(t, is.DeepEqual(values, allNames))
60
61 values, directives = compFunc(nil, []string{"context-c"}, "")
62 assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveDefault), "should provide filenames completion after limit")
63 assert.Check(t, is.Len(values, 0))
64 })
65
66 t.Run("without limits", func(t *testing.T) {
67 compFunc := completeContextNames(cli, -1, false)
68 values, directives := compFunc(nil, []string{"context-c"}, "")
69 assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))
70 assert.Check(t, is.DeepEqual(values, []string{"context-b", "context-a"}), "should not contain already completed")
71
72 values, directives = compFunc(nil, []string{"context-c", "context-a"}, "")
73 assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))
74 assert.Check(t, is.DeepEqual(values, []string{"context-b"}), "should not contain already completed")
75
76 values, directives = compFunc(nil, []string{"context-c", "context-a", "context-b"}, "")
77 assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp), "should provide filenames completion after limit")
78 assert.Check(t, is.Len(values, 0))
79 })
80}

Callers

nothing calls this directly

Calls 2

completeContextNamesFunction · 0.70
LenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…