From ba3468ef720365d4e14786718b99955ea536fe1a Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Mon, 3 Jan 2022 18:12:15 +0100 Subject: [PATCH] [MSHARED-1014] Optionally inherit system environment variables by Commandline --- .../maven/shared/utils/cli/Commandline.java | 37 ++++++++++++++++--- .../utils/cli/CommandLineUtilsTest.java | 35 +++++++++++------- 2 files changed, 53 insertions(+), 19 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 e03e905c..203570b4 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 @@ public class Commandline implements Cloneable { - private final List arguments = new Vector(); + private final List arguments = new Vector<>(); private final Map envVars = Collections.synchronizedMap( new LinkedHashMap() ); private Shell shell; + private boolean shellEnvironmentInherited = true; + /** * Create a new command line object. * Shell is autodetected from operating system. @@ -198,14 +200,13 @@ public void addArguments( String... line ) */ public void addEnvironment( String name, String value ) { - //envVars.add( name + "=" + value ); envVars.put( name, value ); } /** * Add system environment variables. */ - public void addSystemEnvironment() + private void addSystemEnvironment() { Properties systemEnvVars = CommandLineUtils.getSystemEnvVars(); @@ -226,7 +227,11 @@ public void addSystemEnvironment() */ public String[] getEnvironmentVariables() { - addSystemEnvironment(); + if ( isShellEnvironmentInherited() ) + { + addSystemEnvironment(); + } + List environmentVars = new ArrayList<>(); for ( String name : envVars.keySet() ) { @@ -297,7 +302,7 @@ public String[] getArguments() */ public String[] getArguments( boolean mask ) { - List result = new ArrayList( arguments.size() * 2 ); + List result = new ArrayList<>( arguments.size() * 2 ); for ( Arg argument : arguments ) { Argument arg = (Argument) argument; @@ -377,6 +382,28 @@ public void clearArgs() arguments.clear(); } + /** + * 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 true if the environment variables should be propagated, false otherwise. + */ + public boolean isShellEnvironmentInherited() + { + return shellEnvironmentInherited; + } + + /** + * Specifies whether the environment variables of the current process should be propagated to the executing Command. + * + * @param shellEnvironmentInherited true if the environment variables should be propagated, + * false otherwise. + */ + public void setShellEnvironmentInherited( boolean shellEnvironmentInherited ) + { + this.shellEnvironmentInherited = shellEnvironmentInherited; + } + /** * Execute the command. * 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 81b29971..2605eaab 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 @@ 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 void testGetSystemEnvVarsCaseInsensitive() @Test public void testEnsureCaseSensitivity() - throws Exception { - Map data = new HashMap(); + Map 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 void testEnsureCaseSensitivity() */ @Test public void testGetSystemEnvVarsWindows() - throws Exception { if ( !Os.isFamily( Os.FAMILY_WINDOWS ) ) { @@ -103,14 +102,9 @@ public void testTranslateCommandline() 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 void givenADoubleQuoteMarkInArgument_whenExecutingCode_thenCommandLineExc @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 void givenAnEscapedSingleQuoteMarkInArgument_whenTranslatingToCmdLineArgs 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 void environmentVariableWithNullShouldNotBeSet() { } @Test - public void environmentVariableFromSystemIsCopied() { + public void environmentVariableFromSystemIsCopiedByDefault() { Commandline commandline = new Commandline(); @@ -195,6 +190,18 @@ public void environmentVariableFromSystemIsCopied() { assertThat(environmentVariables, hasItemInArray( "TEST_SHARED_ENV=TestValue" ) ); } + @Test + public void environmentVariableFromSystemIsNotCopiedIfInheritedIsFalse() { + + Commandline commandline = new Commandline(); + commandline.setShellEnvironmentInherited( false ); + + String[] environmentVariables = commandline.getEnvironmentVariables(); + + assertNotNull(environmentVariables); + assertThat(environmentVariables, emptyArray() ); + } + @Test public void environmentVariableFromSystemIsRemoved() {