Skip to content

Commit

Permalink
feat: increment pre-release without pre-args
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoh86 committed Dec 19, 2024
1 parent 480ade9 commit 33ef2f1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (m *Manager) update(
}

func (m *Manager) Release(build, msg []string, file string) (string, string, error) {
return m.release(build, msg, file, func(u Updater) UpdateBuild { return u.Pre() })
return m.release(build, msg, file, func(u Updater) UpdateBuild { return u.Release() })
}

func (m *Manager) Build(build, msg []string, file string) (string, string, error) {
Expand Down
13 changes: 13 additions & 0 deletions internal/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ func TestManager(t *testing.T) {
assert.Equal(t, "test1.2.3-test-pre.1+test-bld.2", next)
assert.Equal(t, "git tag -l\ngit tag --message test-msg test1.2.3-test-pre.1+test-bld.2\n", buf.String())
})
t.Run("increment pre-release", func(t *testing.T) {
buf, run, man := tset()
run.output = strings.NewReader("test1.2.3-pre-release.4+build-ver.5\n")
cur, next, err := man.UpdatePre(
nil,
[]string{"test-bld", "2"},
[]string{"test-msg"},
"")
assert.NoError(t, err)
assert.Equal(t, "test1.2.3-pre-release.4+build-ver.5", cur)
assert.Equal(t, "test1.2.3-pre-release.5+test-bld.2", next)
assert.Equal(t, "git tag -l\ngit tag --message test-msg test1.2.3-pre-release.5+test-bld.2\n", buf.String())
})
t.Run("increment patch", func(t *testing.T) {
buf, run, man := tset()
run.output = strings.NewReader("test1.2.3-pre-release.4+build-ver.5\n")
Expand Down
32 changes: 31 additions & 1 deletion internal/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Updater interface {
type UpdatePre interface {
UpdateBuild
Pre(...semver.PRVersion) UpdateBuild
Release() UpdateBuild
}

type UpdateBuild interface {
Expand Down Expand Up @@ -65,9 +66,38 @@ func (i implUpdater) Patch() UpdatePre {
return next
}

func incrementPre(pre []semver.PRVersion) []semver.PRVersion {
if len(pre) == 0 {
return nil
}
for i := len(pre) - 1; i >= 0; i-- {
if pre[i].IsNum {
ret := make([]semver.PRVersion, i+1)
copy(ret, pre[:i+1])
ret[i].VersionNum += 1
return ret
}
}
ret := make([]semver.PRVersion, len(pre)+1)
copy(ret, pre)
ret[len(pre)] = semver.PRVersion{VersionNum: 2}
return ret
}

func (i implUpdater) Pre(p ...semver.PRVersion) UpdateBuild {
next := i
next.ver.Pre = p
if len(p) == 0 {
next.ver.Pre = incrementPre(i.ver.Pre)
} else {
next.ver.Pre = p
}
next.ver.Build = nil
return next
}

func (i implUpdater) Release() UpdateBuild {
next := i
next.ver.Pre = nil
next.ver.Build = nil
return next
}
Expand Down
51 changes: 51 additions & 0 deletions internal/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,41 @@ func mustPRVer(t *testing.T, s string) semver.PRVersion {
return v
}

func TextIncrementPre(t *testing.T) {
t.Run("increment pre-release", func(t *testing.T) {
version, err := semver.Parse("1.2.3-pre-release.4+build-ver.5")
assert.NoError(t, err)
v, err := NewUpdater(version).
Pre().Version()
assert.NoError(t, err)
assert.Equal(t, "1.2.3-pre-release.5", v.String())
})
t.Run("incrementing pre-release, remaining words should be dropped", func(t *testing.T) {
version, err := semver.Parse("1.2.3-pre-release.4.x.y")
assert.NoError(t, err)
v, err := NewUpdater(version).
Pre().Version()
assert.NoError(t, err)
assert.Equal(t, "1.2.3-pre-release.5", v.String())
})
t.Run("incrementing pre-release without numeric suffix", func(t *testing.T) {
version, err := semver.Parse("1.2.3-pre-release")
assert.NoError(t, err)
v, err := NewUpdater(version).
Pre().Version()
assert.NoError(t, err)
assert.Equal(t, "1.2.3-pre-release.2", v.String())
})
t.Run("incrementing pre-release without pre-release never changes", func(t *testing.T) {
version, err := semver.Parse("1.2.3")
assert.NoError(t, err)
v, err := NewUpdater(version).
Pre().Version()
assert.NoError(t, err)
assert.Equal(t, "1.2.3", v.String())
})
}

func TestUpdater(t *testing.T) {
t.Run("source: pre-release+build-ver", func(t *testing.T) {
t.Run("set build", func(t *testing.T) {
Expand Down Expand Up @@ -45,6 +80,22 @@ func TestUpdater(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "1.2.3-beta.6", v.String())
})
t.Run("increment pre-release", func(t *testing.T) {
version, err := semver.Parse("1.2.3-pre-release.4+build-ver.5")
assert.NoError(t, err)
v, err := NewUpdater(version).
Pre().Version()
assert.NoError(t, err)
assert.Equal(t, "1.2.3-pre-release.5", v.String())
})
t.Run("incrementing pre-release, remaining words should be dropped", func(t *testing.T) {
version, err := semver.Parse("1.2.3-pre-release.4.x.y")
assert.NoError(t, err)
v, err := NewUpdater(version).
Pre().Version()
assert.NoError(t, err)
assert.Equal(t, "1.2.3-pre-release.5", v.String())
})
t.Run("increment patch", func(t *testing.T) {
version, err := semver.Parse("1.2.3-pre-release.4+build-ver.5")
assert.NoError(t, err)
Expand Down

0 comments on commit 33ef2f1

Please sign in to comment.