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