From 77d002ae25ec2020463d2adafeafb588a41e5d87 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 13 Oct 2024 18:00:20 +0200 Subject: [PATCH] completion: add test for ImageNames Signed-off-by: Sebastiaan van Stijn (cherry picked from commit ab418a38d826d7137802840d2b406eead9d82cdb) Signed-off-by: Sebastiaan van Stijn --- cli/command/completion/functions_test.go | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/cli/command/completion/functions_test.go b/cli/command/completion/functions_test.go index 943ae49169..dde30d2f66 100644 --- a/cli/command/completion/functions_test.go +++ b/cli/command/completion/functions_test.go @@ -9,6 +9,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/filters" + "github.com/docker/docker/api/types/image" "github.com/docker/docker/client" "github.com/google/go-cmp/cmp/cmpopts" "github.com/spf13/cobra" @@ -29,6 +30,7 @@ func (c fakeCLI) Client() client.APIClient { type fakeClient struct { client.Client containerListFunc func(options container.ListOptions) ([]types.Container, error) + imageListFunc func(options image.ListOptions) ([]image.Summary, error) } func (c *fakeClient) ContainerList(_ context.Context, options container.ListOptions) ([]types.Container, error) { @@ -38,6 +40,13 @@ func (c *fakeClient) ContainerList(_ context.Context, options container.ListOpti return []types.Container{}, nil } +func (c *fakeClient) ImageList(_ context.Context, options image.ListOptions) ([]image.Summary, error) { + if c.imageListFunc != nil { + return c.imageListFunc(options) + } + return []image.Summary{}, nil +} + func TestCompleteContainerNames(t *testing.T) { tests := []struct { doc string @@ -173,6 +182,52 @@ func TestCompleteFromList(t *testing.T) { assert.Check(t, is.DeepEqual(values, expected)) } +func TestCompleteImageNames(t *testing.T) { + tests := []struct { + doc string + images []image.Summary + expOut []string + expDirective cobra.ShellCompDirective + }{ + { + doc: "no results", + expDirective: cobra.ShellCompDirectiveNoFileComp, + }, + { + doc: "with results", + images: []image.Summary{ + {RepoTags: []string{"image-c:latest", "image-c:other"}}, + {RepoTags: []string{"image-b:latest", "image-b:other"}}, + {RepoTags: []string{"image-a:latest", "image-a:other"}}, + }, + expOut: []string{"image-c:latest", "image-c:other", "image-b:latest", "image-b:other", "image-a:latest", "image-a:other"}, + expDirective: cobra.ShellCompDirectiveNoFileComp, + }, + { + doc: "with error", + expDirective: cobra.ShellCompDirectiveError, + }, + } + + for _, tc := range tests { + tc := tc + t.Run(tc.doc, func(t *testing.T) { + comp := ImageNames(fakeCLI{&fakeClient{ + imageListFunc: func(options image.ListOptions) ([]image.Summary, error) { + if tc.expDirective == cobra.ShellCompDirectiveError { + return nil, errors.New("some error occurred") + } + return tc.images, nil + }, + }}) + + volumes, directives := comp(&cobra.Command{}, nil, "") + assert.Check(t, is.Equal(directives&tc.expDirective, tc.expDirective)) + assert.Check(t, is.DeepEqual(volumes, tc.expOut)) + }) + } +} + func TestCompleteNoComplete(t *testing.T) { values, directives := NoComplete(nil, nil, "") assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))