From 070a3f8ff4d94d2d03ec1e61916e2bfb3a07da8f Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Wed, 1 May 2024 17:24:58 +0200 Subject: [PATCH] fix: tilde constraint --- version/constraint.go | 8 -------- version/constraint_test.go | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/version/constraint.go b/version/constraint.go index 3422e12..09c7636 100644 --- a/version/constraint.go +++ b/version/constraint.go @@ -274,14 +274,6 @@ func constraintTilde(v, c *Version) bool { return false } - if v.segments[1] != c.segments[1] && c.si > 1 { - return false - } - - if reflect.DeepEqual(v.segments, c.segments) && !prereleaseCheck(v, c) { - return false - } - return true } diff --git a/version/constraint_test.go b/version/constraint_test.go index 6704cf9..26b9c6b 100644 --- a/version/constraint_test.go +++ b/version/constraint_test.go @@ -48,3 +48,24 @@ func TestConstraintVersionNumber(t *testing.T) { assert.True(t, c.Check(Must(NewVersion("1.0.0")))) assert.False(t, c.Check(Must(NewVersion("1.0.1")))) } + +func TestTildeConstraintPositive(t *testing.T) { + assert.True(t, MustConstraints(NewConstraint("~2.4")).Check(Must(NewVersion("2.9.0")))) + assert.True(t, MustConstraints(NewConstraint("~2.4")).Check(Must(NewVersion("2.4.5")))) + + assert.True(t, MustConstraints(NewConstraint("~1")).Check(Must(NewVersion("1.2.3")))) + assert.True(t, MustConstraints(NewConstraint("~1.0")).Check(Must(NewVersion("1.4.7")))) + + assert.True(t, MustConstraints(NewConstraint("~1.2.1 >=1.2.3")).Check(Must(NewVersion("1.2.3")))) + assert.True(t, MustConstraints(NewConstraint("~1.2.1 =1.2.3")).Check(Must(NewVersion("1.2.3")))) + assert.True(t, MustConstraints(NewConstraint("~1.2.1 1.2.3")).Check(Must(NewVersion("1.2.3")))) + assert.True(t, MustConstraints(NewConstraint("~1.2.1 >=1.2.3 1.2.3")).Check(Must(NewVersion("1.2.3")))) + assert.True(t, MustConstraints(NewConstraint("~1.2.1 1.2.3 >=1.2.3")).Check(Must(NewVersion("1.2.3")))) + assert.True(t, MustConstraints(NewConstraint("~1.2.1 1.2.3")).Check(Must(NewVersion("1.2.3")))) +} + +func TestTildeConstraintNegative(t *testing.T) { + assert.False(t, MustConstraints(NewConstraint("~2.4")).Check(Must(NewVersion("3.0.0")))) + assert.False(t, MustConstraints(NewConstraint("~2.4")).Check(Must(NewVersion("2.3.9")))) + assert.False(t, MustConstraints(NewConstraint("~1")).Check(Must(NewVersion("0.2.3")))) +}