Skip to content

Commit

Permalink
Fix issue with - ranges being transformed to something imparsible
Browse files Browse the repository at this point in the history
There were AND cases that included a range which resulted in a
constraing that could not be parsed. It had to do with the way
the constraint was transformed.

Closes #197

Signed-off-by: Matt Farina <[email protected]>
  • Loading branch information
mattfarina committed Apr 4, 2023
1 parent 8e6ddaa commit b1bb761
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func rewriteRange(i string) string {
}
o := i
for _, v := range m {
t := fmt.Sprintf(">= %s, <= %s", v[1], v[11])
t := fmt.Sprintf(">= %s, <= %s ", v[1], v[11])
o = strings.Replace(o, v[0], t, 1)
}

Expand Down
16 changes: 12 additions & 4 deletions constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ func TestNewConstraint(t *testing.T) {
{"12.3.34.1234", 0, 0, true},
{"12.3.34 ~1.2.3", 1, 2, false},
{"12.3.34~ 1.2.3", 0, 0, true},

{"1.0.0 - 2.0.0, <=2.0.0", 1, 3, false},
}

for _, tc := range tests {
Expand Down Expand Up @@ -394,6 +396,10 @@ func TestConstraintsCheck(t *testing.T) {
{"~1.2.3", "1.3.2", false},
{"~1.1", "1.2.3", false},
{"~1.3", "2.4.5", false},

// Ranges should work in conjunction with other constraints anded together.
{"1.0.0 - 2.0.0 <=2.0.0", "1.5.0", true},
{"1.0.0 - 2.0.0, <=2.0.0", "1.5.0", true},
}

for _, tc := range tests {
Expand Down Expand Up @@ -421,16 +427,18 @@ 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 "},
{"2 - 3 4.0.0 - 5.1", ">= 2, <= 3 >= 4.0.0, <= 5.1 "},
{"1.0.0 - 2.0.0 <=2.0.0", ">= 1.0.0, <= 2.0.0 <=2.0.0"},
}

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

if o != tc.nc {
t.Errorf("Range %s rewritten incorrectly as '%s'", tc.c, o)
t.Errorf("Range %s rewritten incorrectly as %q instead of expected %q", tc.c, o, tc.nc)
}
}
}
Expand Down

0 comments on commit b1bb761

Please sign in to comment.