Search in sources :

Example 6 with CommandLine

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

the class GarageServiceTest method shouldCreateGitCommand.

@Test
public void shouldCreateGitCommand() throws Exception {
    CommandLine git = garageService.getGit();
    assertThat(git.getExecutable(), is("git"));
}
Also used : CommandLine(com.thoughtworks.go.util.command.CommandLine) Test(org.junit.Test)

Example 7 with CommandLine

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

the class GarageServiceTest method shouldFailWhenGitIsNotFoundInPath.

@Test
public void shouldFailWhenGitIsNotFoundInPath() throws Exception {
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    CommandLine commandLine = mock(CommandLine.class);
    GarageService spy = spy(garageService);
    doReturn(commandLine).when(spy).getGit();
    doReturn(commandLine).when(commandLine).withArg(anyString());
    doReturn(commandLine).when(commandLine).withWorkingDir(any(File.class));
    when(commandLine.run(any(InMemoryStreamConsumer.class), eq(GarageService.PROCESS_TAG))).thenThrow(new CommandLineException("failed to execute git"));
    spy.gc(result);
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.hasMessage(), is(true));
    verify(spy, times(1)).getGit();
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) CommandLine(com.thoughtworks.go.util.command.CommandLine) InMemoryStreamConsumer(com.thoughtworks.go.util.command.InMemoryStreamConsumer) File(java.io.File) CommandLineException(com.thoughtworks.go.util.command.CommandLineException) Test(org.junit.Test)

Example 8 with CommandLine

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

the class GarageServiceTest method shouldFailWhenGitExecutionFailsWithANonZeroReturnCode.

@Test
public void shouldFailWhenGitExecutionFailsWithANonZeroReturnCode() throws Exception {
    CommandLine firstCommand = mock(CommandLine.class);
    doReturn(firstCommand).when(firstCommand).withArg(anyString());
    doReturn(firstCommand).when(firstCommand).withWorkingDir(any(File.class));
    when(firstCommand.run(any(InMemoryStreamConsumer.class), eq(GarageService.PROCESS_TAG))).thenReturn(0);
    CommandLine secondCommand = mock(CommandLine.class);
    doReturn(secondCommand).when(secondCommand).withArg(anyString());
    doReturn(secondCommand).when(secondCommand).withWorkingDir(any(File.class));
    when(secondCommand.run(any(InMemoryStreamConsumer.class), eq(GarageService.PROCESS_TAG))).thenReturn(129);
    final List<CommandLine> commands = new LinkedList<>();
    commands.add(firstCommand);
    commands.add(secondCommand);
    HttpLocalizedOperationResult result = new HttpLocalizedOperationResult();
    GarageService spy = spy(garageService);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            CommandLine remove = commands.get(0);
            commands.remove(remove);
            return remove;
        }
    }).when(spy).getGit();
    spy.gc(result);
    assertThat(result.isSuccessful(), is(false));
    assertThat(result.hasMessage(), is(true));
    verify(spy, times(2)).getGit();
    verify(firstCommand).run(any(InMemoryStreamConsumer.class), eq(GarageService.PROCESS_TAG));
    verify(secondCommand).run(any(InMemoryStreamConsumer.class), eq(GarageService.PROCESS_TAG));
}
Also used : HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Answer(org.mockito.stubbing.Answer) CommandLine(com.thoughtworks.go.util.command.CommandLine) InvocationOnMock(org.mockito.invocation.InvocationOnMock) InMemoryStreamConsumer(com.thoughtworks.go.util.command.InMemoryStreamConsumer) File(java.io.File) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 9 with CommandLine

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

the class ExecCommandExecutor method execute.

@Override
public boolean execute(BuildCommand command, BuildSession buildSession) {
    File workingDir = buildSession.resolveRelativeDir(command.getWorkingDirectory());
    if (!workingDir.isDirectory()) {
        String message = "Working directory \"" + workingDir.getAbsolutePath() + "\" is not a directory!";
        LOG.error(message);
        buildSession.println(message);
        return false;
    }
    String cmd = command.getStringArg("command");
    String[] args = command.getArrayArg("args");
    Map<String, String> secrets = buildSession.getSecretSubstitutions();
    Set<String> leftSecrets = new HashSet<>(secrets.keySet());
    CommandLine commandLine = createCommandLine(cmd, buildSession.getConsoleLogCharset());
    for (String arg : args) {
        if (secrets.containsKey(arg)) {
            leftSecrets.remove(arg);
            commandLine.withArg(new SubstitutableCommandArgument(arg, secrets.get(arg)));
        } else {
            commandLine.withArg(arg);
        }
    }
    for (String secret : leftSecrets) {
        commandLine.withNonArgSecret(new SecretSubstitution(secret, secrets.get(secret)));
    }
    commandLine.withWorkingDir(workingDir);
    commandLine.withEnv(buildSession.getEnvs());
    commandLine.withEnv(command.getCommandEnvVars());
    return executeCommandLine(buildSession, commandLine, command.getExecInput()) == 0;
}
Also used : CommandLine(com.thoughtworks.go.util.command.CommandLine) File(java.io.File) HashSet(java.util.HashSet)

Example 10 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 (SystemUtils.IS_OS_WINDOWS) {
        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)31 Test (org.junit.Test)16 InMemoryStreamConsumer (com.thoughtworks.go.util.command.InMemoryStreamConsumer)6 ConsoleResult (com.thoughtworks.go.util.command.ConsoleResult)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 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 UrlArgument (com.thoughtworks.go.util.command.UrlArgument)1 IOException (java.io.IOException)1