Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUREFIRE-1124] Support forkNumber in environment variables #664

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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