use of com.thoughtworks.go.util.command.CruiseControlException in project gocd by gocd.
the class BaseCommandBuilder method build.
public void build(BuildLogElement buildLogElement, DefaultGoPublisher publisher, EnvironmentVariableContext environmentVariableContext, TaskExtension taskExtension) throws CruiseControlException {
final long startTime = System.currentTimeMillis();
if (!workingDir.isDirectory()) {
String message = "Working directory \"" + workingDir.getAbsolutePath() + "\" is not a directory!";
publisher.consumeLine(message);
setBuildError(buildLogElement, message);
throw new CruiseControlException(message);
}
ExecScript execScript = new ExecScript(errorString);
CommandLine commandLine = buildCommandLine();
// mimic Ant target/task logging
buildLogElement.setBuildLogHeader(command);
//TODO: Clean up this code and re-use the CommandLine code
try {
CompositeConsumer consumer = new CompositeConsumer(publisher, execScript);
commandLine.runScript(execScript, consumer, environmentVariableContext, null);
setBuildDuration(startTime, buildLogElement);
if (execScript.foundError()) {
// detected the error string in the command output
String message = "Build failed. Command " + this.command + " reported [" + errorString + "].";
setBuildError(buildLogElement, message);
buildLogElement.setTaskError();
throw new CruiseControlException(message);
} else if (execScript.getExitCode() != 0) {
String message = "return code is " + execScript.getExitCode();
setBuildError(buildLogElement, message);
throw new CruiseControlException(message);
}
} catch (CheckedCommandLineException ex) {
setBuildError(buildLogElement, "exec error");
setTaskError(buildLogElement, "Could not execute command: " + commandLine.toStringForDisplay());
throw ex;
}
}
use of com.thoughtworks.go.util.command.CruiseControlException in project gocd by gocd.
the class PluggableTaskBuilderTest method shouldPublishErrorMessageIfPluginThrowsAnException.
@Test
public void shouldPublishErrorMessageIfPluginThrowsAnException() throws CruiseControlException {
PluggableTask task = mock(PluggableTask.class);
PluggableTaskBuilder taskBuilder = new PluggableTaskBuilder(runIfConfigs, cancelBuilder, pluggableTask, TEST_PLUGIN_ID, "test-directory") {
@Override
protected ExecutionResult executeTask(Task task, DefaultGoPublisher publisher, EnvironmentVariableContext environmentVariableContext, String consoleLogCharset) {
throw new RuntimeException("err");
}
};
try {
taskBuilder.build(goPublisher, variableContext, taskExtension, null, null, "utf-8");
fail("expected exception to be thrown");
} catch (Exception e) {
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(goPublisher).taggedConsumeLine(eq(DefaultGoPublisher.ERR), captor.capture());
String error = "Error: err";
assertThat(captor.getValue(), is(error));
assertThat(e.getMessage(), is(new RuntimeException("err").toString()));
}
}
use of com.thoughtworks.go.util.command.CruiseControlException in project gocd by gocd.
the class PluggableTaskBuilderTest method shouldPublishErrorMessageIfPluginReturnsAFailureResponse.
@Test
public void shouldPublishErrorMessageIfPluginReturnsAFailureResponse() throws CruiseControlException {
PluggableTask task = mock(PluggableTask.class);
PluggableTaskBuilder taskBuilder = new PluggableTaskBuilder(runIfConfigs, cancelBuilder, pluggableTask, TEST_PLUGIN_ID, "test-directory") {
@Override
protected ExecutionResult executeTask(Task task, DefaultGoPublisher publisher, EnvironmentVariableContext environmentVariableContext, String consoleLogCharset) {
return ExecutionResult.failure("err");
}
};
try {
taskBuilder.build(goPublisher, variableContext, taskExtension, null, null, "utf-8");
fail("expected exception to be thrown");
} catch (Exception e) {
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(goPublisher).taggedConsumeLine(eq(DefaultGoPublisher.ERR), captor.capture());
assertThat(captor.getValue(), is("err"));
assertThat(e.getMessage(), is("err"));
}
}
use of com.thoughtworks.go.util.command.CruiseControlException in project gocd by gocd.
the class AntTaskBuilderTest method shouldFailWhenTargetDoesNotExist.
@Test
void shouldFailWhenTargetDoesNotExist() {
String target = "not-exist-target";
String buildXml = "./build.xml";
antTask.setBuildFile(buildXml);
antTask.setTarget(target);
Builder builder = antTaskBuilder.createBuilder(builderFactory, antTask, ExecTaskBuilderTest.pipelineStub(PIPELINE_LABEL, "."), resolver);
try {
builder.build(new StubGoPublisher(), new EnvironmentVariableContext(), taskEntension, null, null, "utf-8");
} catch (CruiseControlException e) {
assertThat(e.getMessage()).contains("Build failed. Command ant reported [BUILD FAILED].");
}
}
Aggregations