Skip to content

Commit

Permalink
fix: --force-patch-increment
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Mar 9, 2023
1 parent e67acc8 commit fb2c56e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
22 changes: 18 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
current, err := getCurrentVersion(tag)
app.FatalIfError(err, "could not get current version from tag: '%s'", tag)

result, err := nextVersion(cmd, current, tag, *preRelease, *build)
result, err := nextVersion(cmd, current, tag, *preRelease, *build, *forcePatchIncrement)
app.FatalIfError(err, "could not get next tag: '%s'", tag)

if *stripPrefix {
Expand All @@ -50,7 +50,23 @@ func main() {
fmt.Println(*prefix + result.String())
}

func nextVersion(cmd string, current *semver.Version, tag, preRelease, build string) (semver.Version, error) {
func nextVersion(cmd string, current *semver.Version, tag, preRelease, build string, force bool) (semver.Version, error) {
if cmd == currentCmd.FullCommand() {
return *current, nil
}

if force {
c, err := current.SetMetadata("")
if err != nil {
return c, err
}
c, err = c.SetPrerelease("")
if err != nil {
return c, err
}
current = &c
}

var result semver.Version
switch cmd {
case nextCmd.FullCommand():
Expand All @@ -61,8 +77,6 @@ func nextVersion(cmd string, current *semver.Version, tag, preRelease, build str
result = current.IncMinor()
case patchCmd.FullCommand():
result = current.IncPatch()
case currentCmd.FullCommand():
result = *current
}

var err error
Expand Down
70 changes: 36 additions & 34 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,115 +24,117 @@ func TestCmd(t *testing.T) {
ver := func() *semver.Version { return semver.MustParse("1.2.3-pre+123") }
t.Run(currentCmd.FullCommand(), func(t *testing.T) {
cmd := currentCmd.FullCommand()
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "")
is.NoErr(err)
is.Equal("1.2.3", v.String())
})
t.Run("build", func(t *testing.T) {
t.Run("version has meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124")
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "", false)
is.NoErr(err)
is.Equal("1.2.3+124", v.String())
is.Equal("1.2.3-pre+123", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "")
is.NoErr(err)
is.Equal("1.2.3-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
t.Run("version is clean", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125")
v, err := nextVersion(cmd, semver.MustParse("v1.2.3"), "v1.2.3", "doesnt matter", "nope", true)
is.NoErr(err)
is.Equal("1.2.3-alpha.2+125", v.String())
is.Equal("1.2.3", v.String())
})
})

t.Run(minorCmd.FullCommand(), func(t *testing.T) {
cmd := minorCmd.FullCommand()
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "")
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "", false)
is.NoErr(err)
is.Equal("1.3.0", v.String())
})
t.Run("build", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124")
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124", false)
is.NoErr(err)
is.Equal("1.3.0+124", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "")
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "", false)
is.NoErr(err)
is.Equal("1.3.0-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125")
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125", false)
is.NoErr(err)
is.Equal("1.3.0-alpha.2+125", v.String())
})
})

t.Run(patchCmd.FullCommand(), func(t *testing.T) {
cmd := patchCmd.FullCommand()
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "", "")
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "", "", false)
is.NoErr(err)
is.Equal("1.2.4", v.String())
})
t.Run("original had no meta", func(t *testing.T) {
t.Run("previous had meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3", "", "")
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3", "", "", false)
is.NoErr(err)
is.Equal("1.2.3", v.String())
})
t.Run("previous had meta, force", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3", "", "", true)
is.NoErr(err)
is.Equal("1.2.4", v.String())
})
t.Run("previous had meta, force, add meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3-alpha.1+1", "alpha.2", "10", true)
is.NoErr(err)
is.Equal("1.2.4-alpha.2+10", v.String())
})
t.Run("build", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "", "124")
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "", "124", false)
is.NoErr(err)
is.Equal("1.2.4+124", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "alpha.1", "")
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "alpha.1", "", false)
is.NoErr(err)
is.Equal("1.2.4-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "alpha.2", "125")
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "alpha.2", "125", false)
is.NoErr(err)
is.Equal("1.2.4-alpha.2+125", v.String())
})
})

t.Run(majorCmd.FullCommand(), func(t *testing.T) {
cmd := majorCmd.FullCommand()
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "")
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "", false)
is.NoErr(err)
is.Equal("2.0.0", v.String())
})
t.Run("build", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124")
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124", false)
is.NoErr(err)
is.Equal("2.0.0+124", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "")
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "", false)
is.NoErr(err)
is.Equal("2.0.0-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125")
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125", false)
is.NoErr(err)
is.Equal("2.0.0-alpha.2+125", v.String())
})
Expand All @@ -141,12 +143,12 @@ func TestCmd(t *testing.T) {
t.Run("errors", func(t *testing.T) {
t.Run("invalid build", func(t *testing.T) {
is := is.New(t)
_, err := nextVersion(minorCmd.FullCommand(), semver.MustParse("1.2.3"), "v1.2.3", "", "+125")
_, err := nextVersion(minorCmd.FullCommand(), semver.MustParse("1.2.3"), "v1.2.3", "", "+125", false)
is.True(err != nil)
})
t.Run("invalid prerelease", func(t *testing.T) {
is := is.New(t)
_, err := nextVersion(minorCmd.FullCommand(), semver.MustParse("1.2.3"), "v1.2.3", "+aaa", "")
_, err := nextVersion(minorCmd.FullCommand(), semver.MustParse("1.2.3"), "v1.2.3", "+aaa", "", false)
is.True(err != nil)
})
})
Expand Down

0 comments on commit fb2c56e

Please sign in to comment.