github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/commands/plugin/plugins_test.go (about)

     1  package plugin_test
     2  
     3  import (
     4  	"net/rpc"
     5  
     6  	. "github.com/cloudfoundry/cli/cf/commands/plugin"
     7  	"github.com/cloudfoundry/cli/cf/configuration/plugin_config"
     8  	testconfig "github.com/cloudfoundry/cli/cf/configuration/plugin_config/fakes"
     9  	"github.com/cloudfoundry/cli/plugin"
    10  	testcmd "github.com/cloudfoundry/cli/testhelpers/commands"
    11  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    12  	testreq "github.com/cloudfoundry/cli/testhelpers/requirements"
    13  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
    14  
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("Plugins", func() {
    20  	var (
    21  		ui                  *testterm.FakeUI
    22  		requirementsFactory *testreq.FakeReqFactory
    23  		config              *testconfig.FakePluginConfiguration
    24  	)
    25  
    26  	BeforeEach(func() {
    27  		ui = &testterm.FakeUI{}
    28  		requirementsFactory = &testreq.FakeReqFactory{}
    29  		config = &testconfig.FakePluginConfiguration{}
    30  
    31  		rpc.DefaultServer = rpc.NewServer()
    32  	})
    33  
    34  	runCommand := func(args ...string) bool {
    35  		cmd := NewPlugins(ui, config)
    36  		return testcmd.RunCommand(cmd, args, requirementsFactory)
    37  	}
    38  	It("should fail with usage when provided any arguments", func() {
    39  		requirementsFactory.LoginSuccess = true
    40  		Expect(runCommand("blahblah")).To(BeFalse())
    41  		Expect(ui.FailedWithUsage).To(BeTrue())
    42  	})
    43  
    44  	It("returns a list of available methods of a plugin", func() {
    45  		config.PluginsReturns(map[string]plugin_config.PluginMetadata{
    46  			"Test1": plugin_config.PluginMetadata{
    47  				Location: "path/to/plugin",
    48  				Commands: []plugin.Command{
    49  					{Name: "test_1_cmd1", HelpText: "help text for test_1_cmd1"},
    50  					{Name: "test_1_cmd2", HelpText: "help text for test_1_cmd2"},
    51  				},
    52  			},
    53  		})
    54  
    55  		runCommand()
    56  
    57  		Expect(ui.Outputs).To(ContainSubstrings(
    58  			[]string{"Listing Installed Plugins..."},
    59  			[]string{"OK"},
    60  			[]string{"Plugin Name", "Command Name", "Command Help"},
    61  			[]string{"Test1", "test_1_cmd1", "help text for test_1_cmd1"},
    62  			[]string{"Test1", "test_1_cmd2", "help text for test_1_cmd2"},
    63  		))
    64  	})
    65  
    66  	It("lists the name of the command, it's alias and version", func() {
    67  		config.PluginsReturns(map[string]plugin_config.PluginMetadata{
    68  			"Test1": plugin_config.PluginMetadata{
    69  				Location: "path/to/plugin",
    70  				Version:  plugin.VersionType{Major: 1, Minor: 2, Build: 3},
    71  				Commands: []plugin.Command{
    72  					{Name: "test_1_cmd1", Alias: "test_1_cmd1_alias", HelpText: "help text for test_1_cmd1"},
    73  					{Name: "test_1_cmd2", Alias: "test_1_cmd2_alias", HelpText: "help text for test_1_cmd2"},
    74  				},
    75  			},
    76  		})
    77  
    78  		runCommand()
    79  
    80  		Expect(ui.Outputs).To(ContainSubstrings(
    81  			[]string{"Test1", "test_1_cmd1", "1.2.3", ", test_1_cmd1_alias", "help text for test_1_cmd1"},
    82  			[]string{"Test1", "test_1_cmd2", "1.2.3", ", test_1_cmd2_alias", "help text for test_1_cmd2"},
    83  		))
    84  	})
    85  
    86  	It("lists 'N/A' as version when plugin does not provide a version", func() {
    87  		config.PluginsReturns(map[string]plugin_config.PluginMetadata{
    88  			"Test1": plugin_config.PluginMetadata{
    89  				Location: "path/to/plugin",
    90  				Commands: []plugin.Command{
    91  					{Name: "test_1_cmd1", Alias: "test_1_cmd1_alias", HelpText: "help text for test_1_cmd1"},
    92  				},
    93  			},
    94  		})
    95  
    96  		runCommand()
    97  
    98  		Expect(ui.Outputs).To(ContainSubstrings(
    99  			[]string{"Test1", "test_1_cmd1", "N/A", ", test_1_cmd1_alias", "help text for test_1_cmd1"},
   100  		))
   101  	})
   102  
   103  	It("does not list the plugin when it provides no available commands", func() {
   104  		config.PluginsReturns(map[string]plugin_config.PluginMetadata{
   105  			"EmptyPlugin": plugin_config.PluginMetadata{Location: "../../../fixtures/plugins/empty_plugin.exe"},
   106  		})
   107  
   108  		runCommand()
   109  		Expect(ui.Outputs).NotTo(ContainSubstrings(
   110  			[]string{"EmptyPlugin"},
   111  		))
   112  	})
   113  
   114  	It("list multiple plugins and their associated commands", func() {
   115  		config.PluginsReturns(map[string]plugin_config.PluginMetadata{
   116  			"Test1": plugin_config.PluginMetadata{Location: "path/to/plugin1", Commands: []plugin.Command{{Name: "test_1_cmd1", HelpText: "help text for test_1_cmd1"}}},
   117  			"Test2": plugin_config.PluginMetadata{Location: "path/to/plugin2", Commands: []plugin.Command{{Name: "test_2_cmd1", HelpText: "help text for test_2_cmd1"}}},
   118  		})
   119  
   120  		runCommand()
   121  		Expect(ui.Outputs).To(ContainSubstrings(
   122  			[]string{"Test1", "test_1_cmd1", "help text for test_1_cmd1"},
   123  			[]string{"Test2", "test_2_cmd1", "help text for test_2_cmd1"},
   124  		))
   125  	})
   126  })