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"));
}
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();
}
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));
}
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;
}
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;
}
Aggregations