[MNG-7032] Evaluate --help and --version after configuring the logging/color

This commit is contained in:
Guillaume Nodet 2021-01-21 17:13:40 +01:00 committed by Martin Kanters
parent 59c5466c40
commit 3e917677e4
2 changed files with 42 additions and 1 deletions

View File

@ -173,6 +173,8 @@ public class MavenCli
private Map<String, ConfigurationProcessor> configurationProcessors; private Map<String, ConfigurationProcessor> configurationProcessors;
private CLIManager cliManager;
public MavenCli() public MavenCli()
{ {
this( null ); this( null );
@ -285,6 +287,7 @@ public class MavenCli
cli( cliRequest ); cli( cliRequest );
properties( cliRequest ); properties( cliRequest );
logging( cliRequest ); logging( cliRequest );
informativeCommands( cliRequest );
version( cliRequest ); version( cliRequest );
localContainer = container( cliRequest ); localContainer = container( cliRequest );
commands( cliRequest ); commands( cliRequest );
@ -374,7 +377,7 @@ public class MavenCli
// //
slf4jLogger = new Slf4jStdoutLogger(); slf4jLogger = new Slf4jStdoutLogger();
CLIManager cliManager = new CLIManager(); cliManager = new CLIManager();
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();
CommandLine mavenConfig = null; CommandLine mavenConfig = null;
@ -424,7 +427,10 @@ public class MavenCli
cliManager.displayHelp( System.out ); cliManager.displayHelp( System.out );
throw e; throw e;
} }
}
private void informativeCommands( CliRequest cliRequest ) throws ExitException
{
if ( cliRequest.commandLine.hasOption( CLIManager.HELP ) ) if ( cliRequest.commandLine.hasOption( CLIManager.HELP ) )
{ {
cliManager.displayHelp( System.out ); cliManager.displayHelp( System.out );

View File

@ -37,7 +37,10 @@ import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -446,6 +449,38 @@ public class MavenCliTest
is( "." + File.separatorChar + "custom2" ) ); is( "." + File.separatorChar + "custom2" ) );
} }
/**
* MNG-7032: Disable colours for {@code --version} if {@code --batch-mode} is also given.
* @throws Exception cli invocation.
*/
@Test
public void testVersionStringWithoutAnsi() throws Exception
{
// given
// - request with version and batch mode
CliRequest cliRequest = new CliRequest( new String[] {
"--version",
"--batch-mode"
}, null );
ByteArrayOutputStream systemOut = new ByteArrayOutputStream();
PrintStream oldOut = System.out;
System.setOut( new PrintStream( systemOut ) );
// when
try {
cli.cli( cliRequest );
} catch ( MavenCli.ExitException exitException ) {
// expected
} finally {
// restore sysout
System.setOut( oldOut );
}
String versionOut = new String( systemOut.toByteArray(), StandardCharsets.UTF_8 );
// then
assertEquals( MessageUtils.stripAnsiCodes( versionOut ), versionOut );
}
private MavenProject createMavenProject( String groupId, String artifactId ) private MavenProject createMavenProject( String groupId, String artifactId )
{ {
MavenProject project = new MavenProject(); MavenProject project = new MavenProject();