1
0
mirror of https://github.com/go-task/task.git synced 2025-08-08 22:36:57 +02:00

feat: bump minor version when repo is dirty or untagged

This commit is contained in:
Pete Davison
2025-08-06 19:45:37 +00:00
parent 26ef693417
commit 3c30a8066d
2 changed files with 66 additions and 2 deletions

View File

@ -4,6 +4,8 @@ import (
_ "embed"
"runtime/debug"
"strings"
"github.com/Masterminds/semver/v3"
)
var (
@ -46,6 +48,10 @@ func getCommit(info *debug.BuildInfo) string {
// However, it can also be overridden at build time using:
// -ldflags="-X 'github.com/go-task/task/v3/internal/version.version=vX.X.X'".
func GetVersion() string {
// If its a development version, we bump the minor version.
if commit != "" || dirty {
return semver.MustParse(version).IncMinor().String()
}
return version
}
@ -61,7 +67,7 @@ func GetVersionWithBuildInfo() string {
buildMetadata = append(buildMetadata, "dirty")
}
if len(buildMetadata) > 0 {
return version + "+" + strings.Join(buildMetadata, ".")
return GetVersion() + "+" + strings.Join(buildMetadata, ".")
}
return version
return GetVersion()
}

View File

@ -0,0 +1,58 @@
package version
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestVersionTxt(t *testing.T) {
// Check that the version.txt is a valid semver version.
require.NotEmpty(t, GetVersion(), "version.txt is not semver compliant")
}
func TestGetVersion(t *testing.T) {
tests := []struct {
version string
commit string
dirty bool
want string
}{
{"1.2.3", "", false, "1.2.3"},
{"1.2.3", "", true, "1.3.0"},
{"1.2.3", "abcdefg", false, "1.3.0"},
{"1.2.3", "abcdefg", true, "1.3.0"},
}
for _, tt := range tests {
version = tt.version
commit = tt.commit
dirty = tt.dirty
t.Run(tt.want, func(t *testing.T) {
require.Equal(t, tt.want, GetVersion())
})
}
}
func TestGetVersionWithBuildInfo(t *testing.T) {
tests := []struct {
version string
commit string
dirty bool
want string
}{
{"1.2.3", "", false, "1.2.3"},
{"1.2.3", "", true, "1.3.0+dirty"},
{"1.2.3", "abcdefg", false, "1.3.0+abcdefg"},
{"1.2.3", "abcdefg", true, "1.3.0+abcdefg.dirty"},
}
for _, tt := range tests {
version = tt.version
commit = tt.commit
dirty = tt.dirty
t.Run(tt.want, func(t *testing.T) {
require.Equal(t, tt.want, GetVersionWithBuildInfo())
})
}
}