Skip to content

Commit

Permalink
Add release bump strategy and related unit tests
Browse files Browse the repository at this point in the history
Also add unit tests for pre-new-* type strategies
  • Loading branch information
kylie-bee committed Jan 14, 2025
1 parent 0ac04ee commit 9d04630
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ relevant files, reducing the risk of human error and saving you valuable time.
0.0.0 ─┬─ major ─ 1.0.0
├─ minor ─ 0.1.0
├─ patch ─ 0.0.1
├─ release ─ 0.0.0
├─ new-pre-major ─ 1.0.0-alpha
├─ new-pre-minor ─ 0.1.0-alpha
├─ new-pre-patch ─ 0.0.1-alpha
Expand Down Expand Up @@ -104,6 +105,7 @@ relevant files, reducing the risk of human error and saving you valuable time.
0.0.1-alpha ─┬─ major ─ 1.0.0
├─ minor ─ 0.1.0
├─ patch ─ 0.0.2
├─ release ─ 0.0.1
├─ new-pre-major ─ 1.0.0-alpha
├─ new-pre-minor ─ 0.1.0-alpha
├─ new-pre-patch ─ 0.0.2-alpha
Expand Down Expand Up @@ -259,6 +261,7 @@ Available Commands:
new-pre-minor Bump the minor version and apply the first pre-release label (e.g. 1.2.3 -> 1.3.0-alpha).
new-pre-patch Bump the patch version and apply the first pre-release label (e.g. 1.2.3 -> 1.2.4-alpha).
patch Bump the patch version number (e.g. 1.2.3 -> 1.2.4).
release Bump the pre-release version to a release version (e.g. 1.2.3-alpha -> 1.2.3).
pre Bump the next pre-release version label (e.g. 1.2.3-alpha -> 1.2.3-beta).
pre-build Bump the pre-release build version number (e.g. 1.2.3 -> 1.2.3+build.1).
pre-major Bump the pre-release major version number (e.g. 1.2.3-alpha -> 1.2.3-alpha.1).
Expand All @@ -270,7 +273,7 @@ Available Commands:

```

The commands `major`, `minor` `patch`, `set`, `new-pre-major`, `new-pre-minor`, `new-pre-patch`, `pre`, `pre-major`,
The commands `major`, `minor` `patch`, `release`, `set`, `new-pre-major`, `new-pre-minor`, `new-pre-patch`, `pre`, `pre-major`,
`pre-minor`, `pre-patch` and `pre-build` support the following flags:'
- `-c`, `-config`: Path to the configuration file (default: `./versionbump.yaml`).
- `-no-prompt`: Do not prompt the user for confirmation before making changes.
Expand Down Expand Up @@ -495,6 +498,7 @@ Without parameters, the `show` command will display the potential versioning pat
$ 0.6.0-alpha.1 ─┬─ major ─ 1.0.0
├─ minor ─ 0.7.0
├─ patch ─ 0.6.1
├─ release ─ 0.6.0
├─ pre-new-major ─ 1.0.0-alpha
├─ pre-new-minor ─ 0.7.0-alpha
├─ pre-new-patch ─ 0.6.1-alpha
Expand All @@ -512,6 +516,7 @@ Potential versioning paths for version: 0.4.1-alpha
0.4.1-alpha ─┬─ major ─ 1.0.0
├─ minor ─ 0.5.0
├─ patch ─ 0.4.2
├─ release ─ 0.4.1
├─ pre-new-major ─ 1.0.0-alpha
├─ pre-new-minor ─ 0.5.0-alpha
├─ pre-new-patch ─ 0.4.2-alpha
Expand All @@ -531,6 +536,9 @@ considered valid semantic version numbers.
```console
$ versionbump history
version History:
- 0.7.0
- 0.7.0-alpha
- 0.6.0
- 0.6.0-alpha.1
- 0.6.0-alpha
- 0.5.4
Expand Down
18 changes: 16 additions & 2 deletions cmd/versionbump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package main

import (
"fmt"
"os"

"github.com/ptgoetz/go-versionbump/internal"
vbc "github.com/ptgoetz/go-versionbump/internal/config"
"github.com/ptgoetz/go-versionbump/pkg/semver"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"os"
)

func main() {
Expand Down Expand Up @@ -131,7 +132,7 @@ var gitTagHistoryCmd = &cobra.Command{
var preReleaseNextCmd = &cobra.Command{
Use: semver.PreRelease.String(),
Short: `Bump the next pre-release version label (e.g. 1.2.3-alpha -> 1.2.3-beta).`,
Long: `Bump the patch version number (e.g. 1.2.3 -> 1.2.4).`,
Long: `Bump the next pre-release version label (e.g. 1.2.3-alpha -> 1.2.3-beta).`,
RunE: bumpPreReleaseNext, // Use RunE for better error handling
}

Expand Down Expand Up @@ -177,6 +178,13 @@ var preReleaseNewPatchCmd = &cobra.Command{
RunE: bumpNewPreReleasePatch, // Use RunE for better error handling
}

var releaseCmd = &cobra.Command{
Use: semver.Release.String(),
Short: `Bump the pre-release version to a release version (e.g. 1.2.3-alpha -> 1.2.3).`,
Long: `Bump the pre-release version to a release version (e.g. 1.2.3-alpha -> 1.2.3).`,
RunE: bumpRelease, // Use RunE for better error handling
}

var preReleaseBuildCmd = &cobra.Command{
Use: semver.PreReleaseBuild.String(),
Short: `Bump the pre-release build version number (e.g. 1.2.3 -> 1.2.3+build.1).`,
Expand Down Expand Up @@ -216,6 +224,7 @@ func init() {
preReleaseNewMajorCmd.Flags().AddFlagSet(prereleaserFlags)
preReleaseNewMinorCmd.Flags().AddFlagSet(prereleaserFlags)
preReleaseNewPatchCmd.Flags().AddFlagSet(prereleaserFlags)
releaseCmd.Flags().AddFlagSet(prereleaserFlags)

showCmd.Flags().AddFlagSet(configColorFlags)
showVersionCmd.Flags().AddFlagSet(commonFlags)
Expand All @@ -239,6 +248,7 @@ func init() {
rootCmd.AddCommand(preReleaseNewMajorCmd)
rootCmd.AddCommand(preReleaseNewMinorCmd)
rootCmd.AddCommand(preReleaseNewPatchCmd)
rootCmd.AddCommand(releaseCmd)
rootCmd.AddCommand(showCmd)
rootCmd.AddCommand(showVersionCmd)
rootCmd.AddCommand(showLatestCmd)
Expand Down Expand Up @@ -299,6 +309,10 @@ func bumpNewPreReleasePatch(cmd *cobra.Command, args []string) error {
return runVersionBump(semver.PreReleaseNewPatch)
}

func bumpRelease(cmd *cobra.Command, args []string) error {
return runVersionBump(semver.Release)
}

func runResetCmd(cmd *cobra.Command, args []string) error {
opts.ResetVersion = args[0]
vb, err := internal.NewVersionBump(opts)
Expand Down
15 changes: 11 additions & 4 deletions internal/versionbump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package internal
import (
"bufio"
"fmt"
"os"
"path"
"strings"
"text/template"

"github.com/ptgoetz/go-versionbump/internal/config"
"github.com/ptgoetz/go-versionbump/internal/git"
"github.com/ptgoetz/go-versionbump/internal/utils"
vbu "github.com/ptgoetz/go-versionbump/internal/utils"
"github.com/ptgoetz/go-versionbump/pkg/semver"
"gopkg.in/yaml.v2"
"os"
"path"
"strings"
"text/template"
)

const Version = "1.0.0-alpha"
Expand Down Expand Up @@ -116,6 +117,9 @@ func (vb *VersionBump) Show(versionStr string) error {
patchVersion, err := curVersion.Bump(semver.Patch, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
patchVersionStr := checkBumpError(vb, patchVersion, err)

releaseVersion, err := curVersion.Bump(semver.Release, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
releaseVersionStr := checkBumpError(vb, releaseVersion, err)

prNextVersion, err := curVersion.Bump(semver.PreRelease, vb.Config.PreReleaseLabels, vb.Config.BuildLabel)
prNextVersionStr := checkBumpError(vb, prNextVersion, err)

Expand Down Expand Up @@ -147,6 +151,7 @@ func (vb *VersionBump) Show(versionStr string) error {
`%s ─┬─ major ─ %s
%s├─ minor ─ %s
%s├─ patch ─ %s
%s├─ release ─ %s
%s├─ new-pre-major ─ %s
%s├─ new-pre-minor ─ %s
%s├─ new-pre-patch ─ %s
Expand All @@ -163,6 +168,8 @@ func (vb *VersionBump) Show(versionStr string) error {
padding,
patchVersionStr,
padding,
releaseVersionStr,
padding,
prNewMajorVersionStr,
padding,
prNewMinorVersionStr,
Expand Down
13 changes: 11 additions & 2 deletions pkg/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ package semver

import (
"fmt"
"github.com/ptgoetz/go-versionbump/internal/utils"
"sort"
"strings"

"github.com/ptgoetz/go-versionbump/internal/utils"
)

const (
vMajor = iota
vMinor
vPatch
vRelease
prNext
prMajor
prNewMajor
Expand All @@ -30,6 +32,7 @@ const (
Major BumpStrategy = "major"
Minor BumpStrategy = "minor"
Patch BumpStrategy = "patch"
Release BumpStrategy = "release"
PreRelease BumpStrategy = "pre"
PreReleaseMajor BumpStrategy = "pre-major"
PreReleaseMinor BumpStrategy = "pre-minor"
Expand All @@ -52,6 +55,8 @@ func versionPartInt(part BumpStrategy) int {
return vMinor
case Patch:
return vPatch
case Release:
return vRelease
case PreRelease:
return prNext
case PreReleaseMajor:
Expand Down Expand Up @@ -128,7 +133,7 @@ func (v *SemanticVersion) Bump(strategy BumpStrategy, preReleaseLabels []string,
preReleaseVersion = newPrereleaseVersion("", 0, 0, 0)
case versionPart >= prNewMajor && versionPart <= prNewPatch:
// bump the root version
version = v.rootVersion.bump(versionPart - 5)
version = v.rootVersion.bump(versionPart - 6)
// reset all pre-release versions
preReleaseVersion = newPrereleaseVersion(preReleaseLabels[0], 0, 0, 0)
case versionPart >= prNext && versionPart <= prPatch:
Expand All @@ -145,6 +150,10 @@ func (v *SemanticVersion) Bump(strategy BumpStrategy, preReleaseLabels []string,
} else {
build = newBuild(buildLabel, 1)
}
case versionPart == vRelease:
version = v.rootVersion
preReleaseVersion = nil
build = nil
default:
return nil, fmt.Errorf("invalid version strategy: %d", versionPart)
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/semver/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package semver

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
sv "golang.org/x/mod/semver"
"testing"
)

func TestParseSemVersion(t *testing.T) {
Expand Down Expand Up @@ -57,13 +58,20 @@ func TestSemVersion_Bump(t *testing.T) {
{"0.0.1", Minor, "0.1.0", false},
{"0.1.0", Major, "1.0.0", false},
{"1.0.0", PreRelease, "1.0.0-alpha", false},
{"1.0.0-alpha+ptgoetz.1", PreRelease, "1.0.0-beta", false},
{"1.0.0", PreReleaseMajor, "1.0.0-alpha", false},
{"1.0.0-beta", PreReleaseMajor, "1.0.0-beta.1", false},
{"1.0.0-alpha", PreReleaseBuild, "1.0.0-alpha+ptgoetz.1", false},
{"1.0.0-alpha+ptgoetz.1", PreReleaseBuild, "1.0.0-alpha+ptgoetz.2", false},
{"1.0.0-alpha+ptgoetz.1", PreRelease, "1.0.0-beta", false},
{"1.0.0-beta", PreReleasePatch, "1.0.0-beta.0.0.1", false},
{"1.0.0-beta", PreReleaseMinor, "1.0.0-beta.0.1", false},
{"1.0.0-beta", PreReleaseMajor, "1.0.0-beta.1", false},
{"1.0.0", PreReleaseNewMajor, "2.0.0-alpha", false},
{"1.0.0-alpha", PreReleaseNewMajor, "2.0.0-alpha", false},
{"1.0.0", PreReleaseNewMinor, "1.1.0-alpha", false},
{"1.0.0-alpha", PreReleaseNewMinor, "1.1.0-alpha", false},
{"1.0.0", PreReleaseNewPatch, "1.0.1-alpha", false},
{"1.0.0-alpha", PreReleaseNewPatch, "1.0.1-alpha", false},
{"1.0.0-beta", Release, "1.0.0", false},
}

for _, test := range tests {
Expand Down

0 comments on commit 9d04630

Please sign in to comment.