Skip to content

Commit 6a0186f

Browse files
committed
chore: address PR #91 review comments
- Use StandardCharsets.UTF_8 in InputStreamReader for consistent decoding - Extract shellCommand() helper in tests to remove OS-detection duplication
1 parent 2ae40cd commit 6a0186f

File tree

2 files changed

+13
-21
lines changed

2 files changed

+13
-21
lines changed

capabilities-common/src/main/java/ai/wanaku/capabilities/sdk/common/ProcessRunner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.File;
55
import java.io.IOException;
66
import java.io.InputStreamReader;
7+
import java.nio.charset.StandardCharsets;
78
import java.util.Map;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
@@ -42,7 +43,8 @@ public static String runWithOutput(String... command) {
4243
}
4344

4445
private static String readOutput(Process process) throws IOException {
45-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
46+
try (BufferedReader reader =
47+
new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
4648
StringBuilder output = new StringBuilder();
4749
String line;
4850
while ((line = reader.readLine()) != null) {

capabilities-common/src/test/java/ai/wanaku/capabilities/sdk/common/ProcessRunnerTest.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@
88

99
public class ProcessRunnerTest {
1010

11-
private static boolean isWindows() {
12-
return System.getProperty("os.name").toLowerCase().contains("win");
11+
private static String[] shellCommand(String script) {
12+
if (System.getProperty("os.name").toLowerCase().contains("win")) {
13+
return new String[] {"cmd.exe", "/c", script};
14+
} else {
15+
return new String[] {"sh", "-c", script};
16+
}
1317
}
1418

1519
@Test
1620
void runWithOutput_returnsCommandOutput() {
17-
String output;
18-
if (isWindows()) {
19-
output = ProcessRunner.runWithOutput("cmd.exe", "/c", "echo hello");
20-
} else {
21-
output = ProcessRunner.runWithOutput("sh", "-c", "echo hello");
22-
}
21+
String output = ProcessRunner.runWithOutput(shellCommand("echo hello"));
2322

2423
assertNotNull(output, "Output should not be null");
2524
assertFalse(output.isEmpty(), "Output should not be empty");
@@ -28,25 +27,16 @@ void runWithOutput_returnsCommandOutput() {
2827

2928
@Test
3029
void runWithOutput_nonZeroExitDoesNotThrow() {
31-
String output;
32-
if (isWindows()) {
33-
output = assertDoesNotThrow(() -> ProcessRunner.runWithOutput("cmd.exe", "/c", "echo output && exit /b 1"));
34-
} else {
35-
output = assertDoesNotThrow(() -> ProcessRunner.runWithOutput("sh", "-c", "echo output; exit 1"));
36-
}
30+
String[] command = shellCommand("echo output; exit 1");
31+
String output = assertDoesNotThrow(() -> ProcessRunner.runWithOutput(command));
3732

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

4237
@Test
4338
void runWithOutput_capturesStderrViaMergedStream() {
44-
String output;
45-
if (isWindows()) {
46-
output = ProcessRunner.runWithOutput("cmd.exe", "/c", "echo stdout && echo stderr 1>&2");
47-
} else {
48-
output = ProcessRunner.runWithOutput("sh", "-c", "echo stdout && echo stderr 1>&2");
49-
}
39+
String output = ProcessRunner.runWithOutput(shellCommand("echo stdout && echo stderr 1>&2"));
5040

5141
assertNotNull(output, "Output should not be null");
5242
assertFalse(output.isEmpty(), "Output should not be empty");

0 commit comments

Comments
 (0)