Search in sources :

Example 1 with CommandLine

use of com.thoughtworks.go.util.command.CommandLine in project gocd by gocd.

the class ProcessWrapperTest method shouldBeAbleToCompleteInput.

@Test
public void shouldBeAbleToCompleteInput() throws Exception {
    String input1 = "SYSIN: Line 1!";
    String input2 = "SYSIN: Line 2!";
    CommandLine line = CommandLine.createCommandLine("ruby").withArgs(script("echo-all-input"));
    ConsoleResult result = run(line, input1, input2);
    assertThat(result.returnValue(), is(0));
    assertThat(result.output(), contains("You said: " + input1));
    assertThat(result.output(), contains("You said: " + input2));
    assertThat(result.error().size(), is(0));
}
Also used : CommandLine(com.thoughtworks.go.util.command.CommandLine) ConsoleResult(com.thoughtworks.go.util.command.ConsoleResult) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with CommandLine

use of com.thoughtworks.go.util.command.CommandLine in project gocd by gocd.

the class ProcessWrapperTest method shouldSetGoServerVariablesIfTheyExist.

@Test
public void shouldSetGoServerVariablesIfTheyExist() {
    System.setProperty("GO_DEPENDENCY_LABEL_PIPELINE_NAME", "999");
    CommandLine line = CommandLine.createCommandLine("ruby").withArgs(script("dump-environment"));
    ConsoleResult result = run(line);
    assertThat("Errors: " + result.errorAsString(), result.returnValue(), is(0));
    assertThat(result.output(), contains("GO_DEPENDENCY_LABEL_PIPELINE_NAME=999"));
}
Also used : CommandLine(com.thoughtworks.go.util.command.CommandLine) ConsoleResult(com.thoughtworks.go.util.command.ConsoleResult) Test(org.junit.Test)

Example 3 with CommandLine

use of com.thoughtworks.go.util.command.CommandLine in project gocd by gocd.

the class ProcessWrapperTest method shouldThrowExceptionWhenExecutableDoesNotExist.

@Test
public void shouldThrowExceptionWhenExecutableDoesNotExist() throws IOException {
    CommandLine line = CommandLine.createCommandLine("doesnotexist");
    try {
        ProcessOutputStreamConsumer outputStreamConsumer = inMemoryConsumer();
        line.execute(outputStreamConsumer, new EnvironmentVariableContext(), null);
        fail("Expected exception");
    } catch (CommandLineException e) {
        assertThat(e.getMessage(), containsString("Make sure this command can execute manually."));
        assertThat(e.getMessage(), containsString("doesnotexist"));
        assertThat(e.getResult(), notNullValue());
    }
}
Also used : CommandLine(com.thoughtworks.go.util.command.CommandLine) EnvironmentVariableContext(com.thoughtworks.go.util.command.EnvironmentVariableContext) ProcessOutputStreamConsumer(com.thoughtworks.go.util.command.ProcessOutputStreamConsumer) CommandLineException(com.thoughtworks.go.util.command.CommandLineException) Test(org.junit.Test)

Example 4 with CommandLine

use of com.thoughtworks.go.util.command.CommandLine 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 5 with CommandLine

use of com.thoughtworks.go.util.command.CommandLine in project gocd by gocd.

the class CommandBuilder method buildCommandLine.

protected CommandLine buildCommandLine() {
    CommandLine command = null;
    if (SystemUtil.isWindows()) {
        command = CommandLine.createCommandLine("cmd").withWorkingDir(workingDir);
        command.withArg("/c");
        command.withArg(translateToWindowsPath(this.command));
    } else {
        command = CommandLine.createCommandLine(this.command).withWorkingDir(workingDir);
    }
    String[] argsArray = CommandLine.translateCommandLine(args);
    for (int i = 0; i < argsArray.length; i++) {
        String arg = argsArray[i];
        command.withArg(arg);
    }
    return command;
}
Also used : CommandLine(com.thoughtworks.go.util.command.CommandLine)

Aggregations

CommandLine (com.thoughtworks.go.util.command.CommandLine)30 Test (org.junit.Test)14 ConsoleResult (com.thoughtworks.go.util.command.ConsoleResult)6 InMemoryStreamConsumer (com.thoughtworks.go.util.command.InMemoryStreamConsumer)5 File (java.io.File)5 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)3 CommandLineException (com.thoughtworks.go.util.command.CommandLineException)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 RunIf (com.googlecode.junit.ext.RunIf)2 CommandLine.createCommandLine (com.thoughtworks.go.util.command.CommandLine.createCommandLine)2 EnvironmentVariableContext (com.thoughtworks.go.util.command.EnvironmentVariableContext)2 ProcessOutputStreamConsumer (com.thoughtworks.go.util.command.ProcessOutputStreamConsumer)2 LinkedList (java.util.LinkedList)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 CheckedCommandLineException (com.thoughtworks.go.util.command.CheckedCommandLineException)1 CompositeConsumer (com.thoughtworks.go.util.command.CompositeConsumer)1 CruiseControlException (com.thoughtworks.go.util.command.CruiseControlException)1 ExecScript (com.thoughtworks.go.util.command.ExecScript)1 PasswordArgument (com.thoughtworks.go.util.command.PasswordArgument)1