github.com/aserto-dev/calc-version@v1.1.4/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  )
    10  
    11  var _ = Describe("calc-version", func() {
    12  
    13  	var (
    14  		dir string
    15  		err error
    16  	)
    17  
    18  	BeforeEach(func() {
    19  		dir, err = ioutil.TempDir("", "calc-version")
    20  		Expect(err).ToNot(HaveOccurred())
    21  		err = os.Chdir(dir)
    22  		Expect(err).ToNot(HaveOccurred())
    23  	})
    24  
    25  	AfterEach(func() {
    26  		// err := os.RemoveAll(dir)
    27  		// Expect(err).ToNot(HaveOccurred())
    28  	})
    29  
    30  	Describe(".current_version", func() {
    31  		Context("when git does not exist", func() {
    32  
    33  			It("raises an error", func() {
    34  				oldBinary := gitBinary
    35  				gitBinary = "thisbinarydoesnotexist"
    36  				defer func() { gitBinary = oldBinary }()
    37  				err := verifyGit()
    38  				Expect(err).To(HaveOccurred())
    39  			})
    40  		})
    41  
    42  		Context("when current dir is not a git work tree", func() {
    43  			It("raises an error", func() {
    44  				_, err := currentVersion()
    45  				Expect(err).To(HaveOccurred())
    46  			})
    47  		})
    48  
    49  		Context("when current dir is a git work tree", func() {
    50  			It("does not raise", func() {
    51  				createGitDirWithTag("v0.0.1")
    52  				_, err := currentVersion()
    53  				Expect(err).ToNot(HaveOccurred())
    54  			})
    55  		})
    56  
    57  		Context("when the current tag is a semver version", func() {
    58  			It("does not raise an error", func() {
    59  				createGitDirWithTag("v0.0.1")
    60  				_, err := currentVersion()
    61  				Expect(err).ToNot(HaveOccurred())
    62  			})
    63  		})
    64  
    65  		Context("when the current tag is a not a semver version", func() {
    66  			It("raises an error", func() {
    67  				createGitDirWithTag("some_tag")
    68  				_, err := currentVersion()
    69  				Expect(err).To(HaveOccurred())
    70  			})
    71  		})
    72  
    73  		Context("when no git tag exists", func() {
    74  			It("raises with the same error message", func() {
    75  				_, err := git("init")
    76  				Expect(err).ToNot(HaveOccurred())
    77  
    78  				_, err = currentVersion()
    79  				Expect(err).To(HaveOccurred())
    80  			})
    81  		})
    82  
    83  		Context("when the current tag is a semver tag with a `+` element", func() {
    84  			It("raise with an error that this not supported", func() {
    85  				createGitDirWithTag("1.0.2+gold")
    86  				_, err := currentVersion()
    87  				Expect(err).To(HaveOccurred())
    88  			})
    89  		})
    90  
    91  		Context("when the current tag is a semver tag without a `v` in front", func() {
    92  			It("does not raise and returns the correct semver version", func() {
    93  				createGitDirWithTag("1.0.2")
    94  				version, err := currentVersion()
    95  				Expect(err).ToNot(HaveOccurred())
    96  				Expect(version).To(MatchRegexp(`^1\.0\.2$`))
    97  			})
    98  		})
    99  
   100  		Context("with newer commits since the current semver tag", func() {
   101  			Context("and a release version", func() {
   102  				BeforeEach(func() {
   103  					createGitDirWithTag("v1.0.2")
   104  					createCommit("test")
   105  				})
   106  
   107  				Context("when there are no uncommitted changes", func() {
   108  					It("returns a pre-release version without a dirty tag", func() {
   109  						version, err := currentVersion()
   110  						Expect(err).ToNot(HaveOccurred())
   111  						Expect(version).To(MatchRegexp(`^1\.0\.2-[0-9]{14}\.1\.g[0-9a-fA-F]{8}$`))
   112  					})
   113  				})
   114  
   115  				Context("when there are uncommitted changes", func() {
   116  					Context("in files tracked by git", func() {
   117  						It("returns a pre-release version with a dirty tag", func() {
   118  							createUncomittedChanges("tracked_file")
   119  
   120  							version, err := currentVersion()
   121  							Expect(err).ToNot(HaveOccurred())
   122  							Expect(version).To(MatchRegexp(`^1\.0\.2-[0-9]{14}\.1\.g[0-9a-fA-F]{8}-dirty$`))
   123  						})
   124  					})
   125  
   126  					Context("in files not tracked by git", func() {
   127  						It("returns a pre-release version with a dirty tag", func() {
   128  							err := ioutil.WriteFile("some_untracked_file", []byte("Dummy content"), 0655)
   129  							Expect(err).ToNot(HaveOccurred())
   130  
   131  							version, err := currentVersion()
   132  							Expect(err).ToNot(HaveOccurred())
   133  							Expect(version).To(MatchRegexp(`^1\.0\.2-[0-9]{14}\.1\.g[0-9a-fA-F]{8}-dirty$`))
   134  						})
   135  					})
   136  				})
   137  			})
   138  
   139  			Context("and an alpha version", func() {
   140  				BeforeEach(func() {
   141  					createGitDirWithTag("v2.4.0-alpha.foo")
   142  					createCommit("test")
   143  				})
   144  
   145  				Context("when there are no uncommitted changes", func() {
   146  					It("returns a pre-release version without a dirty tag", func() {
   147  						version, err := currentVersion()
   148  						Expect(err).ToNot(HaveOccurred())
   149  						Expect(version).To(MatchRegexp(`^2\.4\.0-alpha\.foo-[0-9]{14}\.1\.g[0-9a-fA-F]{8}$`))
   150  					})
   151  				})
   152  
   153  				Context("when there are uncommitted changes", func() {
   154  					Context("in files tracked by git", func() {
   155  						It("returns a pre-release version with a dirty tag", func() {
   156  							createUncomittedChanges("tracked_file")
   157  
   158  							version, err := currentVersion()
   159  							Expect(err).ToNot(HaveOccurred())
   160  							Expect(version).To(MatchRegexp(`^2\.4\.0-alpha\.foo-[0-9]{14}\.1\.g[0-9a-fA-F]{8}-dirty$`))
   161  						})
   162  					})
   163  
   164  					Context("in files not tracked by git", func() {
   165  						It("returns a pre-release version with a dirty tag", func() {
   166  							err := ioutil.WriteFile("some_untracked_file", []byte("Dummy content"), 0655)
   167  							Expect(err).ToNot(HaveOccurred())
   168  
   169  							version, err := currentVersion()
   170  							Expect(err).ToNot(HaveOccurred())
   171  							Expect(version).To(MatchRegexp(`^2\.4\.0-alpha\.foo-[0-9]{14}\.1\.g[0-9a-fA-F]{8}-dirty$`))
   172  						})
   173  					})
   174  				})
   175  			})
   176  		})
   177  
   178  		Context("with no new commits since the current semver tag", func() {
   179  			Context("and a release version", func() {
   180  				BeforeEach(func() {
   181  					createGitDirWithTag("v1.0.2")
   182  				})
   183  
   184  				Context("when there are no uncommitted changes", func() {
   185  					It("returns just the release version", func() {
   186  						version, err := currentVersion()
   187  						Expect(err).ToNot(HaveOccurred())
   188  						Expect(version).To(MatchRegexp(`^1\.0\.2$`))
   189  					})
   190  				})
   191  
   192  				Context("when there are uncommitted changes", func() {
   193  					It("returns the release version with a dirty tag", func() {
   194  						createUncomittedChanges("tracked_file")
   195  						version, err := currentVersion()
   196  						Expect(err).ToNot(HaveOccurred())
   197  						Expect(version).To(MatchRegexp(`^1\.0\.2-dirty$`))
   198  					})
   199  				})
   200  			})
   201  
   202  			Context("and an alpha version", func() {
   203  				BeforeEach(func() {
   204  					createGitDirWithTag("v2.4.0-alpha.foo")
   205  				})
   206  
   207  				Context("when there are no uncommitted changes", func() {
   208  					It("returns just the alpha version", func() {
   209  						version, err := currentVersion()
   210  						Expect(err).ToNot(HaveOccurred())
   211  						Expect(version).To(MatchRegexp(`^2\.4\.0-alpha\.foo$`))
   212  					})
   213  				})
   214  
   215  				Context("when there are uncommitted changes", func() {
   216  					It("returns the alpha version with a dirty tag", func() {
   217  						createUncomittedChanges("tracked_file")
   218  						version, err := currentVersion()
   219  						Expect(err).ToNot(HaveOccurred())
   220  						Expect(version).To(MatchRegexp(`^2\.4\.0-alpha\.foo-dirty$`))
   221  					})
   222  				})
   223  			})
   224  		})
   225  	})
   226  
   227  	Describe("pre-release", func() {
   228  		BeforeEach(func() {
   229  			createGitDirWithTag("v10.200.5")
   230  		})
   231  
   232  		Context("when pre-release is used", func() {
   233  			It("calculates the next patch version", func() {
   234  				version, err := currentVersion()
   235  				Expect(err).ToNot(HaveOccurred())
   236  				version = preRelease(version, "nightly")
   237  
   238  				Expect(version).To(Equal(`10.200.5-nightly`))
   239  			})
   240  		})
   241  
   242  		Context("when pre-release is used and worktree is dirty", func() {
   243  			BeforeEach(func() {
   244  				createUncomittedChanges("tracked_file")
   245  			})
   246  
   247  			It("calculates the next patch version", func() {
   248  				version, err := currentVersion()
   249  				Expect(err).ToNot(HaveOccurred())
   250  				version = preRelease(version, "nightly")
   251  
   252  				Expect(version).To(Equal(`10.200.5-dirty-nightly`))
   253  			})
   254  		})
   255  	})
   256  
   257  	Describe("next", func() {
   258  		BeforeEach(func() {
   259  			createGitDirWithTag("v10.200.5")
   260  		})
   261  
   262  		Context("when patch is used", func() {
   263  			It("calculates the next patch version", func() {
   264  				version, err := currentVersion()
   265  				Expect(err).ToNot(HaveOccurred())
   266  				version, err = next(version, "patch")
   267  				Expect(err).ToNot(HaveOccurred())
   268  
   269  				Expect(version).To(Equal(`10.200.6`))
   270  			})
   271  		})
   272  
   273  		Context("when minor is used", func() {
   274  			It("calculates the next minor version", func() {
   275  				version, err := currentVersion()
   276  				Expect(err).ToNot(HaveOccurred())
   277  				version, err = next(version, "minor")
   278  				Expect(err).ToNot(HaveOccurred())
   279  
   280  				Expect(version).To(Equal(`10.201.0`))
   281  			})
   282  		})
   283  
   284  		Context("when major is used", func() {
   285  			It("calculates the next major version", func() {
   286  				version, err := currentVersion()
   287  				Expect(err).ToNot(HaveOccurred())
   288  				version, err = next(version, "major")
   289  				Expect(err).ToNot(HaveOccurred())
   290  
   291  				Expect(version).To(Equal(`11.0.0`))
   292  			})
   293  		})
   294  	})
   295  })
   296  
   297  func createCommit(fileName string) {
   298  	err := ioutil.WriteFile(fileName, []byte("Dummy content"), 0655)
   299  	Expect(err).ToNot(HaveOccurred())
   300  
   301  	_, err = git("add", fileName)
   302  	Expect(err).ToNot(HaveOccurred())
   303  
   304  	_, err = git("commit", "--no-gpg-sign", "--message", "Dummy", fileName)
   305  	Expect(err).ToNot(HaveOccurred())
   306  }
   307  
   308  func createGitDirWithTag(tag string) {
   309  	_, err := git("init")
   310  	Expect(err).ToNot(HaveOccurred())
   311  
   312  	createCommit(tag)
   313  
   314  	_, err = git("tag", tag)
   315  	Expect(err).ToNot(HaveOccurred())
   316  }
   317  
   318  func createUncomittedChanges(file string) {
   319  	err := ioutil.WriteFile(file, []byte("Dummy content"), 0655)
   320  	Expect(err).ToNot(HaveOccurred())
   321  
   322  	_, err = git("add", file)
   323  	Expect(err).ToNot(HaveOccurred())
   324  }