Skip to content

Commit

Permalink
Require a space around the range separator
Browse files Browse the repository at this point in the history
2-3 could be interpreted as 2.0.0 - 3.0.0 OR
2.0.0-3 where 3 is the pre-release segment. In order to disambiguate
between the two, require that ranges have a space around the range
separator.

2-3 is interpreted as 2.0.0-3
2 - 3 is interpreted as 2.0.0 - 3.0.0
  • Loading branch information
carolynvs committed Apr 2, 2018
1 parent c2e7f6c commit adf96c4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func init() {
cvRegex))

constraintRangeRegex = regexp.MustCompile(fmt.Sprintf(
`\s*(%s)\s*-\s*(%s)\s*`,
`\s*(%s)\s* - \s*(%s)\s*`,
cvRegex, cvRegex))
}

Expand Down
31 changes: 26 additions & 5 deletions constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestNewConstraint(t *testing.T) {
includeMin: true,
},
}, false},
{"3-4 || => 1.0, < 2", Union(
{"3 - 4 || => 1.0, < 2", Union(
rangeConstraint{
min: newV(3, 0, 0),
max: newV(4, 0, 0),
Expand All @@ -265,7 +265,7 @@ func TestNewConstraint(t *testing.T) {
},
), false},
// demonstrates union compression
{"3-4 || => 3.0, < 4", rangeConstraint{
{"3 - 4 || => 3.0, < 4", rangeConstraint{
min: newV(3, 0, 0),
max: newV(4, 0, 0),
includeMin: true,
Expand Down Expand Up @@ -545,9 +545,30 @@ func TestRewriteRange(t *testing.T) {
c string
nc string
}{
{"2-3", ">= 2, <= 3"},
{"2-3, 2-3", ">= 2, <= 3,>= 2, <= 3"},
{"2-3, 4.0.0-5.1", ">= 2, <= 3,>= 4.0.0, <= 5.1"},
{"2 - 3", ">= 2, <= 3"},
{"2 - 3, 2 - 3", ">= 2, <= 3,>= 2, <= 3"},
{"2 - 3, 4.0.0 - 5.1", ">= 2, <= 3,>= 4.0.0, <= 5.1"},
}

for _, tc := range tests {
o := rewriteRange(tc.c)

if o != tc.nc {
t.Errorf("Range %s rewritten incorrectly as '%s'", tc.c, o)
}
}
}

func TestRewriteRange_InvalidRange(t *testing.T) {
// Ranges require a space to be recognized

tests := []struct {
c string
nc string
}{
{"2-3", "2-3"},
{"2-3, 2 - 3","2-3,>= 2, <= 3"},
{"2-3, 4.0.0 - 5.1", "2-3,>= 4.0.0, <= 5.1"},
}

for _, tc := range tests {
Expand Down

0 comments on commit adf96c4

Please sign in to comment.