Skip to content

Commit

Permalink
Fixed Part of SemVer Issue (#1157)
Browse files Browse the repository at this point in the history
* Fixed Part of SemVer Issue

* Fixes the first part of #1087
* Fixed the error for `versionRange` having a tilde `"~"` and wildcard `"x"` (ie. `"versionRange": "~0.10.x"`).
* Also included a test for tilde and wildcard

Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>

* Intial commit

Re-did regex and inturn updated a lot of stuff

Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>

Removed extra line

Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>

* Updated based on code review

Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>

* Updated for caret test cases

Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>

---------

Signed-off-by: nathannaveen <42319948+nathannaveen@users.noreply.github.com>
  • Loading branch information
nathannaveen committed Aug 23, 2023
1 parent 2290eb0 commit 655342e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
23 changes: 18 additions & 5 deletions pkg/misc/depversion/depversion.go
Expand Up @@ -255,7 +255,7 @@ func parseSemverHelper(re *regexp.Regexp, s string) (semver, major, minor, patch
matches := re.FindStringSubmatch(s)

if len(matches) == 0 {
err = fmt.Errorf("Did not match regex: %q %s", s, re)
err = fmt.Errorf("did not match regex: %q %s", s, re)
return
}
semverIdx := re.SubexpIndex("semver")
Expand Down Expand Up @@ -330,9 +330,22 @@ func getConstraint(s string) (string, error) {
}
return "=" + s, nil
}

// ignoring the tilde for the wildcard check
wildcardVersion := strings.TrimPrefix(s, "~")
// ignoring the ^ for the wildcard check, but using it during the parsing
wildcardVersion = strings.TrimPrefix(wildcardVersion, "^")

// check for 1.x minor and patch versions
if isSemVerWildcard(s) {
_, major, minor, _, _, _, err := parseWildcardSemver(s)
if isSemVerWildcard(wildcardVersion) {
if strings.HasPrefix(s, "^") {
split := strings.Split(wildcardVersion, ".")
if len(split) == 3 {
wildcardVersion = fmt.Sprintf("%s.%s", split[0], split[2])
}
}

_, major, minor, _, _, _, err := parseWildcardSemver(wildcardVersion)
if err != nil {
return "", fmt.Errorf("unable to parse semver with wildcard: %v", err)
}
Expand All @@ -353,7 +366,7 @@ func getConstraint(s string) (string, error) {

// NPM ^ for major versions
if strings.HasPrefix(s, "^") {
version := strings.TrimPrefix(s, ("^"))
version := strings.TrimPrefix(s, "^")
semver, major, _, _, _, _, err := parseSemver(version)
if err != nil {
return "", fmt.Errorf("unable to parse semver %v", err)
Expand All @@ -365,7 +378,7 @@ func getConstraint(s string) (string, error) {

// NPM ~ for minor version
if strings.HasPrefix(s, "~") {
version := strings.TrimPrefix(s, ("~"))
version := strings.TrimPrefix(s, "~")
semver, major, minor, _, _, _, err := parseSemver(version)
if err != nil {
return "", fmt.Errorf("unable to parse semver %v", err)
Expand Down
34 changes: 33 additions & 1 deletion pkg/misc/depversion/depversion_test.go
Expand Up @@ -143,6 +143,30 @@ func Test_VersionRangeParse(t *testing.T) {
},
},
},
{
input: "^1.0.x",
expect: VersionMatchObject{
VRSet: []VersionRange{
{">=1.0.0,<2.0.0"},
},
},
},
{
input: "^1.x",
expect: VersionMatchObject{
VRSet: []VersionRange{
{">=1.0.0,<2.0.0"},
},
},
},
{
input: "^1.3.x",
expect: VersionMatchObject{
VRSet: []VersionRange{
{">=1.0.0,<2.0.0"},
},
},
},
{
input: "v0.0.0-20190603091049-60506f45cf65",
expect: VersionMatchObject{
Expand Down Expand Up @@ -242,6 +266,14 @@ func Test_VersionRangeParse(t *testing.T) {
},
},
},
{
input: "~0.10.x",
expect: VersionMatchObject{
VRSet: []VersionRange{
{">=0.10.0,<0.11.0"},
},
},
},
{
// special case latest set to no constraint
input: "latest",
Expand All @@ -262,7 +294,7 @@ func Test_VersionRangeParse(t *testing.T) {

got, err := ParseVersionRange(tt.input)
if err != nil {
t.Errorf("got unexpected err: %v", err)
t.Errorf("got unexpected err from ParseVersionRange: %v", err)
return
}

Expand Down

0 comments on commit 655342e

Please sign in to comment.