Skip to content

Commit

Permalink
[MSHARED-971] Add test for inherited environment
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 7, 2024
1 parent 74dadfd commit 9cbe8b8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ under the License.
<maven.home>${maven.home}</maven.home>
<maven.repo.local>${settings.localRepository}</maven.repo.local>
</systemPropertyVariables>
<environmentVariables>
<INVOKER_TEST_ENV_1>test-env-value</INVOKER_TEST_ENV_1>
</environmentVariables>
<excludes>
<exclude>test-build-should*/**</exclude>
</excludes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
import org.apache.maven.shared.utils.StringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class DefaultInvokerTest {
Expand All @@ -42,6 +45,7 @@ class DefaultInvokerTest {
public void setUp() {
request.setDebug(true);
request.setProperties(getProperties());
request.setBatchMode(true);
}

@Test
Expand Down Expand Up @@ -184,6 +188,35 @@ void testMavenWrapperInProject() throws Exception {
}
}

@Test
@DisabledOnOs(OS.WINDOWS) // mvn 3.6.3 and windows ...
void notInheritEnvVariables() throws Exception {

// ensure that we have env variable in current process
assertEquals("test-env-value", System.getenv("INVOKER_TEST_ENV_1"));

File basedir = getBasedirForBuild();
request.setBaseDirectory(basedir);
request.addArg("initialize");
request.setShellEnvironmentInherited(false);
request.addShellEnvironment("INVOKER_TEST_ENV_2", "test-env-value-2");
// Maven 3.6.3 required JAVA_HOME
request.addShellEnvironment("JAVA_HOME", System.getProperty("java.home"));

final StringBuilder outlines = new StringBuilder();
request.setOutputHandler(line -> outlines.append(line).append(System.lineSeparator()));

InvocationResult result = invoker.execute(request);

String output = outlines.toString();
assertEquals(
0,
result.getExitCode(),
() -> "Maven exit code: " + result.getExitCode() + System.lineSeparator() + output.trim());
assertFalse(output.contains("INVOKER_TEST_ENV_1"));
assertTrue(output.contains("INVOKER_TEST_ENV_2=test-env-value-2"));
}

private Invoker newInvoker() {
Invoker invoker = new DefaultInvoker();

Expand Down
44 changes: 44 additions & 0 deletions src/test/resources/not-inherit-env-variables/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.shared.invoker</groupId>
<artifactId>not-inherit-env-variables</artifactId>
<packaging>pom</packaging>
<version>1</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-help-plugin</artifactId>
<version>${version.maven-help-plugin}</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>system</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 9cbe8b8

Please sign in to comment.