Skip to content

Commit

Permalink
Merge pull request #4 from mamachanko/topic/mamachanko/master/x-in-pr…
Browse files Browse the repository at this point in the history
…erelease

Don't treat x in pre-release and build metadata identifiers as a wild…
  • Loading branch information
joaopapereira authored Apr 2, 2024
2 parents 5409682 + e1ecfdc commit beb83fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
20 changes: 15 additions & 5 deletions v4/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,12 @@ func expandWildcardVersion(parts [][]string) ([][]string, error) {
for _, p := range parts {
var newParts []string
for _, ap := range p {
if strings.Contains(ap, "x") {
opStr, vStr, err := splitComparatorVersion(ap)
if err != nil {
return nil, err
}
opStr, vStr, err := splitComparatorVersion(ap)
if err != nil {
return nil, err
}

if containsWildcard(ap) {
versionWildcardType := getWildcardType(vStr)
flatVersion := createVersionFromWildcard(vStr)

Expand Down Expand Up @@ -381,6 +381,16 @@ func expandWildcardVersion(parts [][]string) ([][]string, error) {
return expandedParts, nil
}

// containsWildcard returns true if there's a wildcard in any of the major, minor or patch components
func containsWildcard(v string) bool {
return strings.Contains(trimIdentifiers(v), ".x")
}

// trimIdentifiers removes any pre-release and build metadata from a version
func trimIdentifiers(v string) string {
return strings.Split(strings.Split(v, "+")[0], "-")[0]
}

func parseComparator(s string) comparator {
switch s {
case "==":
Expand Down
14 changes: 14 additions & 0 deletions v4/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func TestGetWildcardType(t *testing.T) {
{"x", majorWildcard},
{"1.x", minorWildcard},
{"1.2.x", patchWildcard},
{"1.2.3-x", noneWildcard},
{"fo.o.b.ar", noneWildcard},
}

Expand Down Expand Up @@ -387,6 +388,19 @@ func TestParseRange(t *testing.T) {
{"1.2.3", true},
{"1.2.4", false},
}},
{"1.2.3-x.x", []tv{
{"1.2.3-x.y", false},
{"1.2.3-x.x", true},
}},
{"1.2.3+x.x", []tv{
{"1.2.3+x.y", false},
{"1.2.3+x.x", true},
}},
{"1.2.3-x.x+x.x", []tv{
{"1.2.3-x.x+x.y", false},
{"1.2.3-x.y+x.x", false},
{"1.2.3-x.x+x.x", true},
}},
{"=1.2.3", []tv{
{"1.2.2", false},
{"1.2.3", true},
Expand Down

0 comments on commit beb83fb

Please sign in to comment.