Skip to content

Commit

Permalink
[MSHARED-969] Environment variable with null value
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Dec 29, 2020
1 parent 360fd97 commit 8a60ac8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<reuseForks>false</reuseForks>
<environmentVariables>
<TEST_SHARED_ENV>TestValue</TEST_SHARED_ENV>
</environmentVariables>
</configuration>
</plugin>
</plugins>
Expand Down
11 changes: 6 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 @@ -241,15 +241,16 @@ public void addSystemEnvironment()
public String[] getEnvironmentVariables()
{
addSystemEnvironment();
String[] environmentVars = new String[envVars.size()];
int i = 0;
List<String> environmentVars = new ArrayList<>();
for ( String name : envVars.keySet() )
{
String value = envVars.get( name );
environmentVars[i] = name + "=" + value;
i++;
if ( value != null )
{
environmentVars.add( name + "=" + value );
}
}
return environmentVars;
return environmentVars.toArray( new String[0] );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
* under the License.
*/

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItemInArray;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -168,4 +171,40 @@ private void assertCmdLineArgs( final String[] expected, final String cmdLine )
assertEquals( expected.length, actual.length );
assertEquals( Arrays.asList( expected ), Arrays.asList( actual ) );
}

@Test
public void environmentVariableWithNullShouldNotBeSet() {

Commandline commandline = new Commandline();
commandline.addEnvironment("TEST_NULL_ENV", null);

String[] environmentVariables = commandline.getEnvironmentVariables();

assertNotNull( environmentVariables );
assertThat( environmentVariables, not( hasItemInArray( "TEST_NULL_ENV=null" ) ) );
}

@Test
public void environmentVariableFromSystemIsCopied() {

Commandline commandline = new Commandline();

String[] environmentVariables = commandline.getEnvironmentVariables();

assertNotNull(environmentVariables);
assertThat(environmentVariables, hasItemInArray( "TEST_SHARED_ENV=TestValue" ) );
}

@Test
public void environmentVariableFromSystemIsRemoved() {

Commandline commandline = new Commandline();
commandline.addEnvironment("TEST_SHARED_ENV", null);

String[] environmentVariables = commandline.getEnvironmentVariables();

assertNotNull(environmentVariables);
assertThat(environmentVariables, not ( hasItemInArray( "TEST_SHARED_ENV=TestValue" ) ) );
}

}

0 comments on commit 8a60ac8

Please sign in to comment.