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:35:38 UTC

[maven] branch MNG-7032_versioncolours updated (923ac90 -> f667948)

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

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


 discard 923ac90  [MNG-7032] do not print colours for --version when in batch mode.
     new f667948  [MNG-7032] do not print colours for --version when in batch mode.

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (923ac90)
            \
             N -- N -- N   refs/heads/MNG-7032_versioncolours (f667948)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 maven-embedder/src/main/java/org/apache/maven/cli/CLIReportingUtils.java | 1 -
 1 file changed, 1 deletion(-)


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

Posted by bm...@apache.org.
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 f667948d0c9b656aa937632a8fdd512d21391fca
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.
---
 .../main/java/org/apache/maven/cli/MavenCli.java   | 21 +++++++++++--
 .../java/org/apache/maven/cli/MavenCliTest.java    | 36 +++++++++++++++++++++-
 2 files changed, 53 insertions(+), 4 deletions(-)

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();