Skip to content
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 @@ -62,7 +62,7 @@ public abstract class JavaScriptProvider extends Provider {

public JavaScriptProvider(Path manifest, Ecosystem.Type ecosystem, String cmd) {
super(ecosystem, manifest);
this.cmd = Operations.getExecutable(cmd, "-v");
this.cmd = Operations.getExecutable(cmd, "-v", getExecEnv());
Comment thread
ruromero marked this conversation as resolved.
try {
this.manifest = new Manifest(manifest);
} catch (IOException e) {
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/redhat/exhort/tools/Operations.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,17 @@ public int getExitCode() {
* @throws RuntimeException if the executable cannot be found, is not executable, or exits with an
* error code
*/
public static String getExecutable(String command, String args) {
public static String getExecutable(
String command, String args, final Map<String, String> envMap) {
String cmdExecutable = Operations.getCustomPathOrElse(command);
try {
Process process = new ProcessBuilder(cmdExecutable, args).redirectErrorStream(true).start();
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(cmdExecutable, args);
if (envMap != null) {
processBuilder.environment().putAll(envMap);
}
Process process = processBuilder.start();

int exitCode = process.waitFor();
if (exitCode != 0) {
throw new IOException(
Expand All @@ -265,4 +272,8 @@ public static String getExecutable(String command, String args) {
}
return cmdExecutable;
}

public static String getExecutable(String command, String args) {
return getExecutable(command, args, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mockStatic;

import com.redhat.exhort.tools.Operations;
import com.redhat.exhort.utils.Environment;
import java.io.File;
import java.nio.file.Path;
Expand All @@ -37,18 +41,28 @@ public class Javascript_Envs_Test {
@SetSystemProperty(key = "NODE_HOME", value = "test-node-home")
@SetSystemProperty(key = "PATH", value = "test-path")
void test_javascript_get_envs() {
var envs = new JavaScriptNpmProvider(MANIFEST_PATH).getExecEnv();
assertEquals(
Collections.singletonMap("PATH", "test-path" + File.pathSeparator + "test-node-home"),
envs);
try (MockedStatic<Operations> mockedOperations = mockStatic(Operations.class)) {
// Configure the mock to return "npm" when getExecutable is called
mockedOperations
.when(() -> Operations.getExecutable(anyString(), anyString(), anyMap()))
.thenReturn("npm");
var envs = new JavaScriptNpmProvider(MANIFEST_PATH).getExecEnv();
assertEquals(
Collections.singletonMap("PATH", "test-path" + File.pathSeparator + "test-node-home"),
envs);
}
}

@Test
@SetSystemProperty(key = "NODE_HOME", value = "test-node-home")
void test_javascript_get_envs_no_path() {
try (MockedStatic<Environment> mockEnv =
Mockito.mockStatic(Environment.class, Mockito.CALLS_REAL_METHODS)) {
mockStatic(Environment.class, Mockito.CALLS_REAL_METHODS);
MockedStatic<Operations> mockOps = mockStatic(Operations.class)) {
mockEnv.when(() -> Environment.get("PATH")).thenReturn(null);
mockOps
.when(() -> Operations.getExecutable(anyString(), anyString(), anyMap()))
.thenReturn("npm");
var envs = new JavaScriptNpmProvider(MANIFEST_PATH).getExecEnv();
assertEquals(Collections.singletonMap("PATH", "test-node-home"), envs);
}
Expand Down
Loading