github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+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/command_registry" 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 deps command_registry.Dependency 25 ) 26 27 updateCommandDependency := func(pluginCall bool) { 28 deps.Ui = ui 29 deps.PluginConfig = config 30 command_registry.Commands.SetCommand(command_registry.Commands.FindCommand("plugins").SetDependency(deps, pluginCall)) 31 } 32 33 BeforeEach(func() { 34 ui = &testterm.FakeUI{} 35 requirementsFactory = &testreq.FakeReqFactory{} 36 config = &testconfig.FakePluginConfiguration{} 37 38 rpc.DefaultServer = rpc.NewServer() 39 }) 40 41 runCommand := func(args ...string) bool { 42 return testcmd.RunCliCommand("plugins", args, requirementsFactory, updateCommandDependency, false) 43 } 44 45 Context("If --checksum flag is provided", func() { 46 It("computes and prints the sha1 checksum of the binary", func() { 47 config.PluginsReturns(map[string]plugin_config.PluginMetadata{ 48 "Test1": plugin_config.PluginMetadata{ 49 Location: "../../../fixtures/plugins/test_1.go", 50 Version: plugin.VersionType{Major: 1, Minor: 2, Build: 3}, 51 Commands: []plugin.Command{ 52 {Name: "test_1_cmd1", HelpText: "help text for test_1_cmd1"}, 53 }, 54 }, 55 }) 56 57 runCommand("--checksum") 58 59 Expect(ui.Outputs).To(ContainSubstrings( 60 []string{"Plugin Name", "Version", "sha1", "Command Help"}, 61 )) 62 }) 63 }) 64 65 It("should fail with usage when provided any arguments", func() { 66 requirementsFactory.LoginSuccess = true 67 Expect(runCommand("blahblah")).To(BeFalse()) 68 Expect(ui.Outputs).To(ContainSubstrings( 69 []string{"Incorrect Usage", "No argument"}, 70 )) 71 }) 72 73 It("returns a list of available methods of a plugin", func() { 74 config.PluginsReturns(map[string]plugin_config.PluginMetadata{ 75 "Test1": plugin_config.PluginMetadata{ 76 Location: "path/to/plugin", 77 Commands: []plugin.Command{ 78 {Name: "test_1_cmd1", HelpText: "help text for test_1_cmd1"}, 79 {Name: "test_1_cmd2", HelpText: "help text for test_1_cmd2"}, 80 }, 81 }, 82 }) 83 84 runCommand() 85 86 Expect(ui.Outputs).To(ContainSubstrings( 87 []string{"Listing Installed Plugins..."}, 88 []string{"OK"}, 89 []string{"Plugin Name", "Command Name", "Command Help"}, 90 []string{"Test1", "test_1_cmd1", "help text for test_1_cmd1"}, 91 []string{"Test1", "test_1_cmd2", "help text for test_1_cmd2"}, 92 )) 93 }) 94 95 It("lists the name of the command, it's alias and version", func() { 96 config.PluginsReturns(map[string]plugin_config.PluginMetadata{ 97 "Test1": plugin_config.PluginMetadata{ 98 Location: "path/to/plugin", 99 Version: plugin.VersionType{Major: 1, Minor: 2, Build: 3}, 100 Commands: []plugin.Command{ 101 {Name: "test_1_cmd1", Alias: "test_1_cmd1_alias", HelpText: "help text for test_1_cmd1"}, 102 {Name: "test_1_cmd2", Alias: "test_1_cmd2_alias", HelpText: "help text for test_1_cmd2"}, 103 }, 104 }, 105 }) 106 107 runCommand() 108 109 Expect(ui.Outputs).To(ContainSubstrings( 110 []string{"Test1", "test_1_cmd1", "1.2.3", ", test_1_cmd1_alias", "help text for test_1_cmd1"}, 111 []string{"Test1", "test_1_cmd2", "1.2.3", ", test_1_cmd2_alias", "help text for test_1_cmd2"}, 112 )) 113 }) 114 115 It("lists 'N/A' as version when plugin does not provide a version", func() { 116 config.PluginsReturns(map[string]plugin_config.PluginMetadata{ 117 "Test1": plugin_config.PluginMetadata{ 118 Location: "path/to/plugin", 119 Commands: []plugin.Command{ 120 {Name: "test_1_cmd1", Alias: "test_1_cmd1_alias", HelpText: "help text for test_1_cmd1"}, 121 }, 122 }, 123 }) 124 125 runCommand() 126 127 Expect(ui.Outputs).To(ContainSubstrings( 128 []string{"Test1", "test_1_cmd1", "N/A", ", test_1_cmd1_alias", "help text for test_1_cmd1"}, 129 )) 130 }) 131 132 It("does not list the plugin when it provides no available commands", func() { 133 config.PluginsReturns(map[string]plugin_config.PluginMetadata{ 134 "EmptyPlugin": plugin_config.PluginMetadata{Location: "../../../fixtures/plugins/empty_plugin.exe"}, 135 }) 136 137 runCommand() 138 Expect(ui.Outputs).NotTo(ContainSubstrings( 139 []string{"EmptyPlugin"}, 140 )) 141 }) 142 143 It("list multiple plugins and their associated commands", func() { 144 config.PluginsReturns(map[string]plugin_config.PluginMetadata{ 145 "Test1": plugin_config.PluginMetadata{Location: "path/to/plugin1", Commands: []plugin.Command{{Name: "test_1_cmd1", HelpText: "help text for test_1_cmd1"}}}, 146 "Test2": plugin_config.PluginMetadata{Location: "path/to/plugin2", Commands: []plugin.Command{{Name: "test_2_cmd1", HelpText: "help text for test_2_cmd1"}}}, 147 }) 148 149 runCommand() 150 Expect(ui.Outputs).To(ContainSubstrings( 151 []string{"Test1", "test_1_cmd1", "help text for test_1_cmd1"}, 152 []string{"Test2", "test_2_cmd1", "help text for test_2_cmd1"}, 153 )) 154 }) 155 })