Skip to content

Commit

Permalink
adding more tests around executor, view and validations
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinsudheendra committed Jun 6, 2014
1 parent 39e29c6 commit 58433db
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
Binary file added lib/mockito-all-1.9.5.jar
Binary file not shown.
11 changes: 7 additions & 4 deletions src/com/thoughtworks/go/task/rpmbuild/RPMBuildTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public ExecutionResult execute(TaskConfig taskConfig, TaskExecutionContext taskE
List<String> command = Arrays.asList("rpmbuild", "--target", targetArch, "-bb", "-v", "--clean", specFilePath);
try {
taskExecutionContext.console().printLine("[exec] " + StringUtils.join(command, " "));
ProcessBuilder builder = new ProcessBuilder(command).directory(new File(taskExecutionContext.workingDir()));
Process process = builder.start();
Process process = runProcess(taskExecutionContext, command);
taskExecutionContext.console().readOutputOf(process.getInputStream());
taskExecutionContext.console().readErrorOf(process.getErrorStream());
try {
Expand All @@ -65,14 +64,18 @@ public ExecutionResult execute(TaskConfig taskConfig, TaskExecutionContext taskE
return ExecutionResult.failure("[exec] FAILED with return code " + exitValue);
}
} catch (IOException e) {
taskExecutionContext.console().printLine("[exec] EXCEPTION with message " + e.getMessage());
throw new RuntimeException(e);
return ExecutionResult.failure("[exec] Exception: " + e.getMessage(), e);
}
return ExecutionResult.success(String.format("[exec] Successfully executed command [%s]", StringUtils.join(command, " ")));
}
};
}

Process runProcess(TaskExecutionContext taskExecutionContext, List<String> command) throws IOException {
ProcessBuilder builder = new ProcessBuilder(command).directory(new File(taskExecutionContext.workingDir()));
return builder.start();
}

@Override
public TaskView view() {
return new TaskView() {
Expand Down
99 changes: 98 additions & 1 deletion test/com/thoughtworks/go/task/rpmbuild/RPMBuildTaskTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,116 @@
package com.thoughtworks.go.task.rpmbuild;

import com.thoughtworks.go.plugin.api.response.execution.ExecutionResult;
import com.thoughtworks.go.plugin.api.response.validation.ValidationResult;
import com.thoughtworks.go.plugin.api.task.Console;
import com.thoughtworks.go.plugin.api.task.TaskConfig;
import com.thoughtworks.go.plugin.api.task.TaskExecutionContext;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.*;

public class RPMBuildTaskTest {

private RPMBuildTask task;

@Before
public void setUp() throws Exception {
task = new RPMBuildTask();
}

@Test
public void shouldAddDefaultConfigurationToTaskConfig() throws Exception {
RPMBuildTask task = new RPMBuildTask();
TaskConfig config = task.config();
assertThat(config.size(), is(2));
assertThat(config.getValue(RPMBuildTask.TARGET_ARCH), is("noarch"));
assertThat(config.getValue(RPMBuildTask.SPEC_FILE), is("package.spec"));
}

@Test
public void shouldExecuteRPMBuild() throws Exception {
TaskExecutionContext executor = mock(TaskExecutionContext.class);
Console console = mock(Console.class);
doNothing().when(console).printLine(anyString());
doNothing().when(console).readErrorOf(any(InputStream.class));
doNothing().when(console).readOutputOf(any(InputStream.class));
when(executor.console()).thenReturn(console);
RPMBuildTask spy = spy(task);
Process process = mock(Process.class);
when(process.waitFor()).thenReturn(0);
when(process.exitValue()).thenReturn(0);
doReturn(process).when(spy).runProcess(eq(executor), anyList());

ExecutionResult result = spy.executor().execute(task.config(), executor);

assertThat(result.isSuccessful(), is(true));
assertThat(result.getMessagesForDisplay(), is("[exec] Successfully executed command [rpmbuild --target noarch -bb -v --clean package.spec]"));
verify(process).waitFor();
verify(process).exitValue();
verify(process).getInputStream();
verify(process).getErrorStream();
verify(executor, times(3)).console();
verify(spy).runProcess(eq(executor), anyList());
}

@Test
public void shouldFailWhenExitValueIsNonZero() throws Exception {
TaskExecutionContext executor = mock(TaskExecutionContext.class);
Console console = mock(Console.class);
doNothing().when(console).printLine(anyString());
doNothing().when(console).readErrorOf(any(InputStream.class));
doNothing().when(console).readOutputOf(any(InputStream.class));
when(executor.console()).thenReturn(console);
RPMBuildTask spy = spy(task);
Process process = mock(Process.class);
when(process.waitFor()).thenReturn(0);
when(process.exitValue()).thenReturn(1);
doReturn(process).when(spy).runProcess(eq(executor), anyList());

ExecutionResult result = spy.executor().execute(task.config(), executor);

assertThat(result.isSuccessful(), is(false));
assertThat(result.getMessagesForDisplay(), is("[exec] FAILED with return code 1"));
verify(process).waitFor();
verify(process).exitValue();
verify(process).getInputStream();
verify(process).getErrorStream();
verify(executor, times(3)).console();
verify(spy).runProcess(eq(executor), anyList());
}

@Test
public void shouldFailWhenExceptionIsThrown() throws Exception {
TaskExecutionContext executor = mock(TaskExecutionContext.class);
Console console = mock(Console.class);
doNothing().when(console).printLine(anyString());
doNothing().when(console).readErrorOf(any(InputStream.class));
doNothing().when(console).readOutputOf(any(InputStream.class));
when(executor.console()).thenReturn(console);
RPMBuildTask spy = spy(task);
doThrow(new IOException("foo")).when(spy).runProcess(eq(executor), anyList());

ExecutionResult result = spy.executor().execute(task.config(), executor);

assertThat(result.isSuccessful(), is(false));
assertThat(result.getMessagesForDisplay(), is("[exec] Exception: foo"));
verify(executor).console();
verify(spy).runProcess(eq(executor), anyList());
}

@Test
public void shouldSetDisplayValue() throws Exception {
assertThat(task.view().displayValue(), is("RPM Build"));
}

@Test
public void shouldReturnEmptyValidation() throws Exception {
assertThat(task.validate(task.config()).isSuccessful(), is(true));
}
}

0 comments on commit 58433db

Please sign in to comment.