Skip to content

Commit 9b79bda

Browse files
Stefan Wismerslawekjaranowski
Stefan Wismer
authored andcommitted
[SUREFIRE-1124] Support forkNumber in environment variables
1 parent 2966964 commit 9b79bda

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/DefaultForkConfiguration.java

+3
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ public Commandline createCommandLine(
157157

158158
for (Entry<String, String> entry : getEnvironmentVariables().entrySet()) {
159159
String value = entry.getValue();
160+
if (value != null) {
161+
value = replaceThreadNumberPlaceholders(value, forkNumber);
162+
}
160163
cli.addEnvironment(entry.getKey(), value == null ? "" : value);
161164
}
162165

maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkConfigurationTest.java

+62
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,68 @@ protected void resolveClasspath(
146146
.doesNotHaveDuplicates();
147147
}
148148

149+
@Test
150+
public void testEnvInterpolateForkNumber() throws Exception {
151+
Map<String, String> env = new HashMap<>();
152+
env.put("FORK_ID", "${surefire.forkNumber}");
153+
String[] exclEnv = {"PATH"};
154+
155+
String jvm = new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath();
156+
Platform platform = new Platform().withJdkExecAttributesForTests(new JdkAttributes(jvm, false));
157+
158+
ForkConfiguration config =
159+
new DefaultForkConfiguration(
160+
emptyClasspath(),
161+
basedir,
162+
"",
163+
basedir,
164+
new Properties(),
165+
"",
166+
env,
167+
exclEnv,
168+
false,
169+
2,
170+
true,
171+
platform,
172+
new NullConsoleLogger(),
173+
mock(ForkNodeFactory.class)) {
174+
175+
@Override
176+
protected void resolveClasspath(
177+
@Nonnull Commandline cli,
178+
@Nonnull String booterThatHasMainMethod,
179+
@Nonnull StartupConfiguration config,
180+
@Nonnull File dumpLogDirectory) {}
181+
};
182+
183+
List<String[]> providerJpmsArgs = new ArrayList<>();
184+
providerJpmsArgs.add(new String[] {"arg2", "arg3"});
185+
186+
File cpElement = getTempClasspathFile();
187+
List<String> cp = singletonList(cpElement.getAbsolutePath());
188+
189+
ClasspathConfiguration cpConfig =
190+
new ClasspathConfiguration(new Classpath(cp), emptyClasspath(), emptyClasspath(), true, true);
191+
ClassLoaderConfiguration clc = new ClassLoaderConfiguration(true, true);
192+
StartupConfiguration startup = new StartupConfiguration("cls", cpConfig, clc, ALL, providerJpmsArgs);
193+
194+
org.apache.maven.surefire.shared.utils.cli.Commandline cliFork1 =
195+
config.createCommandLine(startup, 1, getTempDirectory());
196+
197+
assertThat(cliFork1.getEnvironmentVariables())
198+
.contains("FORK_ID=1")
199+
.doesNotContain("PATH=")
200+
.doesNotHaveDuplicates();
201+
202+
org.apache.maven.surefire.shared.utils.cli.Commandline cliFork2 =
203+
config.createCommandLine(startup, 2, getTempDirectory());
204+
205+
assertThat(cliFork2.getEnvironmentVariables())
206+
.contains("FORK_ID=2")
207+
.doesNotContain("PATH=")
208+
.doesNotHaveDuplicates();
209+
}
210+
149211
@Test
150212
public void testCliArgs() throws Exception {
151213
String jvm = new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath();

maven-surefire-plugin/src/site/apt/examples/fork-options-and-parallel-execution.apt.vm

+5-4
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,17 @@ public class TestSuite {
281281
specify variables and values to be added to the system properties during the
282282
test execution.
283283

284-
You can use the place holder <<<$\{surefire.forkNumber\}>>> within
285-
<<<argLine>>>, or within the system properties (both those specified via
284+
You can use the placeholder <<<$\{surefire.forkNumber\}>>> within <<<argLine>>>,
285+
<<<environmentVariables>>> (since ${project.artifactId}:3.2.0),
286+
or within the system properties (both those specified via
286287
<<<mvn test -D...>>> and via <<<systemPropertyVariables>>>). Before executing
287-
the tests, the ${thisPlugin.toLowerCase()} plugin replaces that place holder
288+
the tests, the ${thisPlugin.toLowerCase()} plugin replaces that placeholder
288289
by the number of the actually executing process, counting from 1 to the
289290
effective value of <<<forkCount>>> times the maximum number of parallel
290291
executions in Maven parallel builds, i.e. the effective value of the <<<-T>>>
291292
command line argument of Maven core.
292293

293-
In case of disabled forking (<<<forkCount=0>>>), the place holder will be
294+
In case of disabled forking (<<<forkCount=0>>>), the placeholder will be
294295
replaced with <1>.
295296

296297
The following is an example configuration that makes use of up to three forked

0 commit comments

Comments
 (0)