Skip to content

Commit 6ebb3bd

Browse files
committed
feat: added strict version checking capabilities
1 parent f4a4d83 commit 6ebb3bd

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

constraints.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ type Constraints struct {
1717
// NewConstraint returns a Constraints instance that a Version instance can
1818
// be checked against. If there is a parse error it will be returned.
1919
func NewConstraint(c string) (*Constraints, error) {
20-
2120
// Rewrite - ranges into a comparison operation.
2221
c = rewriteRange(c)
2322

@@ -97,7 +96,6 @@ func (cs Constraints) Validate(v *Version) (bool, []error) {
9796
joy = false
9897

9998
} else {
100-
10199
if _, err := c.check(v); err != nil {
102100
e = append(e, err)
103101
joy = false
@@ -151,9 +149,11 @@ func (cs Constraints) MarshalText() ([]byte, error) {
151149
return []byte(cs.String()), nil
152150
}
153151

154-
var constraintOps map[string]cfunc
155-
var constraintRegex *regexp.Regexp
156-
var constraintRangeRegex *regexp.Regexp
152+
var (
153+
constraintOps map[string]cfunc
154+
constraintRegex *regexp.Regexp
155+
constraintRangeRegex *regexp.Regexp
156+
)
157157

158158
// Used to find individual constraints within a multi-constraint string
159159
var findConstraintRegex *regexp.Regexp
@@ -174,14 +174,15 @@ func init() {
174174
"<": constraintLessThan,
175175
">=": constraintGreaterThanEqual,
176176
"=>": constraintGreaterThanEqual,
177+
"==": constraintEqual,
177178
"<=": constraintLessThanEqual,
178179
"=<": constraintLessThanEqual,
179180
"~": constraintTilde,
180181
"~>": constraintTilde,
181182
"^": constraintCaret,
182183
}
183184

184-
ops := `=||!=|>|<|>=|=>|<=|=<|~|~>|\^`
185+
ops := `=||!=|==|>|<|>=|=>|<=|=<|~|~>|\^`
185186

186187
constraintRegex = regexp.MustCompile(fmt.Sprintf(
187188
`^\s*(%s)\s*(%s)\s*$`,
@@ -269,7 +270,6 @@ func parseConstraint(c string) (*constraint, error) {
269270

270271
con, err := NewVersion(ver)
271272
if err != nil {
272-
273273
// The constraintRegex should catch any regex parsing errors. So,
274274
// we should never get here.
275275
return nil, errors.New("constraint Parser Error")
@@ -287,7 +287,6 @@ func parseConstraint(c string) (*constraint, error) {
287287
// is equivalent to * or >=0.0.0
288288
con, err := StrictNewVersion("0.0.0")
289289
if err != nil {
290-
291290
// The constraintRegex should catch any regex parsing errors. So,
292291
// we should never get here.
293292
return nil, errors.New("constraint Parser Error")
@@ -307,7 +306,6 @@ func parseConstraint(c string) (*constraint, error) {
307306
// Constraint functions
308307
func constraintNotEqual(v *Version, c *constraint) (bool, error) {
309308
if c.dirty {
310-
311309
// If there is a pre-release on the version but the constraint isn't looking
312310
// for them assume that pre-releases are not compatible. See issue 21 for
313311
// more details.
@@ -345,8 +343,17 @@ func constraintNotEqual(v *Version, c *constraint) (bool, error) {
345343
return true, nil
346344
}
347345

348-
func constraintGreaterThan(v *Version, c *constraint) (bool, error) {
346+
// Constraint functions
347+
func constraintEqual(v *Version, c *constraint) (bool, error) {
348+
eq := v.Original() == c.orig
349+
if eq {
350+
return true, nil
351+
}
352+
353+
return false, fmt.Errorf("%s is not equal to %s", v, c.orig)
354+
}
349355

356+
func constraintGreaterThan(v *Version, c *constraint) (bool, error) {
350357
// If there is a pre-release on the version but the constraint isn't looking
351358
// for them assume that pre-releases are not compatible. See issue 21 for
352359
// more details.
@@ -407,7 +414,6 @@ func constraintLessThan(v *Version, c *constraint) (bool, error) {
407414
}
408415

409416
func constraintGreaterThanEqual(v *Version, c *constraint) (bool, error) {
410-
411417
// If there is a pre-release on the version but the constraint isn't looking
412418
// for them assume that pre-releases are not compatible. See issue 21 for
413419
// more details.

constraints_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func TestParseConstraint(t *testing.T) {
1717
}{
1818
{">= 1.2", constraintGreaterThanEqual, "1.2.0", false},
1919
{"1.0", constraintTildeOrEqual, "1.0.0", false},
20+
{"==1.0", constraintEqual, "1.0.0", false},
21+
{"==1", constraintEqual, "1.0.0", false},
2022
{"foo", nil, "", true},
2123
{"<= 1.2", constraintLessThanEqual, "1.2.0", false},
2224
{"=< 1.2", constraintLessThanEqual, "1.2.0", false},
@@ -138,6 +140,23 @@ func TestConstraintCheck(t *testing.T) {
138140
{"2", "2.1.1", true},
139141
{"2.1", "2.1.1", true},
140142
{"2.1", "2.2.1", false},
143+
{"==2", "1", false},
144+
{"==2", "3.4.5", false},
145+
{"==2", "2.0.0", false},
146+
{"==2", "2.0.0+alpha", false},
147+
{"==2", "2.0.0-alpha", false},
148+
{"==2", "2.0.1", false},
149+
{"==2.1", "2.1.0", false},
150+
{"==2.1.x", "2.1.0", false},
151+
{"==2.1.x", "2.1.1", false},
152+
{"==2.1", "2.1.1", false},
153+
{"==2.1", "2.2.1", false},
154+
{"==2.1.1", "2.1.1", true},
155+
{"==2.1.1", "2.2.1", false},
156+
{"==2.1.1+alpha", "2.1.1+alpha.1", false},
157+
{"==2.1.1+alpha.1", "2.1.1+alpha.1", true},
158+
{"==2.1.1-alpha", "2.1.1-alpha.1", false},
159+
{"==2.1.1-alpha.1", "2.1.1-alpha.1", true},
141160
{"~1.2.3", "1.2.4", true},
142161
{"~1.2.3", "1.3.4", false},
143162
{"~1.2", "1.2.4", true},
@@ -482,6 +501,16 @@ func TestConstraintsValidate(t *testing.T) {
482501
{"!=4.x", "4.1.0", false},
483502
{"!=4.1.x", "4.2.0", true},
484503
{"!=4.2.x", "4.2.3", false},
504+
{"==4.1", "4.1.0", false},
505+
{"==4.1", "5.1.0", false},
506+
{"==4.x", "5.1.0", false},
507+
{"==4.x", "4.1.0", false},
508+
{"==4.1.x", "4.2.0", false},
509+
{"==4.1.0", "4.1.0", true},
510+
{"==4.1.0+alpha", "4.1.0+alpha", true},
511+
{"==4.1.0+alpha", "4.1.0+alpha-1", false},
512+
{"==4.1.0-alpha", "4.1.0-alpha-1", false},
513+
{"==4.2.x", "4.2.3", false},
485514
{">1.1", "4.1.0", true},
486515
{">1.1", "1.1.0", false},
487516
{"<1.1", "0.1.0", true},
@@ -600,6 +629,15 @@ func TestConstraintsValidate(t *testing.T) {
600629
{"= 2.0", "1.2.3", "1.2.3 is less than 2.0"},
601630
{"!=4.1", "4.1.0", "4.1.0 is equal to 4.1"},
602631
{"!=4.x", "4.1.0", "4.1.0 is equal to 4.x"},
632+
{"==4.x", "5.1.0", "5.1.0 is not equal to 4.x"},
633+
{"==4.x", "4.1.0", "4.1.0 is not equal to 4.x"},
634+
{"==4.1.x", "4.1.0", "4.1.0 is not equal to 4.1.x"},
635+
{"==4.1.x", "4.2.0", "4.2.0 is not equal to 4.1.x"},
636+
{"==4.1.2", "4.1.3", "4.1.3 is not equal to 4.1.2"},
637+
{"==4.1.2-beta", "4.1.2-beta.1", "4.1.2-beta.1 is not equal to 4.1.2-beta"},
638+
{"==4.1.2+beta", "4.1.2+beta.1", "4.1.2+beta.1 is not equal to 4.1.2+beta"},
639+
{"==4.0.1", "4.0.1-beta.1", "4.0.1-beta.1 is a prerelease version and the constraint is only looking for release versions"},
640+
{"==4.2", "4.1.0", "4.1.0 is not equal to 4.2"},
603641
{"!=4.2.x", "4.2.3", "4.2.3 is equal to 4.2.x"},
604642
{">1.1", "1.1.0", "1.1.0 is less than or equal to 1.1"},
605643
{"<1.1", "1.1.0", "1.1.0 is greater than or equal to 1.1"},

0 commit comments

Comments
 (0)