Fix broken HgVersionCommand

Use waitFor instead of exitValue to wait for process to finish
This commit is contained in:
Sebastian Sdorra
2020-11-27 10:46:37 +01:00
parent 8ee8c8b351
commit 136cdbb6b0
2 changed files with 15 additions and 9 deletions

View File

@@ -78,6 +78,9 @@ public class HgVersionCommand {
}
} catch (IOException ex) {
LOG.warn("failed to get python version", ex);
} catch (InterruptedException ex) {
LOG.warn("failed to get python version", ex);
Thread.currentThread().interrupt();
}
return HgVersion.UNKNOWN;
}
@@ -88,19 +91,22 @@ public class HgVersionCommand {
return exec(config.getHgBinary(), HG_ARGS).trim();
} catch (IOException ex) {
LOG.warn("failed to get mercurial version", ex);
return HgVersion.UNKNOWN;
} catch (InterruptedException ex) {
LOG.warn("failed to get mercurial version", ex);
Thread.currentThread().interrupt();
}
return HgVersion.UNKNOWN;
}
@SuppressWarnings("UnstableApiUsage")
private String exec(String command, String[] args) throws IOException {
private String exec(String command, String[] args) throws IOException, InterruptedException {
List<String> cmd = new ArrayList<>();
cmd.add(command);
cmd.addAll(Arrays.asList(args));
Process process = executor.execute(cmd);
byte[] bytes = ByteStreams.toByteArray(process.getInputStream());
int exitCode = process.exitValue();
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new IOException("process ends with exit code " + exitCode);
}

View File

@@ -62,7 +62,7 @@ class HgVersionCommandTest {
}
@Test
void shouldReturnHgVersion() {
void shouldReturnHgVersion() throws InterruptedException {
command("/usr/local/bin/hg", HgVersionCommand.HG_ARGS, "5.5.2", 0);
command("/opt/python/bin/python", HgVersionCommand.PYTHON_ARGS, PYTHON_OUTPUT, 0);
@@ -72,7 +72,7 @@ class HgVersionCommandTest {
}
@Test
void shouldReturnUnknownMercurialVersionOnNonZeroExitCode() {
void shouldReturnUnknownMercurialVersionOnNonZeroExitCode() throws InterruptedException {
command("hg", HgVersionCommand.HG_ARGS, "", 1);
command("python", HgVersionCommand.PYTHON_ARGS, PYTHON_OUTPUT, 0);
@@ -82,7 +82,7 @@ class HgVersionCommandTest {
}
@Test
void shouldReturnUnknownPythonVersionOnNonZeroExitCode() {
void shouldReturnUnknownPythonVersionOnNonZeroExitCode() throws InterruptedException {
command("hg", HgVersionCommand.HG_ARGS, "4.4.2", 0);
command("python", HgVersionCommand.PYTHON_ARGS, "", 1);
@@ -92,7 +92,7 @@ class HgVersionCommandTest {
}
@Test
void shouldReturnUnknownForInvalidPythonOutput() {
void shouldReturnUnknownForInvalidPythonOutput() throws InterruptedException {
command("hg", HgVersionCommand.HG_ARGS, "1.0.0", 0);
command("python", HgVersionCommand.PYTHON_ARGS, "abcdef", 0);
@@ -112,10 +112,10 @@ class HgVersionCommandTest {
assertThat(hgVersion.getPython()).isEqualTo(HgVersion.UNKNOWN);
}
private Process command(String command, String[] args, String content, int exitValue) {
private Process command(String command, String[] args, String content, int exitValue) throws InterruptedException {
Process process = mock(Process.class);
when(process.getInputStream()).thenReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
when(process.exitValue()).thenReturn(exitValue);
when(process.waitFor()).thenReturn(exitValue);
List<String> cmdLine = new ArrayList<>();
cmdLine.add(command);