From 4a401b8938d13f0936e0f43239e8dc664d32a847 Mon Sep 17 00:00:00 2001 From: Marco Schmidt Date: Thu, 14 Nov 2019 21:26:54 -0500 Subject: [PATCH] Fix version increase logic (#346) * Update version bump logic * Changelog * gofmt & goimports * Remove leftover code * Simplify mock expectations * Changelog typos * Add FIX typo to test data * Fix test * Reword changelog --- botutils/botconfig/os_mock_test.go | 3 +- .../v0.10.28/version-increase-for-v1.yaml | 8 + changelogutils/changelog.go | 4 +- changelogutils/entrytype.go | 4 + changelogutils/mounted_repo_mock_test.go | 3 +- changelogutils/reader_test.go | 27 ++ changelogutils/validator.go | 4 +- changelogutils/validator_test.go | 307 ++++++++++-------- debugutils/mocks_kube_test.go | 3 +- debugutils/mocks_test.go | 3 +- gcloudutils/builders/storage_builder.go | 3 +- installutils/kubeinstall/cache.go | 1 + kubeerrutils/kube_errs.go | 1 - testutils/helper/install.go | 6 +- versionutils/version.go | 9 +- versionutils/version_test.go | 22 +- 16 files changed, 254 insertions(+), 154 deletions(-) create mode 100644 changelog/v0.10.28/version-increase-for-v1.yaml diff --git a/botutils/botconfig/os_mock_test.go b/botutils/botconfig/os_mock_test.go index c22696d5..49a77e1c 100644 --- a/botutils/botconfig/os_mock_test.go +++ b/botutils/botconfig/os_mock_test.go @@ -5,8 +5,9 @@ package botconfig_test import ( - gomock "github.com/golang/mock/gomock" reflect "reflect" + + gomock "github.com/golang/mock/gomock" ) // MockOsClient is a mock of OsClient interface diff --git a/changelog/v0.10.28/version-increase-for-v1.yaml b/changelog/v0.10.28/version-increase-for-v1.yaml new file mode 100644 index 00000000..8c24468e --- /dev/null +++ b/changelog/v0.10.28/version-increase-for-v1.yaml @@ -0,0 +1,8 @@ +changelog: +- type: FIX + description: > + When verifying changelog entries after the v1.0.0 release, the version utils currently require a minor version bump + for any non-breaking change. We need to update the logic to ensure that if a changelog entry is of type + `BREAKING_CHANGE` we must bump the major version, if it is of type `NEW_FEATURE` we must bump the minor version, + and else we have to bump the patch version. + issueLink: https://github.com/solo-io/go-utils/issues/345 \ No newline at end of file diff --git a/changelogutils/changelog.go b/changelogutils/changelog.go index 8a1799a8..1afbff90 100644 --- a/changelogutils/changelog.go +++ b/changelogutils/changelog.go @@ -308,16 +308,18 @@ func ComputeChangelogForNonRelease(fs afero.Fs, latestTag, proposedTag, changelo return nil, err } breakingChanges := false + newFeature := false releaseStableApi := false for _, file := range changelog.Files { for _, entry := range file.Entries { breakingChanges = breakingChanges || entry.Type.BreakingChange() + newFeature = newFeature || entry.Type.NewFeature() } releaseStableApi = releaseStableApi || file.GetReleaseStableApi() } - expectedVersion := latestVersion.IncrementVersion(breakingChanges) + expectedVersion := latestVersion.IncrementVersion(breakingChanges, newFeature) if releaseStableApi { if !proposedVersion.Equals(&versionutils.StableApiVersion) { return nil, errors.Errorf("Changelog indicates this is a stable API release, which should be used only to indicate the release of v1.0.0, not %s", proposedVersion) diff --git a/changelogutils/entrytype.go b/changelogutils/entrytype.go index 549b8889..5864e3c9 100644 --- a/changelogutils/entrytype.go +++ b/changelogutils/entrytype.go @@ -47,6 +47,10 @@ func (clt ChangelogEntryType) BreakingChange() bool { return clt == BREAKING_CHANGE } +func (clt ChangelogEntryType) NewFeature() bool { + return clt == NEW_FEATURE +} + func (clt ChangelogEntryType) MarshalJSON() ([]byte, error) { if s, ok := interface{}(clt).(fmt.Stringer); ok { return json.Marshal(s.String()) diff --git a/changelogutils/mounted_repo_mock_test.go b/changelogutils/mounted_repo_mock_test.go index 1f91374f..f5dd7644 100644 --- a/changelogutils/mounted_repo_mock_test.go +++ b/changelogutils/mounted_repo_mock_test.go @@ -6,9 +6,10 @@ package changelogutils_test import ( context "context" - gomock "github.com/golang/mock/gomock" os "os" reflect "reflect" + + gomock "github.com/golang/mock/gomock" ) // MockMountedRepo is a mock of MountedRepo interface diff --git a/changelogutils/reader_test.go b/changelogutils/reader_test.go index 2354eb6a..7d710487 100644 --- a/changelogutils/reader_test.go +++ b/changelogutils/reader_test.go @@ -423,6 +423,7 @@ changelog: description: foo2 issueLink: bar2 ` + validChangelog2 = ` changelog: - type: NON_USER_FACING @@ -432,6 +433,7 @@ changelog: issueLink: bar4 resolvesIssue: false ` + validChangelog3 = ` changelog: - type: DEPENDENCY_BUMP @@ -447,6 +449,31 @@ changelog: issueLink: bar ` + validNewFeatureChangelog = ` +changelog: + - type: NEW_FEATURE + description: cool new feature + issueLink: http://issue +` + + validNonBreakingNorNewFeatureChangelog = ` +changelog: + - type: NON_USER_FACING + - type: DEPENDENCY_BUMP + dependencyOwner: foo + dependencyRepo: bar + dependencyTag: baz + - type: UPGRADE + description: foo5 + issueLink: bar5 + - type: HELM + description: foo6 + issueLink: bar6 + - type: FIX + description: foo1 + issueLink: bar1 +` + validStableReleaseChangelog = ` changelog: - type: NON_USER_FACING diff --git a/changelogutils/validator.go b/changelogutils/validator.go index 69508e4a..00ff6bc5 100644 --- a/changelogutils/validator.go +++ b/changelogutils/validator.go @@ -155,16 +155,18 @@ func (c *changelogValidator) validateVersionBump(ctx context.Context, latestTag return err } breakingChanges := false + newFeature := false releaseStableApi := false for _, file := range changelog.Files { for _, entry := range file.Entries { breakingChanges = breakingChanges || entry.Type.BreakingChange() + newFeature = newFeature || entry.Type.NewFeature() } releaseStableApi = releaseStableApi || file.GetReleaseStableApi() } - expectedVersion := latestVersion.IncrementVersion(breakingChanges) + expectedVersion := latestVersion.IncrementVersion(breakingChanges, newFeature) if releaseStableApi { if !changelog.Version.Equals(&versionutils.StableApiVersion) { return InvalidUseOfStableApiError(changelog.Version.String()) diff --git a/changelogutils/validator_test.go b/changelogutils/validator_test.go index b7e012d0..df2485f2 100644 --- a/changelogutils/validator_test.go +++ b/changelogutils/validator_test.go @@ -290,143 +290,186 @@ var _ = Describe("github utils", func() { Context("incrementing versions", func() { - It("works on patch version bump", func() { - file1 := github.CommitFile{Filename: &path1, Status: &added} - cc := github.CommitsComparison{Files: []github.CommitFile{file1}} - repoClient.EXPECT(). - CompareCommits(ctx, base, sha). - Return(&cc, nil) - code.EXPECT(). - GetFileContents(ctx, path1). - Return([]byte(validChangelog1), nil) - repoClient.EXPECT(). - FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). - Return("v0.5.0", nil) - code.EXPECT(). - ListFiles(ctx, changelogutils.ChangelogDirectory). - Return([]os.FileInfo{getChangelogDir(tag)}, nil) - code.EXPECT(). - ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, tag)). - Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) - code.EXPECT(). - GetFileContents(ctx, path1). - Return([]byte(validChangelog1), nil) - file, err := validator.ValidateChangelog(ctx) - Expect(file).NotTo(BeNil()) - Expect(err).To(BeNil()) - }) - - It("errors when not incrementing major version", func() { - file1 := github.CommitFile{Filename: &path1, Status: &added} - cc := github.CommitsComparison{Files: []github.CommitFile{file1}} - repoClient.EXPECT(). - CompareCommits(ctx, base, sha). - Return(&cc, nil) - code.EXPECT(). - GetFileContents(ctx, path1). - Return([]byte(validBreakingChangelog), nil) - repoClient.EXPECT(). - FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). - Return("v0.5.0", nil) - code.EXPECT(). - ListFiles(ctx, changelogutils.ChangelogDirectory). - Return([]os.FileInfo{getChangelogDir(tag)}, nil) - code.EXPECT(). - ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, tag)). - Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) - code.EXPECT(). - GetFileContents(ctx, path1). - Return([]byte(validBreakingChangelog), nil) - - expected := changelogutils.UnexpectedProposedVersionError("v0.6.0", tag) - file, err := validator.ValidateChangelog(ctx) - Expect(file).To(BeNil()) - Expect(err.Error()).To(Equal(expected.Error())) - }) - - It("works when incrementing major version", func() { - path := filepath.Join(changelogutils.ChangelogDirectory, "v0.6.0", filename1) - file1 := github.CommitFile{Filename: &path, Status: &added} - cc := github.CommitsComparison{Files: []github.CommitFile{file1}} - repoClient.EXPECT(). - CompareCommits(ctx, base, sha). - Return(&cc, nil) - code.EXPECT(). - GetFileContents(ctx, path). - Return([]byte(validBreakingChangelog), nil) - repoClient.EXPECT(). - FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). - Return("v0.5.0", nil) - code.EXPECT(). - ListFiles(ctx, changelogutils.ChangelogDirectory). - Return([]os.FileInfo{getChangelogDir("v0.6.0")}, nil) - code.EXPECT(). - ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, "v0.6.0")). - Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) - code.EXPECT(). - GetFileContents(ctx, path). - Return([]byte(validBreakingChangelog), nil) + Context("major version zero 0.y.z", func() { + + It("works on patch version bump", func() { + file1 := github.CommitFile{Filename: &path1, Status: &added} + cc := github.CommitsComparison{Files: []github.CommitFile{file1}} + repoClient.EXPECT(). + CompareCommits(ctx, base, sha). + Return(&cc, nil) + code.EXPECT(). + GetFileContents(ctx, path1). + Return([]byte(validChangelog1), nil).Times(2) + repoClient.EXPECT(). + FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). + Return("v0.5.0", nil) + code.EXPECT(). + ListFiles(ctx, changelogutils.ChangelogDirectory). + Return([]os.FileInfo{getChangelogDir(tag)}, nil) + code.EXPECT(). + ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, tag)). + Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) + file, err := validator.ValidateChangelog(ctx) + Expect(file).NotTo(BeNil()) + Expect(err).To(BeNil()) + }) + + It("errors when not incrementing major version", func() { + file1 := github.CommitFile{Filename: &path1, Status: &added} + cc := github.CommitsComparison{Files: []github.CommitFile{file1}} + repoClient.EXPECT(). + CompareCommits(ctx, base, sha). + Return(&cc, nil) + code.EXPECT(). + GetFileContents(ctx, path1). + Return([]byte(validBreakingChangelog), nil).Times(2) + repoClient.EXPECT(). + FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). + Return("v0.5.0", nil) + code.EXPECT(). + ListFiles(ctx, changelogutils.ChangelogDirectory). + Return([]os.FileInfo{getChangelogDir(tag)}, nil) + code.EXPECT(). + ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, tag)). + Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) + + expected := changelogutils.UnexpectedProposedVersionError("v0.6.0", tag) + file, err := validator.ValidateChangelog(ctx) + Expect(file).To(BeNil()) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(Equal(expected.Error())) + }) + + It("works when incrementing major version", func() { + path := filepath.Join(changelogutils.ChangelogDirectory, "v0.6.0", filename1) + file1 := github.CommitFile{Filename: &path, Status: &added} + cc := github.CommitsComparison{Files: []github.CommitFile{file1}} + repoClient.EXPECT(). + CompareCommits(ctx, base, sha). + Return(&cc, nil) + code.EXPECT(). + GetFileContents(ctx, path). + Return([]byte(validBreakingChangelog), nil).Times(2) + repoClient.EXPECT(). + FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). + Return("v0.5.0", nil) + code.EXPECT(). + ListFiles(ctx, changelogutils.ChangelogDirectory). + Return([]os.FileInfo{getChangelogDir("v0.6.0")}, nil) + code.EXPECT(). + ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, "v0.6.0")). + Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) + + file, err := validator.ValidateChangelog(ctx) + Expect(err).To(BeNil()) + Expect(file).NotTo(BeNil()) + }) - file, err := validator.ValidateChangelog(ctx) - Expect(err).To(BeNil()) - Expect(file).NotTo(BeNil()) }) - It("works for stable api release", func() { - path := filepath.Join(changelogutils.ChangelogDirectory, "v1.0.0", filename1) - file1 := github.CommitFile{Filename: &path, Status: &added} - cc := github.CommitsComparison{Files: []github.CommitFile{file1}} - repoClient.EXPECT(). - CompareCommits(ctx, base, sha). - Return(&cc, nil) - code.EXPECT(). - GetFileContents(ctx, path). - Return([]byte(validStableReleaseChangelog), nil) - repoClient.EXPECT(). - FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). - Return("v0.5.0", nil) - code.EXPECT(). - ListFiles(ctx, changelogutils.ChangelogDirectory). - Return([]os.FileInfo{getChangelogDir("v1.0.0")}, nil) - code.EXPECT(). - ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, "v1.0.0")). - Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) - code.EXPECT(). - GetFileContents(ctx, path). - Return([]byte(validStableReleaseChangelog), nil) - - file, err := validator.ValidateChangelog(ctx) - Expect(err).To(BeNil()) - Expect(file).NotTo(BeNil()) + Context("major version is 1.y.z", func() { + + DescribeTable("correctly enforces version bump rules", + func(lastTag, nextTag, contents string, expectFailure bool) { + + nextTagFile := filepath.Join(changelogutils.ChangelogDirectory, nextTag, filename1) + cc := github.CommitsComparison{Files: []github.CommitFile{{Filename: &nextTagFile, Status: &added}}} + + repoClient.EXPECT(). + CompareCommits(ctx, base, sha). + Return(&cc, nil) + + code.EXPECT(). + GetFileContents(ctx, nextTagFile). + Return([]byte(contents), nil).Times(2) + + repoClient.EXPECT(). + FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). + Return(lastTag, nil) + + code.EXPECT(). + ListFiles(ctx, changelogutils.ChangelogDirectory). + Return([]os.FileInfo{getChangelogDir(nextTag)}, nil) + + code.EXPECT(). + ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, nextTag)). + Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) + + file, err := validator.ValidateChangelog(ctx) + + if expectFailure { + Expect(err).To(HaveOccurred()) + Expect(file).To(BeNil()) + } else { + Expect(err).NotTo(HaveOccurred()) + Expect(file).NotTo(BeNil()) + } + }, + Entry("breaking change with patch bump", "v1.0.0", "v1.0.1", validBreakingChangelog, true), + Entry("breaking change with minor bump", "v1.0.0", "v1.1.0", validBreakingChangelog, true), + Entry("breaking change with major bump", "v1.0.0", "v2.0.0", validBreakingChangelog, false), + Entry("new feature with patch bump", "v1.0.0", "v1.0.1", validNewFeatureChangelog, true), + Entry("new feature with minor bump", "v1.0.0", "v1.1.0", validNewFeatureChangelog, false), + Entry("new feature with major bump", "v1.0.0", "v2.0.0", validNewFeatureChangelog, true), + Entry("non-breaking with patch bump", "v1.0.0", "v1.0.1", validNonBreakingNorNewFeatureChangelog, false), + Entry("non-breaking with minor bump", "v1.0.0", "v1.1.0", validNonBreakingNorNewFeatureChangelog, true), + Entry("non-breaking with major bump", "v1.0.0", "v2.0.0", validNonBreakingNorNewFeatureChangelog, true), + ) }) - It("errors when not incrementing for stable api release", func() { - path := filepath.Join(changelogutils.ChangelogDirectory, nextTag, filename1) - file1 := github.CommitFile{Filename: &path, Status: &added} - cc := github.CommitsComparison{Files: []github.CommitFile{file1}} - repoClient.EXPECT(). - CompareCommits(ctx, base, sha). - Return(&cc, nil) - code.EXPECT(). - GetFileContents(ctx, path). - Return([]byte(validStableReleaseChangelog), nil) - repoClient.EXPECT(). - FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). - Return("v0.5.0", nil) - code.EXPECT(). - ListFiles(ctx, changelogutils.ChangelogDirectory). - Return([]os.FileInfo{getChangelogDir(nextTag)}, nil) - code.EXPECT(). - ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, nextTag)). - Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) - code.EXPECT(). - GetFileContents(ctx, path). - Return([]byte(validStableReleaseChangelog), nil) - - expected := changelogutils.InvalidUseOfStableApiError(nextTag) - file, err := validator.ValidateChangelog(ctx) - Expect(err.Error()).To(Equal(expected.Error())) - Expect(file).To(BeNil()) + Context("moving from 0.x to 1.x", func() { + + It("works for stable api release", func() { + path := filepath.Join(changelogutils.ChangelogDirectory, "v1.0.0", filename1) + file1 := github.CommitFile{Filename: &path, Status: &added} + cc := github.CommitsComparison{Files: []github.CommitFile{file1}} + repoClient.EXPECT(). + CompareCommits(ctx, base, sha). + Return(&cc, nil) + code.EXPECT(). + GetFileContents(ctx, path). + Return([]byte(validStableReleaseChangelog), nil).Times(2) + repoClient.EXPECT(). + FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). + Return("v0.5.0", nil) + code.EXPECT(). + ListFiles(ctx, changelogutils.ChangelogDirectory). + Return([]os.FileInfo{getChangelogDir("v1.0.0")}, nil) + code.EXPECT(). + ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, "v1.0.0")). + Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) + + file, err := validator.ValidateChangelog(ctx) + Expect(err).To(BeNil()) + Expect(file).NotTo(BeNil()) + }) + + It("errors when not incrementing for stable api release", func() { + path := filepath.Join(changelogutils.ChangelogDirectory, nextTag, filename1) + file1 := github.CommitFile{Filename: &path, Status: &added} + cc := github.CommitsComparison{Files: []github.CommitFile{file1}} + repoClient.EXPECT(). + CompareCommits(ctx, base, sha). + Return(&cc, nil) + code.EXPECT(). + GetFileContents(ctx, path). + Return([]byte(validStableReleaseChangelog), nil).Times(2) + repoClient.EXPECT(). + FindLatestTagIncludingPrereleaseBeforeSha(ctx, base). + Return("v0.5.0", nil) + code.EXPECT(). + ListFiles(ctx, changelogutils.ChangelogDirectory). + Return([]os.FileInfo{getChangelogDir(nextTag)}, nil) + code.EXPECT(). + ListFiles(ctx, filepath.Join(changelogutils.ChangelogDirectory, nextTag)). + Return([]os.FileInfo{&mockFileInfo{name: filename1, isDir: false}}, nil) + + expected := changelogutils.InvalidUseOfStableApiError(nextTag) + file, err := validator.ValidateChangelog(ctx) + Expect(err.Error()).To(Equal(expected.Error())) + Expect(file).To(BeNil()) + }) }) }) diff --git a/debugutils/mocks_kube_test.go b/debugutils/mocks_kube_test.go index 3a0ac8e1..14b82160 100644 --- a/debugutils/mocks_kube_test.go +++ b/debugutils/mocks_kube_test.go @@ -5,9 +5,10 @@ package debugutils import ( - gomock "github.com/golang/mock/gomock" io "io" reflect "reflect" + + gomock "github.com/golang/mock/gomock" ) // MockResponseWrapper is a mock of ResponseWrapper interface diff --git a/debugutils/mocks_test.go b/debugutils/mocks_test.go index bc8cfa1a..4761a1f9 100644 --- a/debugutils/mocks_test.go +++ b/debugutils/mocks_test.go @@ -5,11 +5,12 @@ package debugutils import ( + reflect "reflect" + gomock "github.com/golang/mock/gomock" kuberesource "github.com/solo-io/go-utils/installutils/kuberesource" v1 "k8s.io/api/core/v1" v10 "k8s.io/apimachinery/pkg/apis/meta/v1" - reflect "reflect" ) // MockPodFinder is a mock of PodFinder interface diff --git a/gcloudutils/builders/storage_builder.go b/gcloudutils/builders/storage_builder.go index 70c1c514..a815b3aa 100644 --- a/gcloudutils/builders/storage_builder.go +++ b/gcloudutils/builders/storage_builder.go @@ -5,7 +5,6 @@ import ( "compress/gzip" "context" "fmt" - "github.com/solo-io/go-utils/gcloudutils" "io" "io/ioutil" "net/http" @@ -13,6 +12,8 @@ import ( "strings" "time" + "github.com/solo-io/go-utils/gcloudutils" + "cloud.google.com/go/storage" "github.com/ghodss/yaml" "github.com/google/go-github/github" diff --git a/installutils/kubeinstall/cache.go b/installutils/kubeinstall/cache.go index 243458c9..6b35c812 100644 --- a/installutils/kubeinstall/cache.go +++ b/installutils/kubeinstall/cache.go @@ -43,6 +43,7 @@ func (c *Cache) getClusterResources(ctx context.Context, cfg *rest.Config, }) return currentResources, nil } + /* Initialize the cache with the snapshot of the current cluster */ diff --git a/kubeerrutils/kube_errs.go b/kubeerrutils/kube_errs.go index 5362944b..b9c21991 100644 --- a/kubeerrutils/kube_errs.go +++ b/kubeerrutils/kube_errs.go @@ -16,7 +16,6 @@ func IsImmutableErr(err error) bool { return false } - // is the error AlreadyExists && the resource is not terminating? func IsAlreadyExists(err error) bool { if err != nil { diff --git a/testutils/helper/install.go b/testutils/helper/install.go index 681ffcef..9a2c979d 100644 --- a/testutils/helper/install.go +++ b/testutils/helper/install.go @@ -17,9 +17,9 @@ import ( ) const ( - GATEWAY = "gateway" - INGRESS = "ingress" - KNATIVE = "knative" + GATEWAY = "gateway" + INGRESS = "ingress" + KNATIVE = "knative" ) // Default test configuration diff --git a/versionutils/version.go b/versionutils/version.go index 3943fa11..de2a9fef 100644 --- a/versionutils/version.go +++ b/versionutils/version.go @@ -97,7 +97,7 @@ func (v *Version) Equals(other *Version) bool { return *v == *other } -func (v *Version) IncrementVersion(breakingChange bool) *Version { +func (v *Version) IncrementVersion(breakingChange, newFeature bool) *Version { newMajor := v.Major newMinor := v.Minor newPatch := v.Patch @@ -115,10 +115,13 @@ func (v *Version) IncrementVersion(breakingChange bool) *Version { if breakingChange { newMajor = v.Major + 1 newMinor = 0 - } else { + newPatch = 0 + } else if newFeature { newMinor = v.Minor + 1 + newPatch = 0 + } else { + newPatch = v.Patch + 1 } - newPatch = 0 } return &Version{ Major: newMajor, diff --git a/versionutils/version_test.go b/versionutils/version_test.go index 59f431a0..7e207cce 100644 --- a/versionutils/version_test.go +++ b/versionutils/version_test.go @@ -109,18 +109,24 @@ var _ = Describe("Version", func() { var _ = Context("IncrementVersion", func() { - expectResult := func(start *versionutils.Version, breakingChange bool, expected *versionutils.Version) { - actualIncremented := start.IncrementVersion(breakingChange) + expectResult := func(start *versionutils.Version, breakingChange bool, newFeature bool, expected *versionutils.Version) { + actualIncremented := start.IncrementVersion(breakingChange, newFeature) Expect(actualIncremented).To(BeEquivalentTo(expected)) } It("works", func() { - expectResult(getVersion(0, 0, 1), true, getVersion(0, 1, 0)) - expectResult(getVersion(0, 1, 10), true, getVersion(0, 2, 0)) - expectResult(getVersion(1, 1, 10), true, getVersion(2, 0, 0)) - expectResult(getVersion(0, 0, 1), false, getVersion(0, 0, 2)) - expectResult(getVersion(0, 1, 10), false, getVersion(0, 1, 11)) - expectResult(getVersion(1, 1, 10), false, getVersion(1, 2, 0)) + expectResult(getVersion(0, 0, 1), true, true, getVersion(0, 1, 0)) + expectResult(getVersion(0, 0, 1), true, false, getVersion(0, 1, 0)) + expectResult(getVersion(0, 1, 10), true, false, getVersion(0, 2, 0)) + expectResult(getVersion(0, 1, 10), true, true, getVersion(0, 2, 0)) + expectResult(getVersion(1, 1, 10), true, false, getVersion(2, 0, 0)) + expectResult(getVersion(1, 1, 10), true, true, getVersion(2, 0, 0)) + expectResult(getVersion(0, 0, 1), false, false, getVersion(0, 0, 2)) + expectResult(getVersion(0, 0, 1), false, true, getVersion(0, 0, 2)) + expectResult(getVersion(0, 1, 10), false, false, getVersion(0, 1, 11)) + expectResult(getVersion(0, 1, 10), false, true, getVersion(0, 1, 11)) + expectResult(getVersion(1, 1, 10), false, false, getVersion(1, 1, 11)) + expectResult(getVersion(1, 1, 10), false, true, getVersion(1, 2, 0)) }) })