Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -21,9 +21,8 @@ private ProcessRunner() {}
public static String runWithOutput(String... command) {
try {
ProcessBuilder processBuilder = new ProcessBuilder(command);
// Redirect output and error streams to a pipe
processBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE);
processBuilder.redirectError(ProcessBuilder.Redirect.PIPE);
processBuilder.redirectErrorStream(true);

final Process process = processBuilder.start();
LOG.info("Waiting for process to finish...");
Expand All @@ -36,20 +35,21 @@ public static String runWithOutput(String... command) {
LOG.error("I/O Error: {}", e.getMessage(), e);
throw new WanakuException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error("Interrupted: {}", e.getMessage(), e);
throw new WanakuException(e);
}
}

private static String readOutput(Process process) throws IOException {
// Read the output from the process
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
return output.toString();
}
return output.toString();
}

public static void run(File directory, String... command) {
Expand All @@ -74,6 +74,7 @@ public static void run(File directory, Map<String, String> environmentVariables,
LOG.error("I/O Error: {}", e.getMessage(), e);
throw new WanakuException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.error("Interrupted: {}", e.getMessage(), e);
throw new WanakuException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
package ai.wanaku.capabilities.sdk.common;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ProcessRunnerTest {

private static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}

@Test
void runWithOutput_returnsCommandOutput() {
String output = ProcessRunner.runWithOutput("echo", "hello");
String output;
if (isWindows()) {
output = ProcessRunner.runWithOutput("cmd.exe", "/c", "echo hello");
} else {
output = ProcessRunner.runWithOutput("sh", "-c", "echo hello");
}

assertNotNull(output, "Output should not be null");
assertFalse(output.isEmpty(), "Output should not be empty");
assertTrue(output.contains("hello"), "Output should contain 'hello'");
}

@Test
void runWithOutput_nonZeroExitDoesNotThrow() {
String output;
if (isWindows()) {
output = assertDoesNotThrow(() -> ProcessRunner.runWithOutput("cmd.exe", "/c", "echo output && exit /b 1"));
} else {
output = assertDoesNotThrow(() -> ProcessRunner.runWithOutput("sh", "-c", "echo output; exit 1"));
}

assertNotNull(output, "Output should not be null");
assertTrue(output.contains("output"), "Output should contain 'output'");
}

@Test
void runWithOutput_capturesStderrViaMergedStream() {
String output;
if (isWindows()) {
output = ProcessRunner.runWithOutput("cmd.exe", "/c", "echo stdout && echo stderr 1>&2");
} else {
output = ProcessRunner.runWithOutput("sh", "-c", "echo stdout && echo stderr 1>&2");
}

assertNotNull(output, "Output should not be null");
assertFalse(output.isEmpty(), "Output should not be empty");
assertTrue(output.contains("stdout"), "Output should contain stdout content");
assertTrue(output.contains("stderr"), "Output should contain stderr content (merged stream)");
}
}
Loading