|
| 1 | +package context |
| 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 fakeContextProvider struct { |
| 13 | + contextStore store.Store |
| 14 | +} |
| 15 | + |
| 16 | +func (c *fakeContextProvider) ContextStore() store.Store { |
| 17 | + return c.contextStore |
| 18 | +} |
| 19 | + |
| 20 | +func (*fakeContextProvider) CurrentContext() string { |
| 21 | + return "default" |
| 22 | +} |
| 23 | + |
| 24 | +type fakeContextStore struct { |
| 25 | + store.Store |
| 26 | + names []string |
| 27 | +} |
| 28 | + |
| 29 | +func (f fakeContextStore) List() (c []store.Metadata, _ error) { |
| 30 | + for _, name := range f.names { |
| 31 | + c = append(c, store.Metadata{Name: name}) |
| 32 | + } |
| 33 | + return c, nil |
| 34 | +} |
| 35 | + |
| 36 | +func 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 | +} |
0 commit comments