Skip to content

Commit

Permalink
[SUREFIRE-1124] Support forkNumber in environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Wismer authored and slawekjaranowski committed Aug 31, 2023
1 parent 2966964 commit 9b79bda
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public Commandline createCommandLine(

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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,68 @@ protected void resolveClasspath(
.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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9b79bda

Please sign in to comment.