Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-1014] Optionally inherit system environment variables by Commandline #94

Merged
merged 1 commit into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@
public class Commandline
implements Cloneable
{
private final List<Arg> arguments = new Vector<Arg>();
private final List<Arg> arguments = new Vector<>();
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved

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.
Expand Down Expand Up @@ -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()
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
{
Properties systemEnvVars = CommandLineUtils.getSystemEnvVars();

Expand All @@ -226,7 +227,11 @@ public void addSystemEnvironment()
*/
public String[] getEnvironmentVariables()
{
addSystemEnvironment();
if ( isShellEnvironmentInherited() )
{
addSystemEnvironment();
}

List<String> environmentVars = new ArrayList<>();
for ( String name : envVars.keySet() )
{
Expand Down Expand Up @@ -297,7 +302,7 @@ public String[] getArguments()
*/
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;
Expand Down Expand Up @@ -377,6 +382,29 @@ public void clearArgs()
arguments.clear();
}

/**
* Indicates whether the environment variables of the current process
* should are 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ public void testGetSystemEnvVarsCaseInsensitive()

@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" ) );
Expand All @@ -69,7 +68,6 @@ public void testEnsureCaseSensitivity()
*/
@Test
public void testGetSystemEnvVarsWindows()
throws Exception
{
if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
{
Expand Down Expand Up @@ -103,14 +101,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) {
Expand All @@ -124,8 +117,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());
}

Expand Down Expand Up @@ -160,7 +152,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 )
Expand All @@ -185,7 +179,7 @@ public void environmentVariableWithNullShouldNotBeSet() {
}

@Test
public void environmentVariableFromSystemIsCopied() {
public void environmentVariableFromSystemIsCopiedByDefault() {

Commandline commandline = new Commandline();

Expand All @@ -195,6 +189,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);
assertEquals(0, environmentVariables.length );
}

@Test
public void environmentVariableFromSystemIsRemoved() {

Expand Down