You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sj...@apache.org on 2022/01/03 17:12:37 UTC

[maven-shared-utils] 01/01: [MSHARED-1014] Optionally inherit system environment variables by Commandline

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

sjaranowski pushed a commit to branch MSHARED-1014
in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git

commit 7a21fbd1c845dad51cff0cf41523cd4ef117044f
Author: Slawomir Jaranowski <s....@gmail.com>
AuthorDate: Mon Jan 3 18:12:15 2022 +0100

    [MSHARED-1014] Optionally inherit system environment variables by Commandline
---
 .../apache/maven/shared/utils/cli/Commandline.java | 39 +++++++++++++++++++---
 .../shared/utils/cli/CommandLineUtilsTest.java     | 35 +++++++++++--------
 2 files changed, 56 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java b/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
index e03e905..7281589 100644
--- a/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
+++ b/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
@@ -65,12 +65,14 @@ import org.apache.maven.shared.utils.cli.shell.Shell;
 public class Commandline
     implements Cloneable
 {
-    private final List<Arg> arguments = new Vector<Arg>();
+    private final List<Arg> arguments = new Vector<>();
 
     private final Map<String, String> envVars = Collections.synchronizedMap( new LinkedHashMap<String, String>() );
 
     private Shell shell;
 
+    private boolean shellEnvironmentInherited = true;
+
     /**
      * Create a new command line object.
      * Shell is autodetected from operating system.
@@ -198,13 +200,16 @@ public class Commandline
      */
     public void addEnvironment( String name, String value )
     {
-        //envVars.add( name + "=" + value );
         envVars.put( name, value );
     }
 
     /**
      * Add system environment variables.
+     *
+     * @deprecated by default system environment variables are inherited by executing command.
+     * In order to change default behavior pleas use {@link #setShellEnvironmentInherited(boolean)}
      */
+    @Deprecated
     public void addSystemEnvironment()
     {
         Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();
@@ -226,7 +231,11 @@ public class Commandline
      */
     public String[] getEnvironmentVariables()
     {
-        addSystemEnvironment();
+        if ( isShellEnvironmentInherited() )
+        {
+            addSystemEnvironment();
+        }
+
         List<String> environmentVars = new ArrayList<>();
         for ( String name : envVars.keySet() )
         {
@@ -297,7 +306,7 @@ public class Commandline
      */
     public String[] getArguments( boolean mask )
     {
-        List<String> result = new ArrayList<String>( arguments.size() * 2 );
+        List<String> result = new ArrayList<>( arguments.size() * 2 );
         for ( Arg argument : arguments )
         {
             Argument arg = (Argument) argument;
@@ -378,6 +387,28 @@ public class Commandline
     }
 
     /**
+     * Indicates whether the environment variables of the current process should be propagated to the executing Command.
+     * By default, the current environment variables are inherited by the new Command line execution.
+     *
+     * @return <code>true</code> if the environment variables should be propagated, <code>false</code> otherwise.
+     */
+    public boolean isShellEnvironmentInherited()
+    {
+        return shellEnvironmentInherited;
+    }
+
+    /**
+     * Specifies whether the environment variables of the current process should be propagated to the executing Command.
+     *
+     * @param shellEnvironmentInherited <code>true</code> if the environment variables should be propagated,
+     *            <code>false</code> otherwise.
+     */
+    public void setShellEnvironmentInherited( boolean shellEnvironmentInherited )
+    {
+        this.shellEnvironmentInherited = shellEnvironmentInherited;
+    }
+
+    /**
      * Execute the command.
      * 
      * @return the process
diff --git a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
index 81b2997..2605eaa 100644
--- a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
@@ -22,6 +22,7 @@ package org.apache.maven.shared.utils.cli;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.hasItemInArray;
 import static org.hamcrest.Matchers.not;
+import static org.hamcrest.collection.IsArrayWithSize.emptyArray;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -56,9 +57,8 @@ public class CommandLineUtilsTest
 
     @Test
     public void testEnsureCaseSensitivity()
-        throws Exception
     {
-        Map<String, String> data = new HashMap<String, String>();
+        Map<String, String> data = new HashMap<>();
         data.put( "abz", "cool" );
         assertTrue( CommandLineUtils.ensureCaseSensitivity( data, false ).containsKey( "ABZ" ) );
         assertTrue( CommandLineUtils.ensureCaseSensitivity( data, true ).containsKey( "abz" ) );
@@ -69,7 +69,6 @@ public class CommandLineUtilsTest
      */
     @Test
     public void testGetSystemEnvVarsWindows()
-        throws Exception
     {
         if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
         {
@@ -103,14 +102,9 @@ public class CommandLineUtilsTest
         assertCmdLineArgs( new String[] { "foo", " ' ", "bar" }, "foo \" ' \" bar" );
     }
 
-
     @Test
-    public void givenASingleQuoteMarkInArgument_whenExecutingCode_thenNoExceptionIsThrown() throws Exception {
-        new Commandline("echo \"let's go\"").execute();
-    }
-
-    @Test
-    public void givenADoubleQuoteMarkInArgument_whenExecutingCode_thenCommandLineExceptionIsThrown() throws Exception {
+    public void givenADoubleQuoteMarkInArgument_whenExecutingCode_thenCommandLineExceptionIsThrown()
+    {
         try {
             new Commandline("echo \"let\"s go\"").execute();
         } catch (CommandLineException e) {
@@ -124,8 +118,7 @@ public class CommandLineUtilsTest
     @Test
     public void givenASingleQuoteMarkInArgument_whenExecutingCode_thenExitCode0Returned() throws Exception {
         final Process p = new Commandline("echo \"let's go\"").execute();
-        // Note, this sleep should be removed when java version reaches Java 8
-        Thread.sleep(1000);
+        p.waitFor();
         assertEquals(0, p.exitValue());
     }
 
@@ -160,7 +153,9 @@ public class CommandLineUtilsTest
     public void givenAnEscapedDoubleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenNoExceptionIsThrown()
         throws Exception
     {
-        new Commandline( "echo \"let\\\"s go\"" ).execute();
+        Process p = new Commandline( "echo \"let\\\"s go\"" ).execute();
+        p.waitFor();
+        assertEquals(0, p.exitValue());
     }
 
     private void assertCmdLineArgs( final String[] expected, final String cmdLine )
@@ -185,7 +180,7 @@ public class CommandLineUtilsTest
     }
 
     @Test
-    public void environmentVariableFromSystemIsCopied() {
+    public void environmentVariableFromSystemIsCopiedByDefault() {
 
         Commandline commandline = new Commandline();
 
@@ -196,6 +191,18 @@ public class CommandLineUtilsTest
     }
 
     @Test
+    public void environmentVariableFromSystemIsNotCopiedIfInheritedIsFalse() {
+
+        Commandline commandline = new Commandline();
+        commandline.setShellEnvironmentInherited( false );
+
+        String[] environmentVariables = commandline.getEnvironmentVariables();
+
+        assertNotNull(environmentVariables);
+        assertThat(environmentVariables, emptyArray() );
+    }
+
+    @Test
     public void environmentVariableFromSystemIsRemoved() {
 
         Commandline commandline = new Commandline();