88
99 "github.com/docker/docker/api/types/container"
1010 "github.com/docker/docker/api/types/filters"
11+ "github.com/docker/docker/api/types/image"
1112 "github.com/docker/docker/client"
1213 "github.com/google/go-cmp/cmp/cmpopts"
1314 "github.com/spf13/cobra"
@@ -28,6 +29,7 @@ func (c fakeCLI) Client() client.APIClient {
2829type fakeClient struct {
2930 client.Client
3031 containerListFunc func (options container.ListOptions ) ([]container.Summary , error )
32+ imageListFunc func (options image.ListOptions ) ([]image.Summary , error )
3133}
3234
3335func (c * fakeClient ) ContainerList (_ context.Context , options container.ListOptions ) ([]container.Summary , error ) {
@@ -37,6 +39,13 @@ func (c *fakeClient) ContainerList(_ context.Context, options container.ListOpti
3739 return []container.Summary {}, nil
3840}
3941
42+ func (c * fakeClient ) ImageList (_ context.Context , options image.ListOptions ) ([]image.Summary , error ) {
43+ if c .imageListFunc != nil {
44+ return c .imageListFunc (options )
45+ }
46+ return []image.Summary {}, nil
47+ }
48+
4049func TestCompleteContainerNames (t * testing.T ) {
4150 tests := []struct {
4251 doc string
@@ -172,6 +181,52 @@ func TestCompleteFromList(t *testing.T) {
172181 assert .Check (t , is .DeepEqual (values , expected ))
173182}
174183
184+ func TestCompleteImageNames (t * testing.T ) {
185+ tests := []struct {
186+ doc string
187+ images []image.Summary
188+ expOut []string
189+ expDirective cobra.ShellCompDirective
190+ }{
191+ {
192+ doc : "no results" ,
193+ expDirective : cobra .ShellCompDirectiveNoFileComp ,
194+ },
195+ {
196+ doc : "with results" ,
197+ images : []image.Summary {
198+ {RepoTags : []string {"image-c:latest" , "image-c:other" }},
199+ {RepoTags : []string {"image-b:latest" , "image-b:other" }},
200+ {RepoTags : []string {"image-a:latest" , "image-a:other" }},
201+ },
202+ expOut : []string {"image-c:latest" , "image-c:other" , "image-b:latest" , "image-b:other" , "image-a:latest" , "image-a:other" },
203+ expDirective : cobra .ShellCompDirectiveNoFileComp ,
204+ },
205+ {
206+ doc : "with error" ,
207+ expDirective : cobra .ShellCompDirectiveError ,
208+ },
209+ }
210+
211+ for _ , tc := range tests {
212+ tc := tc
213+ t .Run (tc .doc , func (t * testing.T ) {
214+ comp := ImageNames (fakeCLI {& fakeClient {
215+ imageListFunc : func (options image.ListOptions ) ([]image.Summary , error ) {
216+ if tc .expDirective == cobra .ShellCompDirectiveError {
217+ return nil , errors .New ("some error occurred" )
218+ }
219+ return tc .images , nil
220+ },
221+ }})
222+
223+ volumes , directives := comp (& cobra.Command {}, nil , "" )
224+ assert .Check (t , is .Equal (directives & tc .expDirective , tc .expDirective ))
225+ assert .Check (t , is .DeepEqual (volumes , tc .expOut ))
226+ })
227+ }
228+ }
229+
175230func TestCompleteNoComplete (t * testing.T ) {
176231 values , directives := NoComplete (nil , nil , "" )
177232 assert .Check (t , is .Equal (directives , cobra .ShellCompDirectiveNoFileComp ))
0 commit comments