Skip to content

Commit

Permalink
Fixed #24: Fixed edge case issue where constraint "> 0" does not hand…
Browse files Browse the repository at this point in the history
…le "0.0.1-alpha" properly.
  • Loading branch information
mattfarina committed Nov 29, 2016
1 parent 05d8cdb commit f0750ad
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Release 1.2.1 (unreleased)

## Fixed:
- #24: Fixed edge case issue where constraint "> 0" does not handle "0.0.1-alpha"
properly.

# Release 1.2.0 (2016-11-04)

## Added
Expand Down
87 changes: 79 additions & 8 deletions constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,6 @@ type constraint struct {

// Check if a version meets the constraint
func (c *constraint) check(v *Version) bool {

// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
if v.Prerelease() != "" && c.con.Prerelease() == "" {
return false
}

return c.function(v, c)
}

Expand Down Expand Up @@ -212,6 +204,14 @@ func parseConstraint(c string) (*constraint, error) {
// Constraint functions
func constraintNotEqual(v *Version, c *constraint) bool {
if c.dirty {

// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
if v.Prerelease() != "" && c.con.Prerelease() == "" {
return false
}

if c.con.Major() != v.Major() {
return true
}
Expand All @@ -228,10 +228,31 @@ func constraintNotEqual(v *Version, c *constraint) bool {
}

func constraintGreaterThan(v *Version, c *constraint) bool {

// An edge case the constraint is 0.0.0 and the version is 0.0.0-someprerelease
// exists. This that case.
if !isNonZero(c.con) && isNonZero(v) {
return true
}

// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
if v.Prerelease() != "" && c.con.Prerelease() == "" {
return false
}

return v.Compare(c.con) == 1
}

func constraintLessThan(v *Version, c *constraint) bool {
// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
if v.Prerelease() != "" && c.con.Prerelease() == "" {
return false
}

if !c.dirty {
return v.Compare(c.con) < 0
}
Expand All @@ -246,10 +267,30 @@ func constraintLessThan(v *Version, c *constraint) bool {
}

func constraintGreaterThanEqual(v *Version, c *constraint) bool {
// An edge case the constraint is 0.0.0 and the version is 0.0.0-someprerelease
// exists. This that case.
if !isNonZero(c.con) && isNonZero(v) {
return true
}

// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
if v.Prerelease() != "" && c.con.Prerelease() == "" {
return false
}

return v.Compare(c.con) >= 0
}

func constraintLessThanEqual(v *Version, c *constraint) bool {
// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
if v.Prerelease() != "" && c.con.Prerelease() == "" {
return false
}

if !c.dirty {
return v.Compare(c.con) <= 0
}
Expand All @@ -270,6 +311,13 @@ func constraintLessThanEqual(v *Version, c *constraint) bool {
// ~1.2.3, ~>1.2.3 --> >=1.2.3, <1.3.0
// ~1.2.0, ~>1.2.0 --> >=1.2.0, <1.3.0
func constraintTilde(v *Version, c *constraint) bool {
// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
if v.Prerelease() != "" && c.con.Prerelease() == "" {
return false
}

if v.LessThan(c.con) {
return false
}
Expand All @@ -294,6 +342,13 @@ func constraintTilde(v *Version, c *constraint) bool {
// When there is a .x (dirty) status it automatically opts in to ~. Otherwise
// it's a straight =
func constraintTildeOrEqual(v *Version, c *constraint) bool {
// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
if v.Prerelease() != "" && c.con.Prerelease() == "" {
return false
}

if c.dirty {
c.msg = constraintMsg["~"]
return constraintTilde(v, c)
Expand All @@ -309,6 +364,13 @@ func constraintTildeOrEqual(v *Version, c *constraint) bool {
// ^1.2.3 --> >=1.2.3, <2.0.0
// ^1.2.0 --> >=1.2.0, <2.0.0
func constraintCaret(v *Version, c *constraint) bool {
// If there is a pre-release on the version but the constraint isn't looking
// for them assume that pre-releases are not compatible. See issue 21 for
// more details.
if v.Prerelease() != "" && c.con.Prerelease() == "" {
return false
}

if v.LessThan(c.con) {
return false
}
Expand Down Expand Up @@ -348,3 +410,12 @@ func rewriteRange(i string) string {

return o
}

// Detect if a version is not zero (0.0.0)
func isNonZero(v *Version) bool {
if v.Major() != 0 || v.Minor() != 0 || v.Patch() != 0 || v.Prerelease() != "" {
return true
}

return false
}
6 changes: 5 additions & 1 deletion constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ func TestConstraintCheck(t *testing.T) {
{"<=1.1", "0.1.0", true},
{"<=1.1", "1.1.0", true},
{"<=1.1", "1.1.1", false},
{">0", "0.0.1-alpha", true},
{">=0", "0.0.1-alpha", true},
{">0", "0", false},
{">=0", "0", true},
}

for _, tc := range tests {
Expand All @@ -89,7 +93,7 @@ func TestConstraintCheck(t *testing.T) {

a := c.check(v)
if a != tc.check {
t.Errorf("Constraint '%s' failing", tc.constraint)
t.Errorf("Constraint %q failing with %q", tc.constraint, tc.version)
}
}
}
Expand Down

0 comments on commit f0750ad

Please sign in to comment.