github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/command/minimum_version_check_test.go (about)

     1  package command_test
     2  
     3  import (
     4  	. "code.cloudfoundry.org/cli/command"
     5  	"code.cloudfoundry.org/cli/command/commandfakes"
     6  	"code.cloudfoundry.org/cli/util/ui"
     7  	"code.cloudfoundry.org/cli/version"
     8  
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  	. "github.com/onsi/gomega/gbytes"
    12  )
    13  
    14  var _ = Describe("Minimum Version Check", func() {
    15  	Describe("MinimumAPIVersionCheck", func() {
    16  		minimumVersion := "1.0.0"
    17  		Context("current version is greater than min", func() {
    18  			It("does not return an error", func() {
    19  				currentVersion := "1.0.1"
    20  				err := MinimumAPIVersionCheck(currentVersion, minimumVersion)
    21  				Expect(err).ToNot(HaveOccurred())
    22  			})
    23  		})
    24  
    25  		Context("current version is less than min", func() {
    26  			It("does return an error", func() {
    27  				currentVersion := "1.0.0-alpha.5"
    28  				err := MinimumAPIVersionCheck(currentVersion, minimumVersion)
    29  				Expect(err).To(MatchError(MinimumAPIVersionNotMetError{
    30  					CurrentVersion: currentVersion,
    31  					MinimumVersion: minimumVersion,
    32  				}))
    33  			})
    34  		})
    35  
    36  		Context("current version is the default version", func() {
    37  			It("does not return an error", func() {
    38  				err := MinimumAPIVersionCheck(version.DefaultVersion, minimumVersion)
    39  				Expect(err).ToNot(HaveOccurred())
    40  			})
    41  		})
    42  
    43  		Context("minimum version is empty", func() {
    44  			It("does not return an error", func() {
    45  				err := MinimumAPIVersionCheck("2.0.0", "")
    46  				Expect(err).ToNot(HaveOccurred())
    47  			})
    48  		})
    49  	})
    50  
    51  	Describe("WarnAPIVersionCheck", func() {
    52  		var (
    53  			testUI        *ui.UI
    54  			fakeConfig    *commandfakes.FakeConfig
    55  			apiVersion    string
    56  			minCLIVersion string
    57  			binaryVersion string
    58  		)
    59  
    60  		BeforeEach(func() {
    61  			testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    62  			fakeConfig = new(commandfakes.FakeConfig)
    63  
    64  			apiVersion = "1.2.3"
    65  			fakeConfig.APIVersionReturns(apiVersion)
    66  			minCLIVersion = "1.0.0"
    67  			fakeConfig.MinCLIVersionReturns(minCLIVersion)
    68  		})
    69  
    70  		Context("when checking the cloud controller minimum version warning", func() {
    71  			Context("when the CLI version is less than the recommended minimum", func() {
    72  				BeforeEach(func() {
    73  					binaryVersion = "0.0.0"
    74  					fakeConfig.BinaryVersionReturns(binaryVersion)
    75  				})
    76  
    77  				It("displays a recommendation to update the CLI version", func() {
    78  					err := WarnAPIVersionCheck(fakeConfig, testUI)
    79  					Expect(testUI.Err).To(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion))
    80  					Expect(err).ToNot(HaveOccurred())
    81  				})
    82  			})
    83  		})
    84  
    85  		Context("when the CLI version is greater or equal to the recommended minimum", func() {
    86  			BeforeEach(func() {
    87  				binaryVersion = "1.0.0"
    88  				fakeConfig.BinaryVersionReturns(binaryVersion)
    89  			})
    90  
    91  			It("does not display a recommendation to update the CLI version", func() {
    92  				err := WarnAPIVersionCheck(fakeConfig, testUI)
    93  				Expect(err).ToNot(HaveOccurred())
    94  				Expect(testUI.Err).NotTo(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion))
    95  			})
    96  		})
    97  
    98  		Context("when an error is encountered while parsing the semver versions", func() {
    99  			BeforeEach(func() {
   100  				fakeConfig.BinaryVersionReturns("&#%")
   101  			})
   102  
   103  			It("does not recommend to update the CLI version", func() {
   104  				err := WarnAPIVersionCheck(fakeConfig, testUI)
   105  				Expect(err).To(HaveOccurred())
   106  				Expect(err.Error()).To(Equal("No Major.Minor.Patch elements found"))
   107  				Expect(testUI.Err).NotTo(Say("Cloud Foundry API version %s requires CLI version %s.", apiVersion, minCLIVersion))
   108  			})
   109  		})
   110  
   111  		Context("version contains string", func() {
   112  			BeforeEach(func() {
   113  				minCLIVersion = "1.0.0"
   114  				fakeConfig.MinCLIVersionReturns(minCLIVersion)
   115  				binaryVersion = "1.0.0-alpha.5"
   116  				fakeConfig.BinaryVersionReturns(binaryVersion)
   117  			})
   118  
   119  			It("does not return an error", func() {
   120  				err := WarnAPIVersionCheck(fakeConfig, testUI)
   121  				Expect(err).ToNot(HaveOccurred())
   122  				Expect(testUI.Err).To(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion))
   123  			})
   124  		})
   125  
   126  		Context("minimum version is empty", func() {
   127  			BeforeEach(func() {
   128  				minCLIVersion = ""
   129  				fakeConfig.MinCLIVersionReturns(minCLIVersion)
   130  				binaryVersion = "1.0.0"
   131  				fakeConfig.BinaryVersionReturns(binaryVersion)
   132  			})
   133  
   134  			It("does not return an error", func() {
   135  				err := WarnAPIVersionCheck(fakeConfig, testUI)
   136  				Expect(err).ToNot(HaveOccurred())
   137  				Expect(testUI.Err).NotTo(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion))
   138  			})
   139  		})
   140  
   141  		Context("when comparing the default versions", func() {
   142  			BeforeEach(func() {
   143  				minCLIVersion = "1.2.3"
   144  				fakeConfig.MinCLIVersionReturns(minCLIVersion)
   145  				binaryVersion = version.DefaultVersion
   146  				fakeConfig.BinaryVersionReturns(binaryVersion)
   147  			})
   148  
   149  			It("does not return an error", func() {
   150  				err := WarnAPIVersionCheck(fakeConfig, testUI)
   151  				Expect(err).ToNot(HaveOccurred())
   152  				Expect(testUI.Err).NotTo(Say("Cloud Foundry API version %s requires CLI version %s. You are currently on version %s. To upgrade your CLI, please visit: https://github.com/cloudfoundry/cli#downloads", apiVersion, minCLIVersion, binaryVersion))
   153  			})
   154  		})
   155  	})
   156  })