You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by bm...@apache.org on 2020/12/23 16:32:39 UTC

[maven] 01/01: [MNG-7032] do not print colours for --version when in batch mode.

This is an automated email from the ASF dual-hosted git repository.

bmarwell pushed a commit to branch MNG-7032_versioncolours
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 923ac90b7a96cb1cb34544667aef5e230f870a72
Author: Benjamin Marwell <bm...@apache.org>
AuthorDate: Wed Dec 23 17:31:34 2020 +0100

    [MNG-7032] do not print colours for --version when in batch mode.
---
 .../org/apache/maven/cli/CLIReportingUtils.java    |  1 +
 .../main/java/org/apache/maven/cli/MavenCli.java   | 21 +++++++++++--
 .../java/org/apache/maven/cli/MavenCliTest.java    | 36 +++++++++++++++++++++-
 3 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/CLIReportingUtils.java b/maven-embedder/src/main/java/org/apache/maven/cli/CLIReportingUtils.java
index 97a2db6..e3719bc 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/CLIReportingUtils.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/CLIReportingUtils.java
@@ -29,6 +29,7 @@ import java.util.Locale;
 import java.util.Properties;
 
 import org.apache.commons.lang3.StringUtils;
+import org.apache.maven.shared.utils.logging.MessageUtils;
 import org.codehaus.plexus.util.Os;
 import org.slf4j.Logger;
 
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
index fd650f0..1005fdf 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
@@ -433,6 +433,8 @@ public class MavenCli
 
         if ( cliRequest.commandLine.hasOption( CLIManager.VERSION ) )
         {
+            // MNG-7032: Also disable colours if in batch mode
+            disableColorsInLogfileOrBatch( cliRequest );
             System.out.println( CLIReportingUtils.showVersion() );
             throw new ExitException( 0 );
         }
@@ -518,10 +520,9 @@ public class MavenCli
             throw new IllegalArgumentException( "Invalid color configuration option [" + styleColor
                 + "]. Supported values are (auto|always|never)." );
         }
-        else if ( cliRequest.commandLine.hasOption( CLIManager.BATCH_MODE )
-            || cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) )
+        else
         {
-            MessageUtils.setColorEnabled( false );
+            disableColorsInLogfileOrBatch( cliRequest );
         }
 
         // LOG STREAMS
@@ -569,6 +570,20 @@ public class MavenCli
         }
     }
 
+    /**
+     * Disables the colour output in the case that the {@link CLIManager#BATCH_MODE} option
+     * or {@link CLIManager#LOG_FILE} option was given (or both). In those cases, ANSI output is never feasible.
+     * @param cliRequest the arguments as request pojo.
+     */
+    private void disableColorsInLogfileOrBatch( CliRequest cliRequest )
+    {
+        if ( cliRequest.commandLine.hasOption( CLIManager.BATCH_MODE )
+                || cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) )
+        {
+            MessageUtils.setColorEnabled( false );
+        }
+    }
+
     private void version( CliRequest cliRequest )
     {
         if ( cliRequest.debug || cliRequest.commandLine.hasOption( CLIManager.SHOW_VERSION ) )
diff --git a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
index e60ee9d..13d44d1 100644
--- a/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
+++ b/maven-embedder/src/test/java/org/apache/maven/cli/MavenCliTest.java
@@ -37,7 +37,10 @@ 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;
 
@@ -57,7 +60,6 @@ import org.codehaus.plexus.DefaultPlexusContainer;
 import org.codehaus.plexus.PlexusContainer;
 import org.eclipse.sisu.plexus.PlexusBeanModule;
 import org.junit.After;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.InOrder;
@@ -466,6 +468,38 @@ public class MavenCliTest
                 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 )
     {
         MavenProject project = new MavenProject();