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

sjaranowski 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 9b79bdab7 [SUREFIRE-1124] Support forkNumber in environment variables
9b79bdab7 is described below

commit 9b79bdab7180aa00642e2d9dd85167e0fcaa662d
Author: Stefan Wismer <[email protected]>
AuthorDate: Mon Jun 19 11:41:53 2023 +0200

    [SUREFIRE-1124] Support forkNumber in environment variables
---
 .../booterclient/DefaultForkConfiguration.java     |  3 ++
 .../booterclient/ForkConfigurationTest.java        | 62 ++++++++++++++++++++++
 .../fork-options-and-parallel-execution.apt.vm     |  9 ++--
 3 files changed, 70 insertions(+), 4 deletions(-)

diff --git 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java
 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java
index d469c15d0..d78f6807d 100644
--- 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java
+++ 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java
@@ -157,6 +157,9 @@ public abstract class DefaultForkConfiguration extends 
ForkConfiguration {
 
             for (Entry<String, String> entry : 
getEnvironmentVariables().entrySet()) {
                 String value = entry.getValue();
+                if (value != null) {
+                    value = replaceThreadNumberPlaceholders(value, forkNumber);
+                }
                 cli.addEnvironment(entry.getKey(), value == null ? "" : value);
             }
 
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 708ec1260..2209a808d 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
@@ -146,6 +146,68 @@ public class ForkConfigurationTest {
                 .doesNotHaveDuplicates();
     }
 
+    @Test
+    public void testEnvInterpolateForkNumber() throws Exception {
+        Map<String, String> env = new HashMap<>();
+        env.put("FORK_ID", "${surefire.forkNumber}");
+        String[] exclEnv = {"PATH"};
+
+        String jvm = new File(new File(System.getProperty("java.home"), 
"bin"), "java").getCanonicalPath();
+        Platform platform = new Platform().withJdkExecAttributesForTests(new 
JdkAttributes(jvm, false));
+
+        ForkConfiguration config =
+                new DefaultForkConfiguration(
+                        emptyClasspath(),
+                        basedir,
+                        "",
+                        basedir,
+                        new Properties(),
+                        "",
+                        env,
+                        exclEnv,
+                        false,
+                        2,
+                        true,
+                        platform,
+                        new NullConsoleLogger(),
+                        mock(ForkNodeFactory.class)) {
+
+                    @Override
+                    protected void resolveClasspath(
+                            @Nonnull Commandline cli,
+                            @Nonnull String booterThatHasMainMethod,
+                            @Nonnull StartupConfiguration config,
+                            @Nonnull File dumpLogDirectory) {}
+                };
+
+        List<String[]> providerJpmsArgs = new ArrayList<>();
+        providerJpmsArgs.add(new String[] {"arg2", "arg3"});
+
+        File cpElement = getTempClasspathFile();
+        List<String> cp = singletonList(cpElement.getAbsolutePath());
+
+        ClasspathConfiguration cpConfig =
+                new ClasspathConfiguration(new Classpath(cp), 
emptyClasspath(), emptyClasspath(), true, true);
+        ClassLoaderConfiguration clc = new ClassLoaderConfiguration(true, 
true);
+        StartupConfiguration startup = new StartupConfiguration("cls", 
cpConfig, clc, ALL, providerJpmsArgs);
+
+        org.apache.maven.surefire.shared.utils.cli.Commandline cliFork1 =
+                config.createCommandLine(startup, 1, getTempDirectory());
+
+        assertThat(cliFork1.getEnvironmentVariables())
+                .contains("FORK_ID=1")
+                .doesNotContain("PATH=")
+                .doesNotHaveDuplicates();
+
+        org.apache.maven.surefire.shared.utils.cli.Commandline cliFork2 =
+                config.createCommandLine(startup, 2, getTempDirectory());
+
+        assertThat(cliFork2.getEnvironmentVariables())
+                .contains("FORK_ID=2")
+                .doesNotContain("PATH=")
+                .doesNotHaveDuplicates();
+    }
+
     @Test
     public void testCliArgs() throws Exception {
         String jvm = new File(new File(System.getProperty("java.home"), 
"bin"), "java").getCanonicalPath();
diff --git 
a/maven-surefire-plugin/src/site/apt/examples/fork-options-and-parallel-execution.apt.vm
 
b/maven-surefire-plugin/src/site/apt/examples/fork-options-and-parallel-execution.apt.vm
index 79b821a57..7cdbf4a36 100644
--- 
a/maven-surefire-plugin/src/site/apt/examples/fork-options-and-parallel-execution.apt.vm
+++ 
b/maven-surefire-plugin/src/site/apt/examples/fork-options-and-parallel-execution.apt.vm
@@ -281,16 +281,17 @@ public class TestSuite {
   specify variables and values to be added to the system properties during the
   test execution.
 
-  You can use the place holder <<<$\{surefire.forkNumber\}>>> within 
-  <<<argLine>>>, or within the system properties (both those specified via 
+  You can use the placeholder <<<$\{surefire.forkNumber\}>>> within 
<<<argLine>>>,
+  <<<environmentVariables>>> (since ${project.artifactId}:3.2.0),
+  or within the system properties (both those specified via
   <<<mvn test -D...>>> and via <<<systemPropertyVariables>>>). Before 
executing 
-  the tests, the ${thisPlugin.toLowerCase()} plugin replaces that place holder
+  the tests, the ${thisPlugin.toLowerCase()} plugin replaces that placeholder
   by the number of the actually executing process, counting from 1 to the
   effective value of <<<forkCount>>> times the maximum number of parallel
   executions in Maven parallel builds, i.e. the effective value of the <<<-T>>>
   command line argument of Maven core.
 
-  In case of disabled forking (<<<forkCount=0>>>), the place holder will be
+  In case of disabled forking (<<<forkCount=0>>>), the placeholder will be
   replaced with <1>.
 
   The following is an example configuration that makes use of up to three 
forked

Reply via email to