This is an automated email from the ASF dual-hosted git repository.

olamy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git


The following commit(s) were added to refs/heads/master by this push:
     new 69289a36e Fix fork environment changes introduced by the 
ProcessBuilder switch (#3634)
69289a36e is described below

commit 69289a36effcf92d6dd733dfbb195e70ad687cd3
Author: Olivier Lamy <[email protected]>
AuthorDate: Wed Nov 25 07:50:04 2026 +1000

    Fix fork environment changes introduced by the ProcessBuilder switch (#3634)
    
    Since #3575 the forked JVM is started without a shell, which changed a few 
things:
    
    - on Windows, a configured PATH no longer replaced the inherited Path, the 
fork got both
    - on Windows, excluding PATH no longer removed Path
    - on Linux and macOS, PWD was Maven's directory instead of the fork's 
working directory
    
    Windows variable names are now matched ignoring case, and PWD is set to the 
working
    directory again.
---
 .../booterclient/lazytestprovider/Commandline.java |  5 ++-
 .../booterclient/ForkConfigurationTest.java        |  6 +--
 .../lazytestprovider/CommandlineTest.java          | 52 ++++++++++++++++++++++
 .../surefire/extensions/util/Commandline.java      | 12 ++++-
 4 files changed, 70 insertions(+), 5 deletions(-)

diff --git 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/Commandline.java
 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/Commandline.java
index 64db74a6c..797ab81f1 100644
--- 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/Commandline.java
+++ 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/Commandline.java
@@ -44,7 +44,10 @@ public Commandline(String[] excludedEnvironmentVariables) {
     @Override
     protected Map<String, String> environment() {
         Map<String, String> env = super.environment();
-        env.keySet().removeAll(excludedEnvironmentVariables);
+        // one at a time, so the map decides how names match: ignoring case on 
Windows
+        for (String name : excludedEnvironmentVariables) {
+            env.remove(name);
+        }
         return env;
     }
 }
diff --git 
a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java
 
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java
index 1324872c1..cf9a14980 100644
--- 
a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java
+++ 
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java
@@ -144,7 +144,7 @@ protected void resolveClasspath(
 
         assertThat(cli.getEnvironmentVariables())
                 .contains("key1=val1", "key2=val2", "key3=val3")
-                .doesNotContain("PATH=")
+                .allMatch(e -> !e.regionMatches(IS_OS_WINDOWS, 0, "PATH=", 0, 
5))
                 .doesNotHaveDuplicates();
     }
 
@@ -198,14 +198,14 @@ protected void resolveClasspath(
 
         assertThat(cliFork1.getEnvironmentVariables())
                 .contains("FORK_ID=1")
-                .doesNotContain("PATH=")
+                .allMatch(e -> !e.regionMatches(IS_OS_WINDOWS, 0, "PATH=", 0, 
5))
                 .doesNotHaveDuplicates();
 
         Commandline cliFork2 = config.createCommandLine(startup, 2, 
getTempDirectory());
 
         assertThat(cliFork2.getEnvironmentVariables())
                 .contains("FORK_ID=2")
-                .doesNotContain("PATH=")
+                .allMatch(e -> !e.regionMatches(IS_OS_WINDOWS, 0, "PATH=", 0, 
5))
                 .doesNotHaveDuplicates();
     }
 
diff --git 
a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/CommandlineTest.java
 
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/CommandlineTest.java
index b6f2354e3..ffd98952a 100644
--- 
a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/CommandlineTest.java
+++ 
b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/lazytestprovider/CommandlineTest.java
@@ -18,10 +18,16 @@
  */
 package org.apache.maven.plugin.surefire.booterclient.lazytestprovider;
 
+import java.io.File;
+import java.nio.file.Paths;
+
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+import org.junit.jupiter.api.condition.EnabledOnOs;
 
 import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.condition.OS.WINDOWS;
 
 /**
  *
@@ -41,6 +47,40 @@ public void shouldGetEnvironmentVariables() {
         assertThat(env).doesNotHaveDuplicates().allMatch(s -> s != null && 
!s.startsWith("JAVA_HOME="));
     }
 
+    @Test
+    @EnabledOnOs(WINDOWS)
+    public void shouldOverrideInheritedVariableIgnoringCaseOnWindows() {
+        Commandline cli = new Commandline();
+        cli.addEnvironment(otherCaseOfInheritedPath(), "C:\\configured");
+
+        assertThat(cli.getEnvironmentVariables())
+                .filteredOn(e -> e.regionMatches(true, 0, "PATH=", 0, 5))
+                .containsExactly(inheritedPathName() + "=C:\\configured");
+    }
+
+    @Test
+    @EnabledOnOs(WINDOWS)
+    public void shouldExcludeVariableIgnoringCaseOnWindows() {
+        Commandline cli = new Commandline(new String[] 
{otherCaseOfInheritedPath()});
+
+        assertThat(cli.getEnvironmentVariables()).noneMatch(e -> 
e.regionMatches(true, 0, "PATH=", 0, 5));
+    }
+
+    @Test
+    @DisabledOnOs(WINDOWS)
+    public void shouldSetPwdToWorkingDirectory() {
+        String userDir = System.getProperty("user.dir");
+        Commandline cli = new Commandline();
+        cli.setWorkingDirectory(new File(userDir, "target/../target"));
+
+        assertThat(cli.toProcessBuilder().environment())
+                .containsEntry("PWD", Paths.get(userDir, "target").toString());
+
+        cli.addEnvironment("PWD", "/configured");
+
+        assertThat(cli.toProcessBuilder().environment()).containsEntry("PWD", 
"/configured");
+    }
+
     @Test
     public void shouldExecute() throws Exception {
         Commandline cli = new Commandline();
@@ -51,4 +91,16 @@ public void shouldExecute() throws Exception {
         }
         cli.execute();
     }
+
+    private static String inheritedPathName() {
+        return System.getenv().keySet().stream()
+                .filter(name -> name.equalsIgnoreCase("PATH"))
+                .findFirst()
+                .orElseThrow(() -> new IllegalStateException("no PATH 
variable"));
+    }
+
+    // Path or PATH on Windows, so this gives a name that only matches when 
case is ignored
+    private static String otherCaseOfInheritedPath() {
+        return "PATH".equals(inheritedPathName()) ? "path" : "PATH";
+    }
 }
diff --git 
a/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/Commandline.java
 
b/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/Commandline.java
index 33d301a0c..36f10bf27 100644
--- 
a/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/Commandline.java
+++ 
b/surefire-extensions-api/src/main/java/org/apache/maven/surefire/extensions/util/Commandline.java
@@ -26,6 +26,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
+import java.util.TreeMap;
 
 /**
  * The command line of a process Surefire starts: an executable, its 
arguments, the environment and the working
@@ -112,11 +113,20 @@ public void addEnvironment(String name, String value) {
     /**
      * The environment the process gets: the environment of this JVM, with 
{@link #addEnvironment added} variables
      * overriding it. Subclasses may remove variables.
+     * <p>
+     * On Windows, names are matched ignoring case, like Windows does, so an 
added {@code PATH} replaces {@code Path}.
+     * Elsewhere, {@code PWD} is set to the working directory, as the shell 
used to start the process did.
      *
      * @return a mutable copy
      */
     protected Map<String, String> environment() {
-        Map<String, String> env = new LinkedHashMap<>(System.getenv());
+        Map<String, String> env = WINDOWS ? new 
TreeMap<>(String.CASE_INSENSITIVE_ORDER) : new LinkedHashMap<>();
+        env.putAll(System.getenv());
+        if (!WINDOWS && workingDirectory != null) {
+            env.put(
+                    "PWD",
+                    
workingDirectory.toPath().toAbsolutePath().normalize().toString());
+        }
         env.putAll(addedEnvironment);
         return env;
     }

Reply via email to