github.com/jasonkeene/cli@v6.14.1-0.20160816203908-ca5715166dfb+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/rpcserver"
     9  	"github.com/cloudfoundry/cli/testhelpers/rpcserver/rpcserverfakes"
    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 *rpcserverfakes.FakeHandlers
    32  				ts          *rpcserver.TestServer
    33  				err         error
    34  			)
    35  
    36  			BeforeEach(func() {
    37  				rpcHandlers = new(rpcserverfakes.FakeHandlers)
    38  				ts, err = rpcserver.NewTestRPCServer(rpcHandlers)
    39  				Expect(err).NotTo(HaveOccurred())
    40  			})
    41  
    42  			JustBeforeEach(func() {
    43  				err = ts.Start()
    44  				Expect(err).NotTo(HaveOccurred())
    45  			})
    46  
    47  			AfterEach(func() {
    48  				ts.Stop()
    49  			})
    50  
    51  			Context("checking MinCliVersion", func() {
    52  				It("it calls rpc cmd 'IsMinCliVersion' if plugin metadata 'MinCliVersion' is set", func() {
    53  					args := []string{ts.Port(), "0"}
    54  					session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
    55  					Expect(err).ToNot(HaveOccurred())
    56  
    57  					session.Wait()
    58  
    59  					Expect(rpcHandlers.IsMinCliVersionCallCount()).To(Equal(1))
    60  				})
    61  
    62  				Context("when the min cli version is not met", func() {
    63  					BeforeEach(func() {
    64  						rpcHandlers.IsMinCliVersionStub = func(_ string, result *bool) error {
    65  							*result = false
    66  							return nil
    67  						}
    68  					})
    69  
    70  					It("notifies the user", func() {
    71  
    72  						args := []string{ts.Port(), "0"}
    73  						session, err := Start(exec.Command(validPluginPath, args...), GinkgoWriter, GinkgoWriter)
    74  						Expect(err).ToNot(HaveOccurred())
    75  
    76  						session.Wait()
    77  
    78  						Expect(session).To(gbytes.Say("Minimum CLI version 5.0.0 is required to run this plugin command"))
    79  
    80  					})
    81  				})
    82  			})
    83  		})
    84  	})
    85  
    86  	Describe("MinCliVersionStr", func() {
    87  		It("returns a string representation of VersionType{}", func() {
    88  			version := plugin.VersionType{
    89  				Major: 1,
    90  				Minor: 2,
    91  				Build: 3,
    92  			}
    93  
    94  			str := plugin.MinCliVersionStr(version)
    95  			Expect(str).To(Equal("1.2.3"))
    96  		})
    97  
    98  		It("returns a empty string if no field in VersionType is set", func() {
    99  			version := plugin.VersionType{}
   100  
   101  			str := plugin.MinCliVersionStr(version)
   102  			Expect(str).To(Equal(""))
   103  		})
   104  
   105  		It("uses '0' as return value for field that is not set", func() {
   106  			version := plugin.VersionType{
   107  				Build: 5,
   108  			}
   109  
   110  			str := plugin.MinCliVersionStr(version)
   111  			Expect(str).To(Equal("0.0.5"))
   112  		})
   113  
   114  	})
   115  })