Skip to content

Commit

Permalink
[MSHARED-1128] Introduce execute method and deprecate executeGoal(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Sep 12, 2022
1 parent bbc5aee commit 6b9dbb5
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 28 deletions.
114 changes: 87 additions & 27 deletions src/main/java/org/apache/maven/shared/verifier/Verifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -66,6 +65,13 @@ public class Verifier

private static final String[] DEFAULT_CLI_ARGUMENTS = {"-e", "--batch-mode"};

/**
* Command used to clean project before execution.
* <p
* NOTE: Neither test lifecycle binding nor prefix resolution here but call the goal directly.
*/
private static final String CLEAN_CLI_ARGUMENT = "org.apache.maven.plugins:maven-clean-plugin:clean";

private String localRepo;

private final String basedir;
Expand Down Expand Up @@ -1175,18 +1181,54 @@ else if ( found && !wanted )
//
// ----------------------------------------------------------------------

/**
* Execute Maven.
* <p>
* For replacement please use:
* <pre>
* verifier.addCliArgument( "goal" );
* verifier.execute();
* </pre>
*
* @deprecated will be removed without replacement.
*/
@Deprecated
public void executeGoal( String goal )
throws VerificationException
{
executeGoal( goal, environmentVariables );
}

/**
* Execute Maven.
* <p>
* For replacement please use:
* <pre>
* verifier.addCliArgument( "goal" );
* verifier.setEnvironmentVariable( "key1", "value1" );
* verifier.setEnvironmentVariable( "key2", "value2" );
* verifier.execute();
* </pre>
*
* @deprecated will be removed without replacement.
*/
public void executeGoal( String goal, Map<String, String> envVars )
throws VerificationException
{
executeGoals( Arrays.asList( goal ), envVars );
executeGoals( Collections.singletonList( goal ), envVars );
}

/**
* Execute Maven.
* <p>
* For replacement please use:
* <pre>
* verifier.addCliArguments( "goal1", "goal2" );
* verifier.execute();
* </pre>
*
* @deprecated will be removed without replacement.
*/
public void executeGoals( List<String> goals )
throws VerificationException
{
Expand Down Expand Up @@ -1217,31 +1259,34 @@ public String getExecutable()
}
}

/**
* Execute Maven.
* <p>
* For replacement please use:
* <pre>
* verifier.addCliArguments( "goal1", "goal2" );
* verifier.setEnvironmentVariable( "key1", "value1" );
* verifier.setEnvironmentVariable( "key2", "value2" );
* verifier.execute();
* </pre>
*
* @deprecated will be removed without replacement.
*/
public void executeGoals( List<String> goals, Map<String, String> envVars )
throws VerificationException
{
List<String> allGoals = new ArrayList<String>();

if ( autoclean )
{
/*
* NOTE: Neither test lifecycle binding nor prefix resolution here but call the goal directly.
*/
allGoals.add( "org.apache.maven.plugins:maven-clean-plugin:clean" );
}

allGoals.addAll( goals );

List<String> args = new ArrayList<String>();

int ret;
cliArguments.addAll( goals );
environmentVariables.putAll( envVars );
execute();
}

File logFile = new File( getBasedir(), getLogFileName() );
/**
* Execute Maven.
*/
public void execute() throws VerificationException
{

for ( String cliArgument : cliArguments )
{
args.add( cliArgument.replace( "${basedir}", getBasedir() ) );
}
List<String> args = new ArrayList<>();

Collections.addAll( args, defaultCliArguments );

Expand All @@ -1263,12 +1308,22 @@ public void executeGoals( List<String> goals, Map<String, String> envVars )
args.add( "-Dmaven.repo.local=" + localRepo );
}

args.addAll( allGoals );
if ( autoclean )
{
args.add( CLEAN_CLI_ARGUMENT );
}

try
for ( String cliArgument : cliArguments )
{
args.add( cliArgument.replace( "${basedir}", getBasedir() ) );
}

MavenLauncher launcher = getMavenLauncher( envVars );
int ret;
File logFile = new File( getBasedir(), getLogFileName() );

try
{
MavenLauncher launcher = getMavenLauncher( environmentVariables );

String[] cliArgs = args.toArray( new String[0] );
ret = launcher.run( cliArgs, systemProperties, getBasedir(), logFile );
Expand All @@ -1284,8 +1339,6 @@ public void executeGoals( List<String> goals, Map<String, String> envVars )

if ( ret > 0 )
{
System.err.println( "Exit code: " + ret );

throw new VerificationException(
"Exit code was non-zero: " + ret + "; command line and log = \n" + new File( mavenHome,
"bin/mvn" ) + " "
Expand Down Expand Up @@ -1697,6 +1750,13 @@ public boolean isAutoclean()
return autoclean;
}

/**
* Clean project before execution by adding {@link #CLEAN_CLI_ARGUMENT} to command line.
* <p>
* By default, options is enabled.
*
* @param autoclean indicate if option is enabled
*/
public void setAutoclean( boolean autoclean )
{
this.autoclean = autoclean;
Expand Down
29 changes: 28 additions & 1 deletion src/test/java/org/apache/maven/shared/verifier/VerifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public void testFileInJarPresent()
Verifier verifier = new Verifier( "src/test/resources" );
verifier.verifyFilePresent( "mshared104.jar!/pom.xml" );
verifier.verifyFileNotPresent( "mshared104.jar!/fud.xml" );
verifier.resetStreams();
}

@Test
Expand All @@ -115,7 +116,14 @@ public void testLoadPropertiesFNFE()
{
VerificationException exception = assertThrows( VerificationException.class, () -> {
Verifier verifier = new Verifier( "src/test/resources" );
verifier.loadProperties( "unknown.properties" );
try
{
verifier.loadProperties( "unknown.properties" );
}
finally
{
verifier.resetStreams();
}
} );
assertInstanceOf( FileNotFoundException.class, exception.getCause() );
}
Expand All @@ -126,6 +134,7 @@ public void testDedicatedMavenHome() throws VerificationException, IOException
String mavenHome = Paths.get( "src/test/resources/maven-home" ).toAbsolutePath().toString();
Verifier verifier = new Verifier( temporaryDir.toString(), null, false, mavenHome );
verifier.executeGoal( "some-goal" );
verifier.resetStreams();
Path logFile = Paths.get( verifier.getBasedir(), verifier.getLogFileName() );
ForkedLauncherTest.expectFileLine( logFile, "Hello World from Maven Home" );
}
Expand All @@ -135,6 +144,7 @@ void testDefaultFilterMap() throws VerificationException
{
Verifier verifier = new Verifier( "src/test/resources" );
Map<String, String> filterMap = verifier.newDefaultFilterMap();
verifier.resetStreams();

assertEquals( 2, filterMap.size() );
assertTrue( filterMap.containsKey( "@basedir@" ) );
Expand All @@ -147,6 +157,21 @@ void testDefaultMavenArgument() throws VerificationException
TestVerifier verifier = new TestVerifier( "src/test/resources" );

verifier.executeGoal( "test" );
verifier.resetStreams();

assertThat( verifier.launcher.cliArgs, arrayContaining(
"-e", "--batch-mode", "-Dmaven.repo.local=test-local-repo",
"org.apache.maven.plugins:maven-clean-plugin:clean", "test" ) );
}

@Test
void testDefaultMavenArgumentWithExecuteMethod() throws VerificationException
{
TestVerifier verifier = new TestVerifier( "src/test/resources" );

verifier.addCliArgument( "test" );
verifier.execute();
verifier.resetStreams();

assertThat( verifier.launcher.cliArgs, arrayContaining(
"-e", "--batch-mode", "-Dmaven.repo.local=test-local-repo",
Expand All @@ -170,6 +195,7 @@ void argumentShouldBePassedAsIs( String inputArgument, String expectedArgument )

verifier.addCliArgument( inputArgument );
verifier.executeGoal( "test" );
verifier.resetStreams();

assertThat( verifier.launcher.cliArgs, hasItemInArray( expectedArgument ) );
}
Expand All @@ -181,6 +207,7 @@ void addCliArgsShouldAddSeparateArguments() throws VerificationException

verifier.addCliArguments( "cliArg1", "cliArg2" );
verifier.executeGoal( "test" );
verifier.resetStreams();

assertThat( verifier.launcher.cliArgs, allOf(
hasItemInArray( "cliArg1" ), hasItemInArray( "cliArg2" ) ) );
Expand Down

0 comments on commit 6b9dbb5

Please sign in to comment.