github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/commands/plugin/plugins_test.go (about)

     1  package plugin_test
     2  
     3  import (
     4  	"net/rpc"
     5  
     6  	"code.cloudfoundry.org/cli/cf/commandregistry"
     7  	plugincmd "code.cloudfoundry.org/cli/cf/commands/plugin"
     8  	"code.cloudfoundry.org/cli/cf/configuration/pluginconfig"
     9  	"code.cloudfoundry.org/cli/cf/configuration/pluginconfig/pluginconfigfakes"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    12  	"code.cloudfoundry.org/cli/plugin"
    13  	testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands"
    14  	. "code.cloudfoundry.org/cli/util/testhelpers/matchers"
    15  	testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal"
    16  
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  )
    20  
    21  var _ = Describe("Plugins", func() {
    22  	var (
    23  		ui                  *testterm.FakeUI
    24  		requirementsFactory *requirementsfakes.FakeFactory
    25  		config              *pluginconfigfakes.FakePluginConfiguration
    26  		deps                commandregistry.Dependency
    27  	)
    28  
    29  	updateCommandDependency := func(pluginCall bool) {
    30  		deps.UI = ui
    31  		deps.PluginConfig = config
    32  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("plugins").SetDependency(deps, pluginCall))
    33  	}
    34  
    35  	BeforeEach(func() {
    36  		ui = &testterm.FakeUI{}
    37  		requirementsFactory = new(requirementsfakes.FakeFactory)
    38  		config = new(pluginconfigfakes.FakePluginConfiguration)
    39  
    40  		rpc.DefaultServer = rpc.NewServer()
    41  	})
    42  
    43  	runCommand := func(args ...string) bool {
    44  		return testcmd.RunCLICommand("plugins", args, requirementsFactory, updateCommandDependency, false, ui)
    45  	}
    46  
    47  	Context("If --checksum flag is provided", func() {
    48  		It("computes and prints the sha1 checksum of the binary", func() {
    49  			config.PluginsReturns(map[string]pluginconfig.PluginMetadata{
    50  				"Test1": {
    51  					Location: "../../../fixtures/plugins/test_1.go",
    52  					Version:  plugin.VersionType{Major: 1, Minor: 2, Build: 3},
    53  					Commands: []plugin.Command{
    54  						{Name: "test_1_cmd1", HelpText: "help text for test_1_cmd1"},
    55  					},
    56  				},
    57  			})
    58  
    59  			runCommand("--checksum")
    60  
    61  			Expect(ui.Outputs()).To(ContainSubstrings(
    62  				[]string{"Plugin Name", "Version", "sha1", "Command Help"},
    63  			))
    64  		})
    65  	})
    66  
    67  	Context("when arguments are provided", func() {
    68  		var cmd commandregistry.Command
    69  		var flagContext flags.FlagContext
    70  
    71  		BeforeEach(func() {
    72  			cmd = &plugincmd.Plugins{}
    73  			cmd.SetDependency(deps, false)
    74  			flagContext = flags.NewFlagContext(cmd.MetaData().Flags)
    75  		})
    76  
    77  		It("should fail with usage", func() {
    78  			flagContext.Parse("blahblah")
    79  
    80  			reqs, err := cmd.Requirements(requirementsFactory, flagContext)
    81  			Expect(err).NotTo(HaveOccurred())
    82  
    83  			err = testcmd.RunRequirements(reqs)
    84  			Expect(err).To(HaveOccurred())
    85  			Expect(err.Error()).To(ContainSubstring("Incorrect Usage"))
    86  			Expect(err.Error()).To(ContainSubstring("No argument required"))
    87  		})
    88  	})
    89  
    90  	It("returns a  sorted list of available methods of a plugin", func() {
    91  		config.PluginsReturns(map[string]pluginconfig.PluginMetadata{
    92  			"BTest2": {
    93  				Location: "path/to/plugin",
    94  				Commands: []plugin.Command{
    95  					{Name: "B_test_2_cmd1", HelpText: "help text for test_2_cmd1"},
    96  				},
    97  			},
    98  			"aTest1": {
    99  				Location: "path/to/plugin",
   100  				Commands: []plugin.Command{
   101  					{Name: "a_test_1_cmd1", HelpText: "help text for test_1_cmd1"},
   102  					{Name: "a_test_1_cmd2", HelpText: "help text for test_1_cmd2"},
   103  				},
   104  			},
   105  		})
   106  
   107  		runCommand()
   108  
   109  		Expect(ui.Outputs()).To(ContainSubstrings(
   110  			[]string{"Listing Installed Plugins..."},
   111  			[]string{"OK"},
   112  			[]string{"Plugin Name", "Command Name", "Command Help"},
   113  			[]string{"aTest1", "a_test_1_cmd1", "help text for test_1_cmd1"},
   114  			[]string{"aTest1", "a_test_1_cmd2", "help text for test_1_cmd2"},
   115  			[]string{"BTest2", "B_test_2_cmd1", "help text for test_2_cmd1"},
   116  		))
   117  
   118  		Expect(ui.Outputs()[6]).To(ContainSubstring("Test2"))
   119  	})
   120  
   121  	It("lists the name of the command, it's alias and version", func() {
   122  		config.PluginsReturns(map[string]pluginconfig.PluginMetadata{
   123  			"Test1": {
   124  				Location: "path/to/plugin",
   125  				Version:  plugin.VersionType{Major: 1, Minor: 2, Build: 3},
   126  				Commands: []plugin.Command{
   127  					{Name: "test_1_cmd1", Alias: "test_1_cmd1_alias", HelpText: "help text for test_1_cmd1"},
   128  					{Name: "test_1_cmd2", Alias: "test_1_cmd2_alias", HelpText: "help text for test_1_cmd2"},
   129  				},
   130  			},
   131  		})
   132  
   133  		runCommand()
   134  
   135  		Expect(ui.Outputs()).To(ContainSubstrings(
   136  			[]string{"Test1", "test_1_cmd1", "1.2.3", ", test_1_cmd1_alias", "help text for test_1_cmd1"},
   137  			[]string{"Test1", "test_1_cmd2", "1.2.3", ", test_1_cmd2_alias", "help text for test_1_cmd2"},
   138  		))
   139  	})
   140  
   141  	It("lists 'N/A' as version when plugin does not provide a version", func() {
   142  		config.PluginsReturns(map[string]pluginconfig.PluginMetadata{
   143  			"Test1": {
   144  				Location: "path/to/plugin",
   145  				Commands: []plugin.Command{
   146  					{Name: "test_1_cmd1", Alias: "test_1_cmd1_alias", HelpText: "help text for test_1_cmd1"},
   147  				},
   148  			},
   149  		})
   150  
   151  		runCommand()
   152  
   153  		Expect(ui.Outputs()).To(ContainSubstrings(
   154  			[]string{"Test1", "test_1_cmd1", "N/A", ", test_1_cmd1_alias", "help text for test_1_cmd1"},
   155  		))
   156  	})
   157  
   158  	It("does not list the plugin when it provides no available commands", func() {
   159  		config.PluginsReturns(map[string]pluginconfig.PluginMetadata{
   160  			"EmptyPlugin": {Location: "../../../fixtures/plugins/empty_plugin.exe"},
   161  		})
   162  
   163  		runCommand()
   164  		Expect(ui.Outputs()).NotTo(ContainSubstrings(
   165  			[]string{"EmptyPlugin"},
   166  		))
   167  	})
   168  
   169  	It("list multiple plugins and their associated commands", func() {
   170  		config.PluginsReturns(map[string]pluginconfig.PluginMetadata{
   171  			"Test1": {Location: "path/to/plugin1", Commands: []plugin.Command{{Name: "test_1_cmd1", HelpText: "help text for test_1_cmd1"}}},
   172  			"Test2": {Location: "path/to/plugin2", Commands: []plugin.Command{{Name: "test_2_cmd1", HelpText: "help text for test_2_cmd1"}}},
   173  		})
   174  
   175  		runCommand()
   176  		Expect(ui.Outputs()).To(ContainSubstrings(
   177  			[]string{"Test1", "test_1_cmd1", "help text for test_1_cmd1"},
   178  			[]string{"Test2", "test_2_cmd1", "help text for test_2_cmd1"},
   179  		))
   180  	})
   181  })