github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/cobra_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"testing"
     5  
     6  	pluginmanager "github.com/docker/cli/cli-plugins/manager"
     7  	"github.com/google/go-cmp/cmp/cmpopts"
     8  	"github.com/spf13/cobra"
     9  	"gotest.tools/v3/assert"
    10  	is "gotest.tools/v3/assert/cmp"
    11  )
    12  
    13  func TestVisitAll(t *testing.T) {
    14  	root := &cobra.Command{Use: "root"}
    15  	sub1 := &cobra.Command{Use: "sub1"}
    16  	sub1sub1 := &cobra.Command{Use: "sub1sub1"}
    17  	sub1sub2 := &cobra.Command{Use: "sub1sub2"}
    18  	sub2 := &cobra.Command{Use: "sub2"}
    19  
    20  	root.AddCommand(sub1, sub2)
    21  	sub1.AddCommand(sub1sub1, sub1sub2)
    22  
    23  	// Take the opportunity to test DisableFlagsInUseLine too
    24  	DisableFlagsInUseLine(root)
    25  
    26  	var visited []string
    27  	VisitAll(root, func(ccmd *cobra.Command) {
    28  		visited = append(visited, ccmd.Name())
    29  		assert.Assert(t, ccmd.DisableFlagsInUseLine, "DisableFlagsInUseLine not set on %q", ccmd.Name())
    30  	})
    31  	expected := []string{"sub1sub1", "sub1sub2", "sub1", "sub2", "root"}
    32  	assert.DeepEqual(t, expected, visited)
    33  }
    34  
    35  func TestVendorAndVersion(t *testing.T) {
    36  	// Non plugin.
    37  	assert.Equal(t, vendorAndVersion(&cobra.Command{Use: "test"}), "")
    38  
    39  	// Plugins with various lengths of vendor.
    40  	for _, tc := range []struct {
    41  		vendor   string
    42  		version  string
    43  		expected string
    44  	}{
    45  		{vendor: "vendor", expected: "(vendor)"},
    46  		{vendor: "vendor", version: "testing", expected: "(vendor, testing)"},
    47  	} {
    48  		t.Run(tc.vendor, func(t *testing.T) {
    49  			cmd := &cobra.Command{
    50  				Use: "test",
    51  				Annotations: map[string]string{
    52  					pluginmanager.CommandAnnotationPlugin:        "true",
    53  					pluginmanager.CommandAnnotationPluginVendor:  tc.vendor,
    54  					pluginmanager.CommandAnnotationPluginVersion: tc.version,
    55  				},
    56  			}
    57  			assert.Equal(t, vendorAndVersion(cmd), tc.expected)
    58  		})
    59  	}
    60  }
    61  
    62  func TestInvalidPlugin(t *testing.T) {
    63  	root := &cobra.Command{Use: "root"}
    64  	sub1 := &cobra.Command{Use: "sub1"}
    65  	sub1sub1 := &cobra.Command{Use: "sub1sub1"}
    66  	sub1sub2 := &cobra.Command{Use: "sub1sub2"}
    67  	sub2 := &cobra.Command{Use: "sub2"}
    68  
    69  	assert.Assert(t, is.Len(invalidPlugins(root), 0))
    70  
    71  	sub1.Annotations = map[string]string{
    72  		pluginmanager.CommandAnnotationPlugin:        "true",
    73  		pluginmanager.CommandAnnotationPluginInvalid: "foo",
    74  	}
    75  	root.AddCommand(sub1, sub2)
    76  	sub1.AddCommand(sub1sub1, sub1sub2)
    77  
    78  	assert.DeepEqual(t, invalidPlugins(root), []*cobra.Command{sub1}, cmpopts.IgnoreUnexported(cobra.Command{}))
    79  }
    80  
    81  func TestDecoratedName(t *testing.T) {
    82  	root := &cobra.Command{Use: "root"}
    83  	topLevelCommand := &cobra.Command{Use: "pluginTopLevelCommand"}
    84  	root.AddCommand(topLevelCommand)
    85  	assert.Equal(t, decoratedName(topLevelCommand), "pluginTopLevelCommand ")
    86  	topLevelCommand.Annotations = map[string]string{pluginmanager.CommandAnnotationPlugin: "true"}
    87  	assert.Equal(t, decoratedName(topLevelCommand), "pluginTopLevelCommand*")
    88  }