Skip to content

Commit

Permalink
Merge pull request #2 from scothis/pre-wildcards
Browse files Browse the repository at this point in the history
Refine wildcard behavior to support prerelease versions
  • Loading branch information
neil-hickey authored May 25, 2023
2 parents 8090ce4 + dd8f7fe commit 5409682
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Ranges can be combined by both AND and OR

- `>1.0.0 <2.0.0 || >3.0.0 !4.2.1` would match `1.2.3`, `1.9.9`, `3.1.1`, but not `4.2.1`, `2.1.1`

Ranges operating on pre release versions can produce counter intuitive results. One might expect the range `>=1.0.0 <2.0.0` to always match versions with major version `1`. However, pre release versions are less than the release version, so `1.0.0-build.1` will not match, while `2.0.0-build.1` will. If a range will operate on pre release versions, be sure to include a pre release value of `0` making the desired range `>=1.0.0-0 <2.0.0-0`. Alternatively, the range `1.x` will match all versions with major version `1`.

Range usage:

```
Expand Down
2 changes: 1 addition & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

"github.com/k14s/semver"
"github.com/carvel-dev/semver"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion v4/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"

"github.com/k14s/semver/v4"
"github.com/carvel-dev/semver/v4"
)

func main() {
Expand Down
10 changes: 5 additions & 5 deletions v4/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func (vr *versionRange) rangeFunc() Range {
// Range represents a range of versions.
// A Range can be used to check if a Version satisfies it:
//
// range, err := semver.ParseRange(">1.0.0 <2.0.0")
// range(semver.MustParse("1.1.1") // returns true
// range, err := semver.ParseRange(">1.0.0 <2.0.0")
// range(semver.MustParse("1.1.1") // returns true
type Range func(Version) bool

// OR combines the existing Range with another Range using logical OR.
Expand Down Expand Up @@ -108,7 +108,7 @@ func (rf Range) AND(f Range) Range {
//
// Ranges can be combined by both AND and OR
//
// - `>1.0.0 <2.0.0 || >3.0.0 !4.2.1` would match `1.2.3`, `1.9.9`, `3.1.1`, but not `4.2.1`, `2.1.1`
// - `>1.0.0 <2.0.0 || >3.0.0 !4.2.1` would match `1.2.3`, `1.9.9`, `3.1.1`, but not `4.2.1`, `2.1.1`
func ParseRange(s string) (Range, error) {
parts := splitAndTrim(s)
orParts, err := splitORParts(parts)
Expand Down Expand Up @@ -269,10 +269,10 @@ func createVersionFromWildcard(vStr string) string {

// handle 1.x
if len(parts) == 2 {
return vStr2 + ".0"
vStr2 = vStr2 + ".0"
}

return vStr2
return vStr2 + "-0"
}

// incrementMajorVersion will increment the major version
Expand Down
44 changes: 30 additions & 14 deletions v4/range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ func TestCreateVersionFromWildcard(t *testing.T) {
i string
s string
}{
{"1.2.x", "1.2.0"},
{"1.x", "1.0.0"},
{"1.2.x", "1.2.0-0"},
{"1.x", "1.0.0-0"},
}

for _, tc := range tests {
Expand Down Expand Up @@ -272,18 +272,18 @@ func TestExpandWildcardVersion(t *testing.T) {
o [][]string
}{
{[][]string{{"foox"}}, nil},
{[][]string{{">=1.2.x"}}, [][]string{{">=1.2.0"}}},
{[][]string{{"<=1.2.x"}}, [][]string{{"<1.3.0"}}},
{[][]string{{">1.2.x"}}, [][]string{{">=1.3.0"}}},
{[][]string{{"<1.2.x"}}, [][]string{{"<1.2.0"}}},
{[][]string{{"!=1.2.x"}}, [][]string{{"<1.2.0", ">=1.3.0"}}},
{[][]string{{">=1.x"}}, [][]string{{">=1.0.0"}}},
{[][]string{{"<=1.x"}}, [][]string{{"<2.0.0"}}},
{[][]string{{">1.x"}}, [][]string{{">=2.0.0"}}},
{[][]string{{"<1.x"}}, [][]string{{"<1.0.0"}}},
{[][]string{{"!=1.x"}}, [][]string{{"<1.0.0", ">=2.0.0"}}},
{[][]string{{"1.2.x"}}, [][]string{{">=1.2.0", "<1.3.0"}}},
{[][]string{{"1.x"}}, [][]string{{">=1.0.0", "<2.0.0"}}},
{[][]string{{">=1.2.x"}}, [][]string{{">=1.2.0-0"}}},
{[][]string{{"<=1.2.x"}}, [][]string{{"<1.3.0-0"}}},
{[][]string{{">1.2.x"}}, [][]string{{">=1.3.0-0"}}},
{[][]string{{"<1.2.x"}}, [][]string{{"<1.2.0-0"}}},
{[][]string{{"!=1.2.x"}}, [][]string{{"<1.2.0-0", ">=1.3.0-0"}}},
{[][]string{{">=1.x"}}, [][]string{{">=1.0.0-0"}}},
{[][]string{{"<=1.x"}}, [][]string{{"<2.0.0-0"}}},
{[][]string{{">1.x"}}, [][]string{{">=2.0.0-0"}}},
{[][]string{{"<1.x"}}, [][]string{{"<1.0.0-0"}}},
{[][]string{{"!=1.x"}}, [][]string{{"<1.0.0-0", ">=2.0.0-0"}}},
{[][]string{{"1.2.x"}}, [][]string{{">=1.2.0-0", "<1.3.0-0"}}},
{[][]string{{"1.x"}}, [][]string{{">=1.0.0-0", "<2.0.0-0"}}},
}

for _, tc := range tests {
Expand Down Expand Up @@ -461,6 +461,22 @@ func TestParseRange(t *testing.T) {
{"1.2.6", false},
{"1.3.0", true},
}},
{"1.3.x", []tv{
{"1.2.0", false},
{"1.3.0-0", true},
{"1.3.0", true},
{"1.3.9", true},
{"1.4.0-0", false},
{"1.4.0", false},
}},
{"2.x", []tv{
{"1.9.9", false},
{"2.0.0-0", true},
{"2.0.0", true},
{"2.9.9", true},
{"3.0.0-0", false},
{"3.0.0", false},
}},
// Combined Expressions
{">1.2.2 <1.2.4 || >=2.0.0", []tv{
{"1.2.2", false},
Expand Down

0 comments on commit 5409682

Please sign in to comment.