gnodet commented on code in PR #11770:
URL: https://github.com/apache/maven/pull/11770#discussion_r3284336606
##########
maven-core/src/main/java/org/apache/maven/toolchain/RequirementMatcherFactory.java:
##########
@@ -79,6 +81,29 @@ public boolean matches(String requirement) {
}
}
+ private VersionRange convertRequirementToVersionRange(String
requirement)
+ throws InvalidVersionSpecificationException {
+ // Specific for Version _requirement_ matching;
+ // If the version is a simple integer (like "25")
+ // then treat this as the requirement "the major version is 25"
+ if (Pattern.matches("^[0-9]+$", requirement)) {
+ int majorVersion = Integer.parseInt(requirement);
+ return VersionRange.createFromVersionSpec("[" + majorVersion +
"," + (majorVersion + 1) + ")");
+ }
+
+ // If the version is a major.minor (like "1.5")
+ // then treat this as the requirement "the major version is 1 and
the minor is 5"
+ if (Pattern.matches("^[0-9]\\.[0-9]+$", requirement)) {
Review Comment:
Bug: the major version part is `[0-9]` (single digit only), so a requirement
like `"21.3"` would silently fall through to the default path and be treated as
an exact match. It should be `[0-9]+` to mirror the first pattern.
```suggestion
if (Pattern.matches("^[0-9]+\\.[0-9]+$", requirement)) {
```
Also, as @slawekjaranowski mentioned, both patterns should be pre-compiled
into `private static final Pattern` fields to avoid recompilation on every call.
##########
maven-core/src/test/java/org/apache/maven/toolchain/RequirementMatcherFactoryTest.java:
##########
@@ -50,11 +50,18 @@ public void testCreateExactMatcher() {
public void testCreateVersionMatcher() {
RequirementMatcher matcher;
matcher = RequirementMatcherFactory.createVersionMatcher("1.5.2");
- assertFalse(matcher.matches("1.5"));
- assertTrue(matcher.matches("1.5.2"));
+ assertTrue(matcher.matches("1")); // Major matches
+ assertTrue(matcher.matches("1.5")); // Major.Minor matches
+ assertTrue(matcher.matches("1.5.2")); // Full match
+ assertFalse(matcher.matches("1.6")); // Wrong minor
+ assertFalse(matcher.matches("2")); // Wrong major
Review Comment:
Consider adding tests for multi-digit major versions to catch the regex bug
above, e.g.:
```java
matcher = RequirementMatcherFactory.createVersionMatcher("21.0.2");
assertTrue(matcher.matches("21")); // major-only
assertTrue(matcher.matches("21.0")); // major.minor
assertFalse(matcher.matches("21.1")); // wrong minor
assertFalse(matcher.matches("2")); // wrong major
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]