fix(CommandExecutionService): preserve stdout and stderr outputs

This commit is contained in:
2026-04-20 14:32:25 +08:00
parent eea72c747c
commit 2ec2d8e096
2 changed files with 33 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ import java.io.InputStreamReader;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@@ -37,8 +38,8 @@ public class CommandExecutionService {
public Result exec(WrappedLaunchSpec launchSpec) { public Result exec(WrappedLaunchSpec launchSpec) {
Result result = new Result(); Result result = new Result();
List<String> output = new ArrayList<>(); List<String> output = Collections.synchronizedList(new ArrayList<>());
List<String> error = new ArrayList<>(); List<String> error = Collections.synchronizedList(new ArrayList<>());
try { try {
Process process = startProcess(launchSpec); Process process = startProcess(launchSpec);
@@ -68,11 +69,18 @@ public class CommandExecutionService {
stderrThread.join(); stderrThread.join();
result.setOk(exitCode == 0); result.setOk(exitCode == 0);
result.setResultList(output.isEmpty() ? error : output); List<String> stdoutLines = List.copyOf(output);
result.setTotal(String.join("\n", output.isEmpty() ? error : output)); List<String> stderrLines = List.copyOf(error);
result.setStdoutLines(stdoutLines);
result.setStderrLines(stderrLines);
result.setResultList(stdoutLines.isEmpty() ? stderrLines : stdoutLines);
result.setTotal(buildDisplayText(stdoutLines, stderrLines));
} catch (Exception e) { } catch (Exception e) {
result.setOk(false); result.setOk(false);
result.setTotal(e.getMessage()); result.setTotal(e.getMessage());
result.setStdoutLines(List.of());
result.setStderrLines(List.of(e.getMessage()));
result.setResultList(result.getStderrLines());
} }
return result; return result;
@@ -149,11 +157,23 @@ public class CommandExecutionService {
); );
} }
private String buildDisplayText(List<String> stdoutLines, List<String> stderrLines) {
if (stdoutLines.isEmpty()) {
return String.join("\n", stderrLines);
}
if (stderrLines.isEmpty()) {
return String.join("\n", stdoutLines);
}
return String.join("\n", stdoutLines) + "\n" + String.join("\n", stderrLines);
}
@Data @Data
public static class Result { public static class Result {
private boolean ok; private boolean ok;
private String total; private String total;
private List<String> resultList; private List<String> resultList;
private List<String> stdoutLines;
private List<String> stderrLines;
} }
@Data @Data

View File

@@ -56,6 +56,8 @@ class CommandExecutionServiceTest {
Assertions.assertTrue(result.isOk()); Assertions.assertTrue(result.isOk());
Assertions.assertEquals(List.of("hello", "world"), result.getResultList()); Assertions.assertEquals(List.of("hello", "world"), result.getResultList());
Assertions.assertEquals(List.of("hello", "world"), result.getStdoutLines());
Assertions.assertEquals(List.of(), result.getStderrLines());
Assertions.assertEquals("hello\nworld", result.getTotal()); Assertions.assertEquals("hello\nworld", result.getTotal());
} }
@@ -67,6 +69,8 @@ class CommandExecutionServiceTest {
Assertions.assertTrue(result.isOk()); Assertions.assertTrue(result.isOk());
Assertions.assertEquals(List.of("ok"), result.getResultList()); Assertions.assertEquals(List.of("ok"), result.getResultList());
Assertions.assertEquals(List.of("ok"), result.getStdoutLines());
Assertions.assertEquals(List.of(), result.getStderrLines());
Assertions.assertEquals("ok", result.getTotal()); Assertions.assertEquals("ok", result.getTotal());
} }
@@ -78,6 +82,8 @@ class CommandExecutionServiceTest {
Assertions.assertFalse(result.isOk()); Assertions.assertFalse(result.isOk());
Assertions.assertEquals(List.of("fail"), result.getResultList()); Assertions.assertEquals(List.of("fail"), result.getResultList());
Assertions.assertEquals(List.of(), result.getStdoutLines());
Assertions.assertEquals(List.of("fail"), result.getStderrLines());
Assertions.assertEquals("fail", result.getTotal()); Assertions.assertEquals("fail", result.getTotal());
} }
@@ -89,7 +95,9 @@ class CommandExecutionServiceTest {
Assertions.assertTrue(result.isOk()); Assertions.assertTrue(result.isOk());
Assertions.assertEquals(List.of("out"), result.getResultList()); Assertions.assertEquals(List.of("out"), result.getResultList());
Assertions.assertEquals("out", result.getTotal()); Assertions.assertEquals(List.of("out"), result.getStdoutLines());
Assertions.assertEquals(List.of("err"), result.getStderrLines());
Assertions.assertEquals("out\nerr", result.getTotal());
} }
@Test @Test