Skip to content

Commit 1db5b9b

Browse files
authored
fix: add necessary envs (if has) to validate executable binary in the PATH (#137)
## Description fix: add necessary envs (if has) to validate executable binary in the PATH **Related issue (if any):** amend the fix made in #134 ## Checklist - [x] I have followed this repository's contributing guidelines. - [x] I will adhere to the project's code of conduct. ## Additional information
1 parent ba3ddb5 commit 1db5b9b

3 files changed

Lines changed: 33 additions & 8 deletions

File tree

src/main/java/com/redhat/exhort/providers/JavaScriptProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public abstract class JavaScriptProvider extends Provider {
6262

6363
public JavaScriptProvider(Path manifest, Ecosystem.Type ecosystem, String cmd) {
6464
super(ecosystem, manifest);
65-
this.cmd = Operations.getExecutable(cmd, "-v");
65+
this.cmd = Operations.getExecutable(cmd, "-v", getExecEnv());
6666
try {
6767
this.manifest = new Manifest(manifest);
6868
} catch (IOException e) {

src/main/java/com/redhat/exhort/tools/Operations.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,17 @@ public int getExitCode() {
242242
* @throws RuntimeException if the executable cannot be found, is not executable, or exits with an
243243
* error code
244244
*/
245-
public static String getExecutable(String command, String args) {
245+
public static String getExecutable(
246+
String command, String args, final Map<String, String> envMap) {
246247
String cmdExecutable = Operations.getCustomPathOrElse(command);
247248
try {
248-
Process process = new ProcessBuilder(cmdExecutable, args).redirectErrorStream(true).start();
249+
ProcessBuilder processBuilder = new ProcessBuilder();
250+
processBuilder.command(cmdExecutable, args);
251+
if (envMap != null) {
252+
processBuilder.environment().putAll(envMap);
253+
}
254+
Process process = processBuilder.start();
255+
249256
int exitCode = process.waitFor();
250257
if (exitCode != 0) {
251258
throw new IOException(
@@ -265,4 +272,8 @@ public static String getExecutable(String command, String args) {
265272
}
266273
return cmdExecutable;
267274
}
275+
276+
public static String getExecutable(String command, String args) {
277+
return getExecutable(command, args, null);
278+
}
268279
}

src/test/java/com/redhat/exhort/providers/Javascript_Envs_Test.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
1919
import static org.junit.jupiter.api.Assertions.assertNull;
20+
import static org.mockito.ArgumentMatchers.anyMap;
21+
import static org.mockito.ArgumentMatchers.anyString;
22+
import static org.mockito.Mockito.mockStatic;
2023

24+
import com.redhat.exhort.tools.Operations;
2125
import com.redhat.exhort.utils.Environment;
2226
import java.io.File;
2327
import java.nio.file.Path;
@@ -37,18 +41,28 @@ public class Javascript_Envs_Test {
3741
@SetSystemProperty(key = "NODE_HOME", value = "test-node-home")
3842
@SetSystemProperty(key = "PATH", value = "test-path")
3943
void test_javascript_get_envs() {
40-
var envs = new JavaScriptNpmProvider(MANIFEST_PATH).getExecEnv();
41-
assertEquals(
42-
Collections.singletonMap("PATH", "test-path" + File.pathSeparator + "test-node-home"),
43-
envs);
44+
try (MockedStatic<Operations> mockedOperations = mockStatic(Operations.class)) {
45+
// Configure the mock to return "npm" when getExecutable is called
46+
mockedOperations
47+
.when(() -> Operations.getExecutable(anyString(), anyString(), anyMap()))
48+
.thenReturn("npm");
49+
var envs = new JavaScriptNpmProvider(MANIFEST_PATH).getExecEnv();
50+
assertEquals(
51+
Collections.singletonMap("PATH", "test-path" + File.pathSeparator + "test-node-home"),
52+
envs);
53+
}
4454
}
4555

4656
@Test
4757
@SetSystemProperty(key = "NODE_HOME", value = "test-node-home")
4858
void test_javascript_get_envs_no_path() {
4959
try (MockedStatic<Environment> mockEnv =
50-
Mockito.mockStatic(Environment.class, Mockito.CALLS_REAL_METHODS)) {
60+
mockStatic(Environment.class, Mockito.CALLS_REAL_METHODS);
61+
MockedStatic<Operations> mockOps = mockStatic(Operations.class)) {
5162
mockEnv.when(() -> Environment.get("PATH")).thenReturn(null);
63+
mockOps
64+
.when(() -> Operations.getExecutable(anyString(), anyString(), anyMap()))
65+
.thenReturn("npm");
5266
var envs = new JavaScriptNpmProvider(MANIFEST_PATH).getExecEnv();
5367
assertEquals(Collections.singletonMap("PATH", "test-node-home"), envs);
5468
}

0 commit comments

Comments
 (0)