Skip to content

Commit 5409682

Browse files
authored
Merge pull request #2 from scothis/pre-wildcards
Refine wildcard behavior to support prerelease versions
2 parents 8090ce4 + dd8f7fe commit 5409682

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ Ranges can be combined by both AND and OR
8787

8888
- `>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`
8989

90+
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`.
91+
9092
Range usage:
9193

9294
```

examples/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/k14s/semver"
6+
"github.com/carvel-dev/semver"
77
)
88

99
func main() {

v4/examples/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/k14s/semver/v4"
6+
"github.com/carvel-dev/semver/v4"
77
)
88

99
func main() {

v4/range.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func (vr *versionRange) rangeFunc() Range {
6767
// Range represents a range of versions.
6868
// A Range can be used to check if a Version satisfies it:
6969
//
70-
// range, err := semver.ParseRange(">1.0.0 <2.0.0")
71-
// range(semver.MustParse("1.1.1") // returns true
70+
// range, err := semver.ParseRange(">1.0.0 <2.0.0")
71+
// range(semver.MustParse("1.1.1") // returns true
7272
type Range func(Version) bool
7373

7474
// OR combines the existing Range with another Range using logical OR.
@@ -108,7 +108,7 @@ func (rf Range) AND(f Range) Range {
108108
//
109109
// Ranges can be combined by both AND and OR
110110
//
111-
// - `>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`
111+
// - `>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`
112112
func ParseRange(s string) (Range, error) {
113113
parts := splitAndTrim(s)
114114
orParts, err := splitORParts(parts)
@@ -269,10 +269,10 @@ func createVersionFromWildcard(vStr string) string {
269269

270270
// handle 1.x
271271
if len(parts) == 2 {
272-
return vStr2 + ".0"
272+
vStr2 = vStr2 + ".0"
273273
}
274274

275-
return vStr2
275+
return vStr2 + "-0"
276276
}
277277

278278
// incrementMajorVersion will increment the major version

v4/range_test.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ func TestCreateVersionFromWildcard(t *testing.T) {
218218
i string
219219
s string
220220
}{
221-
{"1.2.x", "1.2.0"},
222-
{"1.x", "1.0.0"},
221+
{"1.2.x", "1.2.0-0"},
222+
{"1.x", "1.0.0-0"},
223223
}
224224

225225
for _, tc := range tests {
@@ -272,18 +272,18 @@ func TestExpandWildcardVersion(t *testing.T) {
272272
o [][]string
273273
}{
274274
{[][]string{{"foox"}}, nil},
275-
{[][]string{{">=1.2.x"}}, [][]string{{">=1.2.0"}}},
276-
{[][]string{{"<=1.2.x"}}, [][]string{{"<1.3.0"}}},
277-
{[][]string{{">1.2.x"}}, [][]string{{">=1.3.0"}}},
278-
{[][]string{{"<1.2.x"}}, [][]string{{"<1.2.0"}}},
279-
{[][]string{{"!=1.2.x"}}, [][]string{{"<1.2.0", ">=1.3.0"}}},
280-
{[][]string{{">=1.x"}}, [][]string{{">=1.0.0"}}},
281-
{[][]string{{"<=1.x"}}, [][]string{{"<2.0.0"}}},
282-
{[][]string{{">1.x"}}, [][]string{{">=2.0.0"}}},
283-
{[][]string{{"<1.x"}}, [][]string{{"<1.0.0"}}},
284-
{[][]string{{"!=1.x"}}, [][]string{{"<1.0.0", ">=2.0.0"}}},
285-
{[][]string{{"1.2.x"}}, [][]string{{">=1.2.0", "<1.3.0"}}},
286-
{[][]string{{"1.x"}}, [][]string{{">=1.0.0", "<2.0.0"}}},
275+
{[][]string{{">=1.2.x"}}, [][]string{{">=1.2.0-0"}}},
276+
{[][]string{{"<=1.2.x"}}, [][]string{{"<1.3.0-0"}}},
277+
{[][]string{{">1.2.x"}}, [][]string{{">=1.3.0-0"}}},
278+
{[][]string{{"<1.2.x"}}, [][]string{{"<1.2.0-0"}}},
279+
{[][]string{{"!=1.2.x"}}, [][]string{{"<1.2.0-0", ">=1.3.0-0"}}},
280+
{[][]string{{">=1.x"}}, [][]string{{">=1.0.0-0"}}},
281+
{[][]string{{"<=1.x"}}, [][]string{{"<2.0.0-0"}}},
282+
{[][]string{{">1.x"}}, [][]string{{">=2.0.0-0"}}},
283+
{[][]string{{"<1.x"}}, [][]string{{"<1.0.0-0"}}},
284+
{[][]string{{"!=1.x"}}, [][]string{{"<1.0.0-0", ">=2.0.0-0"}}},
285+
{[][]string{{"1.2.x"}}, [][]string{{">=1.2.0-0", "<1.3.0-0"}}},
286+
{[][]string{{"1.x"}}, [][]string{{">=1.0.0-0", "<2.0.0-0"}}},
287287
}
288288

289289
for _, tc := range tests {
@@ -461,6 +461,22 @@ func TestParseRange(t *testing.T) {
461461
{"1.2.6", false},
462462
{"1.3.0", true},
463463
}},
464+
{"1.3.x", []tv{
465+
{"1.2.0", false},
466+
{"1.3.0-0", true},
467+
{"1.3.0", true},
468+
{"1.3.9", true},
469+
{"1.4.0-0", false},
470+
{"1.4.0", false},
471+
}},
472+
{"2.x", []tv{
473+
{"1.9.9", false},
474+
{"2.0.0-0", true},
475+
{"2.0.0", true},
476+
{"2.9.9", true},
477+
{"3.0.0-0", false},
478+
{"3.0.0", false},
479+
}},
464480
// Combined Expressions
465481
{">1.2.2 <1.2.4 || >=2.0.0", []tv{
466482
{"1.2.2", false},

0 commit comments

Comments
 (0)