Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MENFORCER-444] Improve error message for failed version rules #218

Merged
merged 1 commit into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public void enforceVersion(Log log, String variableName, String requiredVersionR
vr = VersionRange.createFromVersionSpec(requiredVersionRange);

if (containsVersion(vr, actualVersion)) {
log.debug(msg + " is allowed in the range " + requiredVersionRange + ".");
log.debug(msg + " is allowed in the range " + toString(vr) + ".");
} else {
String message = getMessage();

if (StringUtils.isEmpty(message)) {
message = msg + " is not in the allowed range " + vr + ".";
message = msg + " is not in the allowed range " + toString(vr) + ".";
}

throw new EnforcerRuleException(message);
Expand All @@ -95,6 +95,14 @@ public void enforceVersion(Log log, String variableName, String requiredVersionR
}
}

protected static String toString(VersionRange vr) {
// as recommended version is used as lower bound in this context modify the string representation
if (vr.getRecommendedVersion() != null) {
return "[" + vr.getRecommendedVersion().toString() + ",)";
} else {
return vr.toString();
}
}
/**
* Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
* containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.apache.commons.lang3.SystemUtils;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.logging.Log;
Expand Down Expand Up @@ -76,7 +78,7 @@ public void execute(EnforcerRuleHelper helper) throws EnforcerRuleException {
+ " Build: " + detectedJdkVersion.getBuildNumber() + " Qualifier: "
+ detectedJdkVersion.getQualifier());

setCustomMessageIfNoneConfigured(detectedJdkVersion, getVersion());
setCustomMessageIfNoneConfigured(detectedJdkVersion, getVersion(), log);

enforceVersion(helper.getLog(), "JDK", getVersion(), detectedJdkVersion);
}
Expand Down Expand Up @@ -115,11 +117,20 @@ public static String normalizeJDKVersion(String theJdkVersion) {
return StringUtils.stripEnd(version, ".");
}

private void setCustomMessageIfNoneConfigured(ArtifactVersion detectedJdkVersion, String allowedVersionRange) {
private void setCustomMessageIfNoneConfigured(
ArtifactVersion detectedJdkVersion, String allowedVersionRange, Log log) {
if (getMessage() == null) {
String version;
try {
VersionRange vr = VersionRange.createFromVersionSpec(allowedVersionRange);
version = AbstractVersionEnforcer.toString(vr);
} catch (InvalidVersionSpecificationException e) {
log.debug("Could not parse allowed version range " + allowedVersionRange, e);
version = allowedVersionRange;
}
String message = String.format(
"Detected JDK version %s (JAVA_HOME=%s) is not in the allowed range %s.",
detectedJdkVersion, SystemUtils.JAVA_HOME, allowedVersionRange);
detectedJdkVersion, SystemUtils.JAVA_HOME, version);
super.setMessage(message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void shouldIncludeJavaHomeLocationInTheErrorMessage() {
.isInstanceOf(EnforcerRuleException.class)
.hasMessage(
"Detected JDK version %s (JAVA_HOME=%s) is not in the allowed range %s.",
thisVersion, SystemUtils.JAVA_HOME, requiredVersion);
thisVersion, SystemUtils.JAVA_HOME, "[" + requiredVersion + ",)");
}

@Test
Expand Down