Ensured that exception is thrown when process returns with invalid code

This commit is contained in:
Marcin Grzejszczak
2017-03-10 13:30:49 +01:00
parent 640c0edf57
commit a67c4ba70d
2 changed files with 52 additions and 1 deletions

View File

@@ -109,19 +109,26 @@ class ProcessExecutor {
String workingDir = this.properties.getWorkingDir();
log.info("Will run the build via {} and wait for result for [{}] minutes", commands, waitTimeInMinutes);
ProcessBuilder builder = builder(commands, workingDir);
Process process = builder.start();
Process process = startProcess(builder);
boolean finished = process.waitFor(waitTimeInMinutes, TimeUnit.MINUTES);
if (!finished) {
log.error("The build hasn't managed to finish in [{}] minutes", waitTimeInMinutes);
process.destroyForcibly();
throw new IllegalStateException("Process waiting time of [" + waitTimeInMinutes + "] minutes exceeded");
}
if (process.exitValue() != 0) {
throw new IllegalStateException("The process has exited with exit code [" + process.exitValue() + "]");
}
}
catch (InterruptedException | IOException e) {
throw new IllegalStateException(e);
}
}
Process startProcess(ProcessBuilder builder) throws IOException {
return builder.start();
}
ProcessBuilder builder(String[] commands, String workingDir) {
return new ProcessBuilder(commands)
.directory(new File(workingDir))

View File

@@ -2,6 +2,8 @@ package org.springframework.cloud.release.internal.project;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
@@ -124,6 +126,20 @@ public class ProjectTests {
thenThrownBy(() -> builder.publishDocs("")).hasMessageContaining("Process waiting time of [0] minutes exceeded");
}
@Test
public void should_throw_exception_when_process_exits_with_invalid_code() throws Exception {
ReleaserProperties properties = new ReleaserProperties();
properties.getMaven().setBuildCommand("exit 1");
properties.setWorkingDir(file("/projects/builder/unresolved").getPath());
Project builder = new Project(properties, new ProcessExecutor(properties) {
@Override Process startProcess(ProcessBuilder builder) throws IOException {
return processWithInvalidExitCode();
}
});
thenThrownBy(builder::build).hasMessageContaining("The process has exited with exit code [1]");
}
@Test
public void should_successfully_execute_a_bump_versions_command() throws Exception {
ReleaserProperties properties = new ReleaserProperties();
@@ -140,6 +156,34 @@ public class ProjectTests {
then(this.reader.readPom(converters).getParent().getVersion()).isEqualTo("2.3.4.BUILD-SNAPSHOT");
}
private Process processWithInvalidExitCode() {
return new Process() {
@Override public OutputStream getOutputStream() {
return null;
}
@Override public InputStream getInputStream() {
return null;
}
@Override public InputStream getErrorStream() {
return null;
}
@Override public int waitFor() throws InterruptedException {
return 0;
}
@Override public int exitValue() {
return 1;
}
@Override public void destroy() {
}
};
}
private TestProcessExecutor executor(ReleaserProperties properties) {
return new TestProcessExecutor(properties);
}