From 9cbe8b8eb1c7cc5e9700c72881337ddc29c09d6f Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Tue, 7 May 2024 21:43:17 +0200 Subject: [PATCH] [MSHARED-971] Add test for inherited environment --- pom.xml | 3 ++ .../shared/invoker/DefaultInvokerTest.java | 33 ++++++++++++++ .../not-inherit-env-variables/pom.xml | 44 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/test/resources/not-inherit-env-variables/pom.xml diff --git a/pom.xml b/pom.xml index 74b78e9..863f8da 100644 --- a/pom.xml +++ b/pom.xml @@ -105,6 +105,9 @@ under the License. ${maven.home} ${settings.localRepository} + + test-env-value + test-build-should*/** diff --git a/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java b/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java index 6a94b64..8584e56 100644 --- a/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java +++ b/src/test/java/org/apache/maven/shared/invoker/DefaultInvokerTest.java @@ -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 { @@ -42,6 +45,7 @@ class DefaultInvokerTest { public void setUp() { request.setDebug(true); request.setProperties(getProperties()); + request.setBatchMode(true); } @Test @@ -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(); diff --git a/src/test/resources/not-inherit-env-variables/pom.xml b/src/test/resources/not-inherit-env-variables/pom.xml new file mode 100644 index 0000000..90aee9f --- /dev/null +++ b/src/test/resources/not-inherit-env-variables/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + org.apache.maven.shared.invoker + not-inherit-env-variables + pom + 1 + + + + + org.apache.maven.plugins + maven-help-plugin + ${version.maven-help-plugin} + + + initialize + + system + + + + + + +