Skip to content

Commit

Permalink
[MINSTALL-186] Use proper repositorySystemSession
Browse files Browse the repository at this point in the history
When looking for existing files in the local repository, use the proper
`repositorySystemSession`.

Current behavior ignore the `localRepositoryPath` parameter when
checking for file existence and lead to false positive.
  • Loading branch information
rbioteau committed Nov 25, 2022
1 parent bb53609 commit 6b45e0b
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target/
!src/test/resources/unit/*/target
.project
.classpath
.settings/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
*/
@Mojo( name = "install-file", requiresProject = false, aggregator = true, threadSafe = true )
public class InstallFileMojo
extends AbstractMojo
extends AbstractMojo
{
private static final String LS = System.getProperty( "line.separator" );

Expand Down Expand Up @@ -248,8 +248,8 @@ public void execute()
).setFile( file );
installRequest.addArtifact( mainArtifact );

File artifactLocalFile = getLocalRepositoryFile( session.getRepositorySession(), mainArtifact );
File pomLocalFile = getPomLocalRepositoryFile( session.getRepositorySession(), mainArtifact );
File artifactLocalFile = getLocalRepositoryFile( repositorySystemSession, mainArtifact );
File pomLocalFile = getPomLocalRepositoryFile( repositorySystemSession, mainArtifact );

if ( file.equals( artifactLocalFile ) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,79 @@ public class InstallFileMojoTest

private final String LOCAL_REPO = "target/local-repo/";

private final String SPECIFIC_LOCAL_REPO = "target/specific-local-repo/";

public void setUp()
throws Exception
{
super.setUp();

FileUtils.deleteDirectory( new File( getBasedir() + "/" + LOCAL_REPO ) );
FileUtils.deleteDirectory( new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO ) );
}

public void testInstallFileFromLocalRepositoryToLocalRepositoryPath()
throws Exception
{
File localRepository =
new File( getBasedir(), "target/test-classes/unit/install-file-from-local-repository-test/target" );

File testPom = new File( localRepository.getParentFile(), "plugin-config.xml" );

InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );

assertNotNull( mojo );

setVariableValueToObject( mojo, "session", createMavenSession( localRepository.getAbsolutePath() ) );

File specificLocalRepositoryPath = new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO );

setVariableValueToObject( mojo, "localRepositoryPath", specificLocalRepositoryPath );

assignValuesForParameter( mojo );

mojo.execute();

String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/"
+ artifactId + "-" + version;

File installedArtifact = new File( localPath + "." + "jar" );

assertTrue( installedArtifact.exists() );

assertEquals( FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).toString(), 5,
FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).size() );
}

public void testInstallFileWithLocalRepositoryPath()
throws Exception
{
File testPom =
new File( getBasedir(), "target/test-classes/unit/install-file-with-checksum/" + "plugin-config.xml" );

InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );

assertNotNull( mojo );

setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );

File specificLocalRepositoryPath = new File( getBasedir() + "/" + SPECIFIC_LOCAL_REPO );

setVariableValueToObject( mojo, "localRepositoryPath", specificLocalRepositoryPath );

assignValuesForParameter( mojo );

mojo.execute();

String localPath = getBasedir() + "/" + SPECIFIC_LOCAL_REPO + groupId + "/" + artifactId + "/" + version + "/"
+ artifactId + "-" + version;

File installedArtifact = new File( localPath + "." + "jar" );

assertTrue( installedArtifact.exists() );

assertEquals( FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).toString(), 5,
FileUtils.getFiles( new File( SPECIFIC_LOCAL_REPO ), null, null ).size() );
}

public void testInstallFileTestEnvironment()
Expand All @@ -73,7 +140,7 @@ public void testInstallFileTestEnvironment()

InstallFileMojo mojo = (InstallFileMojo) lookupMojo( "install-file", testPom );

setVariableValueToObject( mojo, "session", createMavenSession() );
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );

assertNotNull( mojo );
}
Expand All @@ -87,7 +154,7 @@ public void testBasicInstallFile()

assertNotNull( mojo );

setVariableValueToObject( mojo, "session", createMavenSession() );
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );

assignValuesForParameter( mojo );

Expand All @@ -111,7 +178,7 @@ public void testInstallFileWithClassifier()

assertNotNull( mojo );

setVariableValueToObject( mojo, "session", createMavenSession() );
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );

assignValuesForParameter( mojo );

Expand All @@ -137,7 +204,7 @@ public void testInstallFileWithGeneratePom()

assertNotNull( mojo );

setVariableValueToObject( mojo, "session", createMavenSession() );
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );

assignValuesForParameter( mojo );

Expand Down Expand Up @@ -178,7 +245,7 @@ public void testInstallFileWithPomFile()

assertNotNull( mojo );

setVariableValueToObject( mojo, "session", createMavenSession() );
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );

assignValuesForParameter( mojo );

Expand Down Expand Up @@ -211,7 +278,7 @@ public void testInstallFileWithPomAsPackaging()

assertNotNull( mojo );

setVariableValueToObject( mojo, "session", createMavenSession() );
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );

assignValuesForParameter( mojo );

Expand Down Expand Up @@ -239,7 +306,7 @@ public void testInstallFile()

assertNotNull( mojo );

setVariableValueToObject( mojo, "session", createMavenSession() );
setVariableValueToObject( mojo, "session", createMavenSession( LOCAL_REPO ) );

assignValuesForParameter( mojo );

Expand Down Expand Up @@ -276,13 +343,14 @@ private String dotToSlashReplacer( String parameter )
return parameter.replace( '.', '/' );
}

private MavenSession createMavenSession() throws NoLocalRepositoryManagerException
private MavenSession createMavenSession( String localRepositoryBaseDir )
throws NoLocalRepositoryManagerException
{
MavenSession session = mock( MavenSession.class );
DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
repositorySession.setLocalRepositoryManager(
new EnhancedLocalRepositoryManagerFactory().newInstance(
repositorySession, new LocalRepository( LOCAL_REPO )
repositorySession, new LocalRepository( localRepositoryBaseDir )
)
);
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project>
<build>
<plugins>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<configuration>
<groupId>org.apache.maven.test</groupId>
<artifactId>maven-install-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<file>${basedir}/target/test-classes/unit/install-file-from-local-repository-test/target/org/apache/maven/test/maven-install-test/1.0-SNAPSHOT/maven-install-test-1.0-SNAPSHOT.jar
</file>
</configuration>
</plugin>
</plugins>
</build>
</project>
Binary file not shown.

0 comments on commit 6b45e0b

Please # to comment.