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

[WFCORE-6842] Do not add null environment variables to the ProcessBui… #6026

Merged
merged 1 commit into from
Jun 3, 2024
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
18 changes: 14 additions & 4 deletions launcher/src/main/java/org/wildfly/core/launcher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,27 +192,37 @@ public Launcher setDirectory(final String dir) {
}

/**
* Adds an environment variable to the process being created.
* Adds an environment variable to the process being created. If the key or value is {@code null}, the environment
* variable will not be added.
*
* @param key they key for the variable
* @param value the value for the variable
*
* @return the launcher
* @see ProcessBuilder#environment()
*/
public Launcher addEnvironmentVariable(final String key, final String value) {
env.put(key, value);
if (key != null && value != null) {
env.put(key, value);
}
return this;
}

/**
* Adds the environment variables to the process being created.
* Adds the environment variables to the process being created. Note that {@code null} keys or values will not be
* added.
*
* @param env the environment variables to add
*
* @return the launcher
* @see ProcessBuilder#environment()
*/
public Launcher addEnvironmentVariables(final Map<String, String> env) {
this.env.putAll(env);
env.forEach((key, value) -> {
if (key != null && value != null) {
addEnvironmentVariable(key, value);
}
});
return this;
}

Expand Down
82 changes: 82 additions & 0 deletions launcher/src/test/java/org/wildfly/core/launcher/LauncherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.wildfly.core.launcher;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public class LauncherTest {

private Path stdout;

@Before
public void setup() throws IOException {
stdout = Files.createTempFile("stdout", ".txt");
}

@After
public void deleteStdout() throws IOException {
if (stdout != null) {
Files.deleteIfExists(stdout);
}
}

@Test
public void checkSingleNullEnvironmentVariable() throws Exception {
final TestCommandBuilder commandBuilder = new TestCommandBuilder();
checkProcess(Launcher.of(commandBuilder).addEnvironmentVariable("TEST", null));
}

@Test
public void checkNullEnvironmentVariables() throws Exception {
final TestCommandBuilder commandBuilder = new TestCommandBuilder();
final Map<String, String> env = new HashMap<>();
env.put("TEST", null);
env.put("TEST_2", "test2");
checkProcess(Launcher.of(commandBuilder).addEnvironmentVariables(env));
}

private void checkProcess(final Launcher launcher) throws IOException, InterruptedException {
Process process = null;
try {
process = launcher.setRedirectErrorStream(true).redirectOutput(stdout).launch();
Assert.assertNotNull("Process should not be null", process);
Assert.assertTrue("Process should have exited within 5 seconds", process.waitFor(5, TimeUnit.SECONDS));
Assert.assertEquals(String.format("Process should have exited with an exit code of 0:%n%s", Files.readString(stdout)),
0, process.exitValue());
} finally {
ProcessHelper.destroyProcess(process);
}
}

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
private static class TestCommandBuilder implements CommandBuilder {
@Override
public List<String> buildArguments() {
return List.of();
}

@Override
public List<String> build() {
return List.of(Jvm.current().getCommand(), "-version");
}
}
}