github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/plugin/plugin_shim_test.go (about)

     1  package plugin_test
     2  
     3  import (
     4  	"os/exec"
     5  	"path/filepath"
     6  
     7  	"github.com/cloudfoundry/cli/plugin"
     8  	"github.com/cloudfoundry/cli/testhelpers/rpc_server"
     9  	fake_rpc_handlers "github.com/cloudfoundry/cli/testhelpers/rpc_server/fakes"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/gbytes"
    13  	. "github.com/onsi/gomega/gexec"
    14  )
    15  
    16  var _ = Describe("Command", func() {
    17  	var (
    18  		validPluginPath = filepath.Join("..", "fixtures", "plugins", "test_1.exe")
    19  	)
    20  
    21  	Describe(".Start", func() {
    22  		It("Exits with status 1 if it cannot ping the host port passed as an argument", func() {
    23  			args := []string{"0", "0"}
    24  			session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
    25  			Expect(err).ToNot(HaveOccurred())
    26  			Eventually(session, 2).Should(Exit(1))
    27  		})
    28  
    29  		Context("Executing plugins with '.Start()'", func() {
    30  			var (
    31  				rpcHandlers *fake_rpc_handlers.FakeHandlers
    32  				ts          *test_rpc_server.TestServer
    33  				err         error
    34  			)
    35  
    36  			BeforeEach(func() {
    37  				rpcHandlers = &fake_rpc_handlers.FakeHandlers{}
    38  				ts, err = test_rpc_server.NewTestRpcServer(rpcHandlers)
    39  				Expect(err).NotTo(HaveOccurred())
    40  
    41  				err = ts.Start()
    42  				Expect(err).NotTo(HaveOccurred())
    43  			})
    44  
    45  			AfterEach(func() {
    46  				ts.Stop()
    47  			})
    48  
    49  			Context("checking MinCliVersion", func() {
    50  				It("it calls rpc cmd 'IsMinCliVersion' if plugin metadata 'MinCliVersion' is set", func() {
    51  					args := []string{ts.Port(), "0"}
    52  					session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
    53  					Expect(err).ToNot(HaveOccurred())
    54  
    55  					session.Wait()
    56  
    57  					Expect(rpcHandlers.IsMinCliVersionCallCount()).To(Equal(1))
    58  				})
    59  
    60  				It("notifies the user 'min cli version is not met'", func() {
    61  					rpcHandlers.IsMinCliVersionStub = func(_ string, result *bool) error {
    62  						*result = false
    63  						return nil
    64  					}
    65  
    66  					args := []string{ts.Port(), "0"}
    67  					session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
    68  					Expect(err).ToNot(HaveOccurred())
    69  
    70  					session.Wait()
    71  
    72  					Expect(session).To(gbytes.Say("Minimum CLI version 5.0.0 is required to run this plugin command"))
    73  				})
    74  			})
    75  		})
    76  	})
    77  
    78  	Describe("MinCliVersionStr", func() {
    79  		It("returns a string representation of VersionType{}", func() {
    80  			version := plugin.VersionType{
    81  				Major: 1,
    82  				Minor: 2,
    83  				Build: 3,
    84  			}
    85  
    86  			str := plugin.MinCliVersionStr(version)
    87  			Expect(str).To(Equal("1.2.3"))
    88  		})
    89  
    90  		It("returns a empty string if no field in VersionType is set", func() {
    91  			version := plugin.VersionType{}
    92  
    93  			str := plugin.MinCliVersionStr(version)
    94  			Expect(str).To(Equal(""))
    95  		})
    96  
    97  		It("uses '0' as return value for field that is not set", func() {
    98  			version := plugin.VersionType{
    99  				Build: 5,
   100  			}
   101  
   102  			str := plugin.MinCliVersionStr(version)
   103  			Expect(str).To(Equal("0.0.5"))
   104  		})
   105  
   106  	})
   107  })