Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix pre-release checks in constraints #35

Merged
merged 1 commit into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package version

import (
"fmt"
"reflect"
"regexp"
"strings"
)
Expand Down Expand Up @@ -113,6 +114,26 @@ func parseSingle(v string) (*Constraint, error) {
}, nil
}

func prereleaseCheck(v, c *Version) bool {
switch vPre, cPre := v.Prerelease() != "", c.Prerelease() != ""; {
case cPre && vPre:
// A constraint with a pre-release can only match a pre-release version
// with the same base segments.
return reflect.DeepEqual(c.Segments64(), v.Segments64())

case !cPre && vPre:
// A constraint without a pre-release can only match a version without a
// pre-release.
return false

case cPre && !vPre:
// OK, except with the pessimistic operator
case !cPre && !vPre:
// OK
}
return true
}

//-------------------------------------------------------------------
// Constraint functions
//-------------------------------------------------------------------
Expand All @@ -126,22 +147,27 @@ func constraintNotEqual(v, c *Version) bool {
}

func constraintGreaterThan(v, c *Version) bool {
return v.Compare(c) == 1
return prereleaseCheck(v, c) && v.Compare(c) == 1
}

func constraintLessThan(v, c *Version) bool {
return v.Compare(c) == -1
return prereleaseCheck(v, c) && v.Compare(c) == -1
}

func constraintGreaterThanEqual(v, c *Version) bool {
return v.Compare(c) >= 0
return prereleaseCheck(v, c) && v.Compare(c) >= 0
}

func constraintLessThanEqual(v, c *Version) bool {
return v.Compare(c) <= 0
return prereleaseCheck(v, c) && v.Compare(c) <= 0
}

func constraintPessimistic(v, c *Version) bool {
// Using a pessimistic constraint with a pre-release, restricts versions to pre-releases
if !prereleaseCheck(v, c) || (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
13 changes: 13 additions & 0 deletions constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ 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.0", "2.1.0-beta", false},
{"~> 2.1.0-a", "2.2.0", false},
{"~> 2.1.0-a", "2.1.0", false},
{"~> 2.1.0-a", "2.1.0-beta", true},
{"~> 2.1.0-a", "2.2.0-alpha", false},
{"> 2.0", "2.1.0-beta", false},
{">= 2.1.0-a", "2.1.0-beta", true},
{">= 2.1.0-a", "2.1.1-beta", false},
{">= 2.0.0", "2.1.0-beta", false},
{">= 2.1.0-a", "2.1.1", true},
{">= 2.1.0-a", "2.1.1-beta", false},
{">= 2.1.0-a", "2.1.0", true},
{"<= 2.1.0-a", "2.0.0", true},
}

for _, tc := range cases {
Expand Down