2018-12-17 11:59:11 -05:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-12-11 09:50:04 -05:00
|
|
|
pluginmanager "github.com/docker/cli/cli-plugins/manager"
|
2018-12-19 06:29:01 -05:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2018-12-17 11:59:11 -05:00
|
|
|
"github.com/spf13/cobra"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-12-17 11:59:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestVisitAll(t *testing.T) {
|
|
|
|
root := &cobra.Command{Use: "root"}
|
|
|
|
sub1 := &cobra.Command{Use: "sub1"}
|
|
|
|
sub1sub1 := &cobra.Command{Use: "sub1sub1"}
|
|
|
|
sub1sub2 := &cobra.Command{Use: "sub1sub2"}
|
|
|
|
sub2 := &cobra.Command{Use: "sub2"}
|
|
|
|
|
|
|
|
root.AddCommand(sub1, sub2)
|
|
|
|
sub1.AddCommand(sub1sub1, sub1sub2)
|
|
|
|
|
|
|
|
// Take the opportunity to test DisableFlagsInUseLine too
|
|
|
|
DisableFlagsInUseLine(root)
|
|
|
|
|
|
|
|
var visited []string
|
|
|
|
VisitAll(root, func(ccmd *cobra.Command) {
|
|
|
|
visited = append(visited, ccmd.Name())
|
|
|
|
assert.Assert(t, ccmd.DisableFlagsInUseLine, "DisableFlagsInUseLine not set on %q", ccmd.Name())
|
|
|
|
})
|
|
|
|
expected := []string{"sub1sub1", "sub1sub2", "sub1", "sub2", "root"}
|
|
|
|
assert.DeepEqual(t, expected, visited)
|
|
|
|
}
|
2018-12-11 09:50:04 -05:00
|
|
|
|
2019-02-15 11:27:22 -05:00
|
|
|
func TestVendorAndVersion(t *testing.T) {
|
2018-12-11 09:50:04 -05:00
|
|
|
// Non plugin.
|
2019-02-15 11:27:22 -05:00
|
|
|
assert.Equal(t, vendorAndVersion(&cobra.Command{Use: "test"}), "")
|
2018-12-11 09:50:04 -05:00
|
|
|
|
|
|
|
// Plugins with various lengths of vendor.
|
|
|
|
for _, tc := range []struct {
|
|
|
|
vendor string
|
2019-02-15 11:27:22 -05:00
|
|
|
version string
|
2018-12-11 09:50:04 -05:00
|
|
|
expected string
|
|
|
|
}{
|
2019-02-15 11:27:22 -05:00
|
|
|
{vendor: "vendor", expected: "(vendor)"},
|
|
|
|
{vendor: "vendor", version: "testing", expected: "(vendor, testing)"},
|
2018-12-11 09:50:04 -05:00
|
|
|
} {
|
|
|
|
t.Run(tc.vendor, func(t *testing.T) {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "test",
|
|
|
|
Annotations: map[string]string{
|
2019-02-15 11:27:22 -05:00
|
|
|
pluginmanager.CommandAnnotationPlugin: "true",
|
|
|
|
pluginmanager.CommandAnnotationPluginVendor: tc.vendor,
|
|
|
|
pluginmanager.CommandAnnotationPluginVersion: tc.version,
|
2018-12-11 09:50:04 -05:00
|
|
|
},
|
|
|
|
}
|
2019-02-15 11:27:22 -05:00
|
|
|
assert.Equal(t, vendorAndVersion(cmd), tc.expected)
|
2018-12-11 09:50:04 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-12-19 06:29:01 -05:00
|
|
|
|
|
|
|
func TestInvalidPlugin(t *testing.T) {
|
|
|
|
root := &cobra.Command{Use: "root"}
|
|
|
|
sub1 := &cobra.Command{Use: "sub1"}
|
|
|
|
sub1sub1 := &cobra.Command{Use: "sub1sub1"}
|
|
|
|
sub1sub2 := &cobra.Command{Use: "sub1sub2"}
|
|
|
|
sub2 := &cobra.Command{Use: "sub2"}
|
|
|
|
|
|
|
|
assert.Assert(t, is.Len(invalidPlugins(root), 0))
|
|
|
|
|
|
|
|
sub1.Annotations = map[string]string{
|
|
|
|
pluginmanager.CommandAnnotationPlugin: "true",
|
|
|
|
pluginmanager.CommandAnnotationPluginInvalid: "foo",
|
|
|
|
}
|
|
|
|
root.AddCommand(sub1, sub2)
|
|
|
|
sub1.AddCommand(sub1sub1, sub1sub2)
|
|
|
|
|
|
|
|
assert.DeepEqual(t, invalidPlugins(root), []*cobra.Command{sub1}, cmpopts.IgnoreUnexported(cobra.Command{}))
|
|
|
|
}
|
2019-02-15 11:27:22 -05:00
|
|
|
|
2022-06-28 04:17:50 -04:00
|
|
|
func TestCommandAliases(t *testing.T) {
|
|
|
|
root := &cobra.Command{Use: "root"}
|
|
|
|
sub := &cobra.Command{Use: "subcommand", Aliases: []string{"alias1", "alias2"}}
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
sub2 := &cobra.Command{Use: "subcommand2", Annotations: map[string]string{"aliases": "root foo, root bar"}}
|
2022-06-28 04:17:50 -04:00
|
|
|
root.AddCommand(sub)
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
root.AddCommand(sub2)
|
2022-06-28 04:17:50 -04:00
|
|
|
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
assert.Equal(t, hasAliases(sub), true)
|
2022-06-28 04:17:50 -04:00
|
|
|
assert.Equal(t, commandAliases(sub), "root subcommand, root alias1, root alias2")
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
assert.Equal(t, hasAliases(sub2), true)
|
|
|
|
assert.Equal(t, commandAliases(sub2), "root foo, root bar")
|
|
|
|
|
|
|
|
sub.Annotations = map[string]string{"aliases": "custom alias, custom alias 2"}
|
|
|
|
assert.Equal(t, hasAliases(sub), true)
|
|
|
|
assert.Equal(t, commandAliases(sub), "custom alias, custom alias 2")
|
2022-06-28 04:17:50 -04:00
|
|
|
}
|
|
|
|
|
2019-02-15 11:27:22 -05:00
|
|
|
func TestDecoratedName(t *testing.T) {
|
|
|
|
root := &cobra.Command{Use: "root"}
|
|
|
|
topLevelCommand := &cobra.Command{Use: "pluginTopLevelCommand"}
|
|
|
|
root.AddCommand(topLevelCommand)
|
|
|
|
assert.Equal(t, decoratedName(topLevelCommand), "pluginTopLevelCommand ")
|
|
|
|
topLevelCommand.Annotations = map[string]string{pluginmanager.CommandAnnotationPlugin: "true"}
|
|
|
|
assert.Equal(t, decoratedName(topLevelCommand), "pluginTopLevelCommand*")
|
|
|
|
}
|