completion: add test for ImageNames

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit ab418a38d8)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-10-13 18:00:20 +02:00
parent 95e329d3e3
commit 77d002ae25
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 55 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/google/go-cmp/cmp/cmpopts" "github.com/google/go-cmp/cmp/cmpopts"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -29,6 +30,7 @@ func (c fakeCLI) Client() client.APIClient {
type fakeClient struct { type fakeClient struct {
client.Client client.Client
containerListFunc func(options container.ListOptions) ([]types.Container, error) 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) { 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 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) { func TestCompleteContainerNames(t *testing.T) {
tests := []struct { tests := []struct {
doc string doc string
@ -173,6 +182,52 @@ func TestCompleteFromList(t *testing.T) {
assert.Check(t, is.DeepEqual(values, expected)) 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) { func TestCompleteNoComplete(t *testing.T) {
values, directives := NoComplete(nil, nil, "") values, directives := NoComplete(nil, nil, "")
assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp)) assert.Check(t, is.Equal(directives, cobra.ShellCompDirectiveNoFileComp))