[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 Michael Osipov
parent 73f34c00d1
commit 39254860d6
2 changed files with 44 additions and 1 deletions

View File

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

View File

@ -29,7 +29,12 @@ import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import org.apache.commons.cli.ParseException;
import org.apache.maven.Maven;
@ -331,4 +336,36 @@ public class MavenCliTest
orderdEventSpyDispatcherMock.verify(eventSpyDispatcherMock, times(1)).onEvent(any(ToolchainsBuildingResult.class));
}
/**
* 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 );
}
}