Skip to content

Commit ee95afb

Browse files
committed
Fixing issue where validation of constraint setion gave false positives
Closes #185 Validation of strings was producing false positives when there were too many sections. A string like 1.2.3.1234 would be found in error but on like 1.23.3.1234 or 1.2.34.1234 could pass even though it had too many segments. This is because a string like 1.23.3.1234 would be found as two different constraints of 1.1 and 3.3.1234. The fix was to treat validation for the the first and following constraints separately. For for the following ones a space or command is now required before it to show it's a second or further one. Signed-off-by: Matt Farina <matt@mattfarina.com>
1 parent 1959065 commit ee95afb

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

constraints.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,13 @@ func init() {
180180
ops,
181181
cvRegex))
182182

183+
// The first time a constraint shows up will look slightly different from
184+
// future times it shows up due to a leading space or comma in a given
185+
// string.
183186
validConstraintRegex = regexp.MustCompile(fmt.Sprintf(
184-
`^(\s*(%s)\s*(%s)\s*\,?)+$`,
187+
`^(\s*(%s)\s*(%s)\s*)((?:\s+|,\s*)(%s)\s*(%s)\s*)*$`,
188+
ops,
189+
cvRegex,
185190
ops,
186191
cvRegex))
187192
}

constraints_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ func TestNewConstraint(t *testing.T) {
231231

232232
// The 3 - 4 should be broken into 2 by the range rewriting
233233
{"3 - 4 || => 3.0, < 4", 2, 2, false},
234+
235+
// Due to having 4 parts these should produce an error. See
236+
// https://github.com/Masterminds/semver/issues/185 for the reason for
237+
// these tests.
238+
{"12.3.4.1234", 0, 0, true},
239+
{"12.23.4.1234", 0, 0, true},
240+
{"12.3.34.1234", 0, 0, true},
241+
{"12.3.34 ~1.2.3", 1, 2, false},
242+
{"12.3.34~ 1.2.3", 0, 0, true},
234243
}
235244

236245
for _, tc := range tests {

version_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ func TestNewVersion(t *testing.T) {
8787
{"1.2.2147483648", false},
8888
{"1.2147483648.3", false},
8989
{"2147483648.3.0", false},
90+
91+
// Due to having 4 parts these should produce an error. See
92+
// https://github.com/Masterminds/semver/issues/185 for the reason for
93+
// these tests.
94+
{"12.3.4.1234", true},
95+
{"12.23.4.1234", true},
96+
{"12.3.34.1234", true},
9097
}
9198

9299
for _, tc := range tests {

0 commit comments

Comments
 (0)