Search in sources :

Example 1 with CruiseControlException

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;
    }
}
Also used : CompositeConsumer(com.thoughtworks.go.util.command.CompositeConsumer) CheckedCommandLineException(com.thoughtworks.go.util.command.CheckedCommandLineException) CommandLine(com.thoughtworks.go.util.command.CommandLine) ExecScript(com.thoughtworks.go.util.command.ExecScript) CruiseControlException(com.thoughtworks.go.util.command.CruiseControlException)

Example 2 with CruiseControlException

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()));
    }
}
Also used : PluggableTask(com.thoughtworks.go.config.pluggabletask.PluggableTask) ArgumentCaptor(org.mockito.ArgumentCaptor) DefaultGoPublisher(com.thoughtworks.go.work.DefaultGoPublisher) EnvironmentVariableContext(com.thoughtworks.go.util.command.EnvironmentVariableContext) PluggableTask(com.thoughtworks.go.config.pluggabletask.PluggableTask) CruiseControlException(com.thoughtworks.go.util.command.CruiseControlException) Test(org.junit.jupiter.api.Test)

Example 3 with CruiseControlException

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"));
    }
}
Also used : PluggableTask(com.thoughtworks.go.config.pluggabletask.PluggableTask) ArgumentCaptor(org.mockito.ArgumentCaptor) DefaultGoPublisher(com.thoughtworks.go.work.DefaultGoPublisher) EnvironmentVariableContext(com.thoughtworks.go.util.command.EnvironmentVariableContext) PluggableTask(com.thoughtworks.go.config.pluggabletask.PluggableTask) CruiseControlException(com.thoughtworks.go.util.command.CruiseControlException) Test(org.junit.jupiter.api.Test)

Example 4 with CruiseControlException

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].");
    }
}
Also used : StubGoPublisher(com.thoughtworks.go.domain.StubGoPublisher) CruiseControlException(com.thoughtworks.go.util.command.CruiseControlException) CommandBuilder(com.thoughtworks.go.domain.builder.CommandBuilder) Builder(com.thoughtworks.go.domain.builder.Builder) EnvironmentVariableContext(com.thoughtworks.go.util.command.EnvironmentVariableContext) Test(org.junit.jupiter.api.Test)

Aggregations

CruiseControlException (com.thoughtworks.go.util.command.CruiseControlException)4 EnvironmentVariableContext (com.thoughtworks.go.util.command.EnvironmentVariableContext)3 Test (org.junit.jupiter.api.Test)3 PluggableTask (com.thoughtworks.go.config.pluggabletask.PluggableTask)2 DefaultGoPublisher (com.thoughtworks.go.work.DefaultGoPublisher)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 StubGoPublisher (com.thoughtworks.go.domain.StubGoPublisher)1 Builder (com.thoughtworks.go.domain.builder.Builder)1 CommandBuilder (com.thoughtworks.go.domain.builder.CommandBuilder)1 CheckedCommandLineException (com.thoughtworks.go.util.command.CheckedCommandLineException)1 CommandLine (com.thoughtworks.go.util.command.CommandLine)1 CompositeConsumer (com.thoughtworks.go.util.command.CompositeConsumer)1 ExecScript (com.thoughtworks.go.util.command.ExecScript)1