Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[executeCommandLine] should return STDERR if STDOUT is empty #2114

Merged
merged 4 commits into from
Jan 16, 2021
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 @@ -78,9 +78,8 @@ public static void executeCommandLine(String... commandLine) {

Process processTemp = null;
Future<String> outputFuture = null;
Future<String> errorFuture = null;
cleanup: try {
Process process = processTemp = new ProcessBuilder(commandLine).start();
Process process = processTemp = new ProcessBuilder(commandLine).redirectErrorStream(true).start();

outputFuture = executor.submit(() -> {
try (InputStream inputStream = process.getInputStream();
Expand All @@ -91,32 +90,13 @@ public static void executeCommandLine(String... commandLine) {
}
});

errorFuture = executor.submit(() -> {
try (InputStream inputStream = process.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
StringWriter output = new StringWriter();
reader.transferTo(output);
return output.toString();
}
});
int exitCode;
if (timeout == null) {
exitCode = process.waitFor();
} else if (process.waitFor(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
exitCode = process.exitValue();
} else {
process.waitFor();
} else if (!process.waitFor(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
logger.warn("Timeout occurred when executing commandLine '{}'", Arrays.toString(commandLine));
break cleanup;
}
if (exitCode == 0) {
return outputFuture.get();
} else {
if (logger.isDebugEnabled()) {
logger.debug("exit code '{}', result '{}', errors '{}'", exitCode, outputFuture.get(),
errorFuture.get());
}
return errorFuture.get();
}
return outputFuture.get();
} catch (ExecutionException e) {
if (logger.isDebugEnabled()) {
logger.warn("Error occurred when executing commandLine '{}'", Arrays.toString(commandLine),
Expand All @@ -139,9 +119,6 @@ public static void executeCommandLine(String... commandLine) {
if (outputFuture != null) {
outputFuture.cancel(true);
}
if (errorFuture != null) {
errorFuture.cancel(true);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ private boolean isWindowsSystem() {
String osName = System.getProperty("os.name").toLowerCase();
return osName.indexOf("windows") >= 0;
}

@Test
public void testExecuteCommandLineAndWaitStdErrRedirection() {
final String result;
if (isWindowsSystem()) {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "cmd", "/c", "dir", "xxx.xxx",
"1>", "nul");
} else {
result = ExecUtil.executeCommandLineAndWaitResponse(Duration.ofSeconds(1), "ls", "xxx.xxx");
}
assertNotNull(result);
assertNotEquals("", result);
}
}