mirror of https://github.com/docker/cli.git
Parametrized existing completion tests
Signed-off-by: Harald Albers <github@albersweb.de>
This commit is contained in:
parent
3517b1bf87
commit
e03af58525
|
@ -3,6 +3,7 @@ package system
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/docker/api/types/system"
|
||||
|
@ -16,98 +17,83 @@ import (
|
|||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
// Successful completion lists all container names, prefixed with "container=".
|
||||
// Filtering the completions by the current word is delegated to the completion script.
|
||||
func TestCompleteEventFilterContainer(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) {
|
||||
return []container.Summary{
|
||||
*builders.Container("c1"),
|
||||
*builders.Container("c2"),
|
||||
}, nil
|
||||
func TestCompleteEventFilter(t *testing.T) {
|
||||
tests := []struct {
|
||||
client *fakeClient
|
||||
toComplete string
|
||||
expected []string
|
||||
}{
|
||||
{
|
||||
client: &fakeClient{
|
||||
containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) {
|
||||
return []container.Summary{
|
||||
*builders.Container("c1"),
|
||||
*builders.Container("c2"),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
toComplete: "container=",
|
||||
expected: []string{"container=c1", "container=c2"},
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "container=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{"container=c1", "container=c2"})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
||||
|
||||
// In case of API errors, no completions are returned.
|
||||
func TestCompleteEventFilterContainerAPIError(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) {
|
||||
return nil, errors.New("API error")
|
||||
{
|
||||
client: &fakeClient{
|
||||
containerListFunc: func(_ context.Context, _ container.ListOptions) ([]container.Summary, error) {
|
||||
return nil, errors.New("API error")
|
||||
},
|
||||
},
|
||||
toComplete: "container=",
|
||||
expected: []string{},
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "container=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
||||
|
||||
// Successful completion lists the name and id of the current Docker daemon, prefixed with "daemon=".
|
||||
// Filtering the completions by the current word is delegated to the completion script.
|
||||
func TestCompleteEventFilterDaemon(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
infoFunc: func(ctx context.Context) (system.Info, error) {
|
||||
return system.Info{
|
||||
ID: "daemon-id",
|
||||
Name: "daemon-name",
|
||||
}, nil
|
||||
{
|
||||
client: &fakeClient{
|
||||
infoFunc: func(ctx context.Context) (system.Info, error) {
|
||||
return system.Info{
|
||||
ID: "daemon-id",
|
||||
Name: "daemon-name",
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
toComplete: "daemon=",
|
||||
expected: []string{"daemon=daemon-name", "daemon=daemon-id"},
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "daemon=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{"daemon=daemon-name", "daemon=daemon-id"})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
||||
|
||||
// In case of API errors, no completions are returned.
|
||||
func TestCompleteEventFilterDaemonAPIError(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
infoFunc: func(ctx context.Context) (system.Info, error) {
|
||||
return system.Info{}, errors.New("API error")
|
||||
{
|
||||
client: &fakeClient{
|
||||
infoFunc: func(ctx context.Context) (system.Info, error) {
|
||||
return system.Info{}, errors.New("API error")
|
||||
},
|
||||
},
|
||||
toComplete: "daemon=",
|
||||
expected: []string{},
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "daemon=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
||||
|
||||
// Successful completion lists all network names, prefixed with "network=".
|
||||
// Filtering the completions by the current word is delegated to the completion script.
|
||||
func TestCompleteEventFilterNetwork(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) {
|
||||
return []network.Summary{
|
||||
*builders.NetworkResource(builders.NetworkResourceName("nw1")),
|
||||
*builders.NetworkResource(builders.NetworkResourceName("nw2")),
|
||||
}, nil
|
||||
{
|
||||
client: &fakeClient{
|
||||
networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) {
|
||||
return []network.Summary{
|
||||
*builders.NetworkResource(builders.NetworkResourceName("nw1")),
|
||||
*builders.NetworkResource(builders.NetworkResourceName("nw2")),
|
||||
}, nil
|
||||
},
|
||||
},
|
||||
toComplete: "network=",
|
||||
expected: []string{"network=nw1", "network=nw2"},
|
||||
},
|
||||
})
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "network=")
|
||||
|
||||
assert.DeepEqual(t, completions, []string{"network=nw1", "network=nw2"})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
}
|
||||
|
||||
// In case of API errors, no completions are returned.
|
||||
func TestCompleteEventFilterNetworkAPIError(t *testing.T) {
|
||||
cli := test.NewFakeCli(&fakeClient{
|
||||
networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) {
|
||||
return nil, errors.New("API error")
|
||||
{
|
||||
client: &fakeClient{
|
||||
networkListFunc: func(_ context.Context, _ network.ListOptions) ([]network.Summary, error) {
|
||||
return nil, errors.New("API error")
|
||||
},
|
||||
},
|
||||
toComplete: "network=",
|
||||
expected: []string{},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, "network=")
|
||||
for _, tc := range tests {
|
||||
cli := test.NewFakeCli(tc.client)
|
||||
|
||||
assert.DeepEqual(t, completions, []string{})
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp)
|
||||
completions, directive := completeFilters(cli)(NewEventsCommand(cli), nil, tc.toComplete)
|
||||
|
||||
assert.DeepEqual(t, completions, tc.expected)
|
||||
assert.Equal(t, directive, cobra.ShellCompDirectiveNoFileComp, fmt.Sprintf("wrong directive in completion for '%s'", tc.toComplete))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue