Skip to content

Commit

Permalink
feat: Reverting support for prefix, semver strict only
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed Mar 4, 2021
1 parent 0868faf commit 119ef7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
23 changes: 9 additions & 14 deletions pkg/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ const (
alpha NonFinalVersion = iota + 1
beta
canary
edge
rc
test
)

var (
// According to https://semver.org/#spec-item-11
semverMatcher = regexp.MustCompile(`(?i)^([a-zA-Z-]*)([0-9]+)\.([0-9]+)(?:\.([0-9]+))?(-[a-zA-Z0-9.]+)?(?:\+[a-zA-Z0-9.]+)?$`)
semverMatcher = regexp.MustCompile(`(?i)^[a-zA-Z]*([0-9]+)\.([0-9]+)(?:\.([0-9]+))?(-[a-zA-Z0-9.]+)?(?:\+[a-zA-Z0-9.]+)?$`)

nonFinalVersions = []string{"alpha", "beta", "canary", "edge", "rc", "test"}
nonFinalVersions = []string{"alpha", "beta", "canary", "rc", "test"}

// NoneVersion is the empty semver
NoneVersion = Version{}
Expand Down Expand Up @@ -102,20 +101,20 @@ func Parse(version string) (Version, error) {
}
var err error

semver.major, err = strconv.ParseUint(matches[2], 10, 64)
semver.major, err = strconv.ParseUint(matches[1], 10, 64)
if err != nil {
return NoneVersion, fmt.Errorf("version major is not numeric")
}

if len(matches[3]) != 0 {
semver.minor, err = strconv.ParseUint(matches[3], 10, 64)
if len(matches[2]) != 0 {
semver.minor, err = strconv.ParseUint(matches[2], 10, 64)
if err != nil {
return NoneVersion, fmt.Errorf("version minor is not numeric")
}
}

if len(matches[4]) != 0 {
semver.patch, err = strconv.ParseUint(matches[4], 10, 64)
if len(matches[3]) != 0 {
semver.patch, err = strconv.ParseUint(matches[3], 10, 64)
if err != nil {
return NoneVersion, fmt.Errorf("version patch is not numeric")
}
Expand All @@ -125,16 +124,12 @@ func Parse(version string) (Version, error) {
}

func parseNonFinalVersion(matches []string) NonFinalVersion {
if len(matches[1]) <= 1 && len(matches[5]) == 0 {
if len(matches[4]) == 0 {
return -1
}

for index, nonFinalVersion := range nonFinalVersions {
if strings.Contains(matches[1], nonFinalVersion) {
return NonFinalVersion(index + 1)
}

if len(matches[5]) != 0 && strings.Contains(matches[5], nonFinalVersion) {
if strings.Contains(matches[4], nonFinalVersion) {
return NonFinalVersion(index + 1)
}
}
Expand Down
12 changes: 2 additions & 10 deletions pkg/semver/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,8 @@ func TestParse(t *testing.T) {
args{
version: "stable-2.10.1",
},
Version{"stable-2.10.1", 2, 10, 1, 0},
nil,
},
{
"edge version",
args{
version: "edge-2.10.1",
},
Version{"edge-2.10.1", 2, 10, 1, edge},
nil,
NoneVersion,
errors.New("unable to parse version"),
},
{
"flag rc version",
Expand Down

0 comments on commit 119ef7f

Please sign in to comment.