Skip to content

Commit

Permalink
fix pre-release checks in constraints
Browse files Browse the repository at this point in the history
Pre-releases should not be automatically accepted as candidate versions
for constraints without a pre-release component. This follows the logic
documented for RubyGems which appears to be the inspiration for the
constraint operators, and matches the behavior of other version
packages.

Adding a pre-release component to a pessimistic constraint will limit
accepted versions to pre-releases only. Adding a pre-release to other
constraint types will accept any comparable version, including
pre-releases.
  • Loading branch information
jbardin committed Mar 14, 2018
1 parent 4fe82ae commit 9dd34d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func (cs Constraints) String() string {

// Check tests if a constraint is validated by the given version.
func (c *Constraint) Check(v *Version) bool {
// A constraint without a pre-release can only match a version without a
// pre-release.
if c.check.Prerelease() == "" && v.Prerelease() != "" {
return false
}

return c.f(v, c.check)
}

Expand Down Expand Up @@ -142,6 +148,12 @@ func constraintLessThanEqual(v, c *Version) bool {
}

func constraintPessimistic(v, c *Version) bool {
// Using a pessimistic constraint with a pre-release, restricts versions to
// pre-releases.
if c.Prerelease() != "" && v.Prerelease() == "" {
return false
}

// If the version being checked is naturally less than the constraint, then there
// is no way for the version to be valid against the constraint
if v.LessThan(c) {
Expand Down
8 changes: 8 additions & 0 deletions constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func TestConstraintCheck(t *testing.T) {
{"~> 1.0.9.5", "1.0.9.6", true},
{"~> 1.0.9.5", "1.0.9.5.0", true},
{"~> 1.0.9.5", "1.0.9.5.1", true},
{">=2.1.0-a", "2.1.1-beta", true},
{"~> 2.0", "2.1.0-beta", false},
{">= 1.0.0", "2.1.0-beta", false},
{"> 2.0", "2.1.0-beta", false},
{">= 2.1.0-a", "2.1.1", true},
{"~> 2.1.0-a", "2.2.0", false},
{">= 2.1.0-a", "2.1.0", true},
{"<= 2.1.0-a", "2.0.0", true},
}

for _, tc := range cases {
Expand Down

0 comments on commit 9dd34d5

Please sign in to comment.