Skip to content

Commit

Permalink
Resolves mojohaus#794: Do not change versions where oldVersion does n…
Browse files Browse the repository at this point in the history
…ot match the POM version
  • Loading branch information
andrzejj0 committed Oct 26, 2022
1 parent 1826b64 commit e07fa08
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 44 deletions.
22 changes: 8 additions & 14 deletions src/main/java/org/codehaus/mojo/versions/SetMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,9 @@ public void execute() throws MojoExecutionException, MojoFailureException

try
{
final MavenProject project;
if ( processFromLocalAggregationRoot )
{
project = PomHelper.getLocalRoot( projectBuilder, getProject(), localRepository, null, getLog() );
}
else
{
project = getProject();
}
final MavenProject project = processFromLocalAggregationRoot
? PomHelper.getLocalRoot( projectBuilder, getProject(), localRepository, null, getLog() )
: getProject();

getLog().info( "Local aggregation root: " + project.getBasedir() );
Map<String, Model> reactorModels = PomHelper.getReactorModels( project, getLog() );
Expand Down Expand Up @@ -381,11 +375,11 @@ public void execute() throws MojoExecutionException, MojoFailureException
StringUtils.isBlank( oldVersion ) || "*".equals( oldVersion ) ? "" : mVersion );
}
}
if ( !found && RegexUtils.getWildcardScore( groupId ) == 0 && RegexUtils.getWildcardScore( artifactId ) == 0
&& RegexUtils.getWildcardScore( oldVersion ) == 0 )
{
applyChange( project, reactor, files, groupId, artifactId, oldVersion );
}
// if ( !found && RegexUtils.getWildcardScore( groupId ) == 0 && RegexUtils.getWildcardScore( artifactId ) == 0
// && RegexUtils.getWildcardScore( oldVersion ) == 0 )
// {
// applyChange( project, reactor, files, groupId, artifactId, oldVersion );
// }

// now process all the updates
for ( File file : files )
Expand Down
75 changes: 45 additions & 30 deletions src/test/java/org/codehaus/mojo/versions/SetMojoTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.codehaus.mojo.versions;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;

import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.model.Model;
Expand All @@ -14,19 +13,38 @@
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.versions.utils.TestUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

public class SetMojoTest extends AbstractMojoTestCase
{
@Rule
public MojoRule mojoRule = new MojoRule( this );

private Path tempDir;

@Before
public void setUp() throws Exception
{
super.setUp();
tempDir = TestUtils.createTempDir( "set" );
}

@After
public void tearDown() throws IOException
{
TestUtils.tearDownTempDir( tempDir );
}

@Test
public void testGetIncrementedVersion() throws MojoExecutionException
{
Expand Down Expand Up @@ -118,33 +136,30 @@ public void testVersionlessDependency() throws Exception
public void testRemoveSnapshotIdempotency()
throws Exception
{
Path pomDir = Files.createTempDirectory( "set-" );
try
{
Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/set/remove-snapshot/pom.xml" ),
Paths.get( pomDir.toString(), "pom.xml" ), REPLACE_EXISTING );

SetMojo firstRun = (SetMojo) mojoRule.lookupConfiguredMojo( pomDir.toFile(), "set" );
firstRun.execute();
assertThat( String.join( "", Files.readAllLines( Paths.get( pomDir.toString(), "pom.xml" ) ) ),
containsString( "<version>1.0</version>" ) );

// no exception should be thrown, the file should stay with version "1.0"
SetMojo secondRun = (SetMojo) mojoRule.lookupConfiguredMojo( pomDir.toFile(), "set" );
MavenExecutionRequest request =
(MavenExecutionRequest) getVariableValueFromObject( secondRun.settings, "request" );
setVariableValueToObject( request, "interactiveMode", false );
secondRun.execute();
assertThat( String.join( "", Files.readAllLines( Paths.get( pomDir.toString(), "pom.xml" ) ) ),
containsString( "<version>1.0</version>" ) );
}
finally
{
if ( pomDir != null && pomDir.toFile().exists() )
{
Arrays.stream( Objects.requireNonNull( pomDir.toFile().listFiles() ) ).forEach( File::delete );
pomDir.toFile().delete();
}
}
Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/set/remove-snapshot/pom.xml" ),
Paths.get( tempDir.toString(), "pom.xml" ), REPLACE_EXISTING );

SetMojo firstRun = (SetMojo) mojoRule.lookupConfiguredMojo( tempDir.toFile(), "set" );
firstRun.execute();
assertThat( String.join( "", Files.readAllLines( tempDir.resolve( "pom.xml" ) ) ),
containsString( "<version>1.0</version>" ) );

// no exception should be thrown, the file should stay with version "1.0"
SetMojo secondRun = (SetMojo) mojoRule.lookupConfiguredMojo( tempDir.toFile(), "set" );
MavenExecutionRequest request =
(MavenExecutionRequest) getVariableValueFromObject( secondRun.settings, "request" );
setVariableValueToObject( request, "interactiveMode", false );
secondRun.execute();
assertThat( String.join( "", Files.readAllLines( tempDir.resolve( "pom.xml" ) ) ),
containsString( "<version>1.0</version>" ) );
}

@Test
public void testSetOldVersionMismatch() throws Exception
{
TestUtils.copyDir( Paths.get( "src/test/resources/org/codehaus/mojo/set/issue-794" ), tempDir );
mojoRule.lookupConfiguredMojo( tempDir.toFile(), "set" ).execute();
assertThat( String.join( "", Files.readAllLines( tempDir.resolve( "pom.xml" ) ) ),
not( containsString( "<version>bar</version>" ) ) );
}
}
23 changes: 23 additions & 0 deletions src/test/resources/org/codehaus/mojo/set/issue-794/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>default-group</groupId>
<artifactId>default-artifact</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<goals>
<goal>set</goal>
</goals>
<configuration>
<oldVersion>foo</oldVersion>
<newVersion>bar</newVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit e07fa08

Please sign in to comment.