diff options
author | Julien Eluard <[email protected]> | 2014-10-01 22:08:28 -0300 |
---|---|---|
committer | Julien Eluard <[email protected]> | 2014-10-01 22:08:28 -0300 |
commit | bca36dc2202f806ecc022b8b34f110eba830fe9e (patch) | |
tree | 8254007571478526443aacef36adf8b77d8bbf53 | |
parent | 4a2555c269d1dbc0a8f4253ded9b5719dc9146dd (diff) | |
parent | 95232052f900ac565917246b29b56b058cc060da (diff) |
Merge pull request #45 from shuraa/master
Fix regression, skip incompatible version during previous version detection.
-rwxr-xr-x | .gitignore | 2 | ||||
-rwxr-xr-x | api/src/main/java/org/semver/Version.java | 10 | ||||
-rwxr-xr-x | api/src/test/java/org/semver/VersionTest.java | 19 | ||||
-rwxr-xr-x | enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java | 81 |
4 files changed, 74 insertions, 38 deletions
@@ -2,3 +2,5 @@ target/ .classpath .project .settings +.idea/ +*.iml
\ No newline at end of file diff --git a/api/src/main/java/org/semver/Version.java b/api/src/main/java/org/semver/Version.java index 4f73e4f..83765da 100755 --- a/api/src/main/java/org/semver/Version.java +++ b/api/src/main/java/org/semver/Version.java @@ -46,7 +46,7 @@ public final class Version implements Comparable<Version> { private static final Pattern DIGITS_ONLY = Pattern.compile("\\d+"); - private static final String SNAPSHOT_VERSION_SUFFIX = "-SNAPSHOT"; + private static final String SNAPSHOT_VERSION_SUFFIX = "SNAPSHOT"; private final int major; private final int minor; @@ -168,6 +168,14 @@ public final class Version implements Comparable<Version> { return this.special != null && this.special.isSnapshot(); } + /** + * @param version version to check with + * @return {@code true}, if supplied version is compatible with this version, {@code false} - otherwise + */ + public boolean isCompatible(Version version) { + return version != null && this.major == version.major; + } + @Override public int hashCode() { int hash = 5; diff --git a/api/src/test/java/org/semver/VersionTest.java b/api/src/test/java/org/semver/VersionTest.java index bdefa5a..625c3ac 100755 --- a/api/src/test/java/org/semver/VersionTest.java +++ b/api/src/test/java/org/semver/VersionTest.java @@ -97,6 +97,25 @@ public class VersionTest { } @Test + public void shouldBeSnapshotVersion() { + Assert.assertTrue(Version.parse("1.5.30-SNAPSHOT").isSnapshot()); + } + + @Test + public void shouldBeCompatible() { + Assert.assertTrue(Version.parse("1.0.0").isCompatible(Version.parse("1.2.3-SNAPSHOT"))); + Assert.assertTrue(Version.parse("1.0.0").isCompatible(Version.parse("1.0.1"))); + Assert.assertTrue(Version.parse("1.0.0").isCompatible(Version.parse("1.1.0"))); + } + + @Test + public void shouldBeIncompatible() { + Assert.assertFalse(Version.parse("0.0.1-SNAPSHOT").isCompatible(null)); + Assert.assertFalse(Version.parse("1.0.1").isCompatible(Version.parse("2.0.0"))); + Assert.assertFalse(Version.parse("1.1.0-rc3").isCompatible(Version.parse("3.1.0-SNAPSHOT"))); + } + + @Test public void isNewer() { Assert.assertTrue(Version.parse("3.2.3").compareTo(Version.parse("3.2-M1-SNAPSHOT")) > 0); Assert.assertTrue(Version.parse("1.0.0").compareTo(Version.parse("0.0.0")) > 0); diff --git a/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java b/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java index 38c2517..f0ce4cf 100755 --- a/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java +++ b/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java @@ -99,53 +99,39 @@ public abstract class AbstractEnforcerRule implements EnforcerRule { @Override public void execute(final EnforcerRuleHelper helper) throws EnforcerRuleException { - final MavenProject project; - try { - project = (MavenProject) helper.evaluate("${project}"); - } catch (ExpressionEvaluationException e) { - throw new EnforcerRuleException("Failed to access ${project} variable", e); - } - final String type = project.getArtifact().getType(); - if (!AbstractEnforcerRule.JAR_ARTIFACT_TYPE.equals(type) && - !AbstractEnforcerRule.BUNDLE_ARTIFACT_TYPE.equals(type)) { + final MavenProject project = getMavenProject(helper); + if (shouldSkipRuleExecution(project)) { helper.getLog().debug("Skipping non "+AbstractEnforcerRule.JAR_ARTIFACT_TYPE+ " or " + BUNDLE_ARTIFACT_TYPE + " artifact."); return; } final Artifact previousArtifact; - final Artifact currentArtifact = project.getArtifact(); - validateArtifact(currentArtifact); + final Artifact currentArtifact = validateArtifact(project.getArtifact()); final Version current = Version.parse(currentArtifact.getVersion()); - final File currentJar = currentArtifact.getFile(); try { final ArtifactRepository localRepository = (ArtifactRepository) helper.evaluate("${localRepository}"); final String version; + if (this.previousVersion != null) { version = this.previousVersion; - helper.getLog().info("Version specified as <"+version+">"); } else { final ArtifactMetadataSource artifactMetadataSource = (ArtifactMetadataSource) helper.getComponent(ArtifactMetadataSource.class); final List<ArtifactVersion> availableVersions = getAvailableReleasedVersions(artifactMetadataSource, project, localRepository); final List<ArtifactVersion> availablePreviousVersions = filterNonPreviousVersions(availableVersions, current); - if (availablePreviousVersions.isEmpty()) { helper.getLog().warn("No previously released version. Backward compatibility check not performed."); - return; } - version = availablePreviousVersions.iterator().next().toString(); - helper.getLog().info("Version deduced as <"+version+"> (among all availables: "+availablePreviousVersions+")"); } final ArtifactFactory artifactFactory = (ArtifactFactory) helper.getComponent(ArtifactFactory.class); - previousArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), version, null, type); + previousArtifact = artifactFactory.createArtifact(project.getGroupId(), project.getArtifactId(), version, null, project.getArtifact().getType()); final ArtifactResolver resolver = (ArtifactResolver) helper.getComponent(ArtifactResolver.class ); resolver.resolve(previousArtifact, project.getRemoteArtifactRepositories(), localRepository); - validateArtifact(previousArtifact); } catch (Exception e) { helper.getLog().warn("Exception while accessing artifacts; skipping check.", e); @@ -154,36 +140,25 @@ public abstract class AbstractEnforcerRule implements EnforcerRule { final Version previous = Version.parse(previousArtifact.getVersion()); final File previousJar = previousArtifact.getFile(); - - helper.getLog().info("Using <"+previousJar+"> as previous JAR"); - helper.getLog().info("Using <"+currentJar+"> as current JAR"); - - try { - final DiffCriteria diffCriteria = publicOnly ? new PublicDiffCriteria() : new SimpleDiffCriteria(); - final Comparer comparer = new Comparer(diffCriteria, previousJar, currentJar, extractFilters(this.includes), extractFilters(this.excludes)); - final Delta delta = comparer.diff(); - - enforce(helper, delta, previous, current); - } catch (IOException e) { - throw new EnforcerRuleException("Exception while checking compatibility: "+e.toString(), e); - } + final File currentJar = currentArtifact.getFile(); + compareJars(helper, previous, previousJar, current, currentJar); } protected abstract void enforce(final EnforcerRuleHelper helper, final Delta delta, final Version previous, final Version current) throws EnforcerRuleException; - + protected final void fail(final Delta delta, final String message) throws EnforcerRuleException { if (this.dumpDetails) { Dumper.dump(delta); } throw new EnforcerRuleException(message); } - + /** * @param artifactMetadataSource * @param project * @param localRepository * @return all available versions from most recent to oldest - * @throws ArtifactMetadataRetrievalException + * @throws ArtifactMetadataRetrievalException */ protected final List<ArtifactVersion> getAvailableReleasedVersions(final ArtifactMetadataSource artifactMetadataSource, final MavenProject project, final ArtifactRepository localRepository) throws ArtifactMetadataRetrievalException { final List<ArtifactVersion> availableVersions = artifactMetadataSource.retrieveAvailableVersions(project.getArtifact(), localRepository, project.getRemoteArtifactRepositories()); @@ -203,21 +178,53 @@ public abstract class AbstractEnforcerRule implements EnforcerRule { protected final List<ArtifactVersion> filterNonPreviousVersions(final List<ArtifactVersion> availableVersions, final Version version) { final List<ArtifactVersion> versions = new ArrayList<ArtifactVersion>(); for (final ArtifactVersion artifactVersion : availableVersions) { - if (version.compareTo(Version.parse(artifactVersion.toString())) > 0) { + Version parsedVersion = Version.parse(artifactVersion.toString()); + if (version.isCompatible(parsedVersion) && version.compareTo(parsedVersion) > 0) { versions.add(artifactVersion); } } return versions; } + private static MavenProject getMavenProject(EnforcerRuleHelper helper) throws EnforcerRuleException { + final MavenProject project; + try { + project = (MavenProject) helper.evaluate("${project}"); + } catch (ExpressionEvaluationException e) { + throw new EnforcerRuleException("Failed to access ${project} variable", e); + } + return project; + } + + private static boolean shouldSkipRuleExecution(MavenProject project) { + return !AbstractEnforcerRule.JAR_ARTIFACT_TYPE.equals(project.getArtifact().getType()) && + !AbstractEnforcerRule.BUNDLE_ARTIFACT_TYPE.equals(project.getArtifact().getType()); + } + + private void compareJars(final EnforcerRuleHelper helper, final Version previous, final File previousJar, final Version current, + final File currentJar) throws EnforcerRuleException { + helper.getLog().info("Using <" + previousJar + "> as previous JAR"); + helper.getLog().info("Using <" + currentJar + "> as current JAR"); + try { + final DiffCriteria diffCriteria = publicOnly ? new PublicDiffCriteria() : new SimpleDiffCriteria(); + final Comparer comparer = + new Comparer(diffCriteria, previousJar, currentJar, extractFilters(this.includes), extractFilters(this.excludes)); + final Delta delta = comparer.diff(); + enforce(helper, delta, previous, current); + } catch (IOException e) { + throw new EnforcerRuleException("Exception while checking compatibility: " + e.toString(), e); + } + } + /** * Validates that specified {@link Artifact} is a file. * @param artifact */ - private void validateArtifact(final Artifact artifact) { + private static Artifact validateArtifact(final Artifact artifact) { if (!artifact.getFile().isFile()) { throw new IllegalArgumentException("<"+artifact.getFile()+"> is not a file"); } + return artifact; } @Override |