diff --git a/lib/mockito-all-1.9.5.jar b/lib/mockito-all-1.9.5.jar new file mode 100644 index 0000000..00416eb Binary files /dev/null and b/lib/mockito-all-1.9.5.jar differ diff --git a/src/com/thoughtworks/go/task/rpmbuild/RPMBuildTask.java b/src/com/thoughtworks/go/task/rpmbuild/RPMBuildTask.java index fb6eec9..6298bdf 100644 --- a/src/com/thoughtworks/go/task/rpmbuild/RPMBuildTask.java +++ b/src/com/thoughtworks/go/task/rpmbuild/RPMBuildTask.java @@ -51,8 +51,7 @@ public ExecutionResult execute(TaskConfig taskConfig, TaskExecutionContext taskE List 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 { @@ -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 command) throws IOException { + ProcessBuilder builder = new ProcessBuilder(command).directory(new File(taskExecutionContext.workingDir())); + return builder.start(); + } + @Override public TaskView view() { return new TaskView() { diff --git a/test/com/thoughtworks/go/task/rpmbuild/RPMBuildTaskTest.java b/test/com/thoughtworks/go/task/rpmbuild/RPMBuildTaskTest.java index 5eb6c52..961989b 100644 --- a/test/com/thoughtworks/go/task/rpmbuild/RPMBuildTaskTest.java +++ b/test/com/thoughtworks/go/task/rpmbuild/RPMBuildTaskTest.java @@ -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)); + } } \ No newline at end of file