Enhance versionning on build packages (#165)
It was too challenging to only use "tags" without a prefix to trigger builds. So, the new rule is to prefix releases tags by "releases/" Reviewed-on: #165
This commit is contained in:
@@ -3,6 +3,7 @@ package generator
|
||||
import (
|
||||
"regexp"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Version is the version of katenary. It is set at compile time.
|
||||
@@ -14,8 +15,15 @@ var Version = "master" // changed at compile time
|
||||
func GetVersion() string {
|
||||
// try to get the semantic version from the Version variable (theorically set at compile time)
|
||||
if reg := regexp.MustCompile(`^v?\d+.\d+.\d+.*|^release-.*`); reg.MatchString(Version) {
|
||||
Version = strings.Replace(Version, "release-", "v", 1)
|
||||
if Version[0] != 'v' {
|
||||
Version = "v" + Version
|
||||
}
|
||||
return Version
|
||||
}
|
||||
if reg := regexp.MustCompile(`^releases/.*`); reg.MatchString(Version) {
|
||||
return strings.Replace(Version, "releases/", "v", 1)
|
||||
}
|
||||
|
||||
// OK... let's try to get the version from the build info
|
||||
// get the version from the build info (when installed with go install)
|
||||
|
@@ -6,29 +6,53 @@ import (
|
||||
)
|
||||
|
||||
func TestVersion(t *testing.T) {
|
||||
expected := "v1.0.0"
|
||||
|
||||
errorMessage := func(value, expected any) string {
|
||||
return "Expected version to be " + expected.(string) + ", got " + value.(string)
|
||||
}
|
||||
|
||||
// first, let's test the default behavior
|
||||
// when we compile from source, without setting the Version variable,
|
||||
|
||||
// by default, when we are developing, the Version variable is set to "master"
|
||||
// we build on "devel" branch
|
||||
v := GetVersion()
|
||||
// by default, the version comes from build info and it's a development version
|
||||
if !strings.Contains(v, "(devel)") {
|
||||
t.Errorf("Expected version to be set, got %s", v)
|
||||
errorMessage(v, "(devel)")
|
||||
}
|
||||
|
||||
// now, imagine we are on a release branch
|
||||
Version = "1.0.0"
|
||||
v = GetVersion()
|
||||
if !strings.Contains(v, "1.0.0") {
|
||||
t.Errorf("Expected version to be set, got %s", v)
|
||||
if !strings.Contains(v, expected) {
|
||||
errorMessage(v, expected)
|
||||
}
|
||||
// now, imagine we are on v1.0.0
|
||||
Version = "v1.0.0"
|
||||
v = GetVersion()
|
||||
if !strings.Contains(v, "v1.0.0") {
|
||||
t.Errorf("Expected version to be set, got %s", v)
|
||||
if !strings.Contains(v, expected) {
|
||||
errorMessage(v, expected)
|
||||
}
|
||||
// we can also compile a release branch
|
||||
Version = "release-1.0.0"
|
||||
v = GetVersion()
|
||||
if !strings.Contains(v, "release-1.0.0") {
|
||||
t.Errorf("Expected version to be set, got %s", v)
|
||||
if !strings.Contains(v, expected) {
|
||||
errorMessage(v, expected)
|
||||
}
|
||||
|
||||
// for releases-* tags
|
||||
Version = "release-1.0.0"
|
||||
v = GetVersion()
|
||||
if !strings.Contains(v, expected) {
|
||||
errorMessage(v, expected)
|
||||
}
|
||||
|
||||
// and for releases/* tags
|
||||
Version = "releases/1.0.0"
|
||||
v = GetVersion()
|
||||
if !strings.Contains(v, expected) {
|
||||
errorMessage(v, expected)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user