Skip to content
This repository has been archived by the owner on Mar 21, 2019. It is now read-only.

Commit

Permalink
Refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
manupsunny committed Apr 7, 2016
1 parent 00fe9b5 commit 73e6b1e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
24 changes: 10 additions & 14 deletions plugin/src/main/java/com/thoughtworks/gauge/gradle/GaugeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class GaugeTask extends DefaultTask {
private final Logger log = LoggerFactory.getLogger("gauge");
Expand All @@ -44,30 +41,29 @@ public void gauge() {
extension = project.getExtensions().findByType(GaugeExtension.class);
PropertyManager propertyManager = new PropertyManager(project, extension);
propertyManager.setProperties();
executeGaugeSpecs();
}

private void executeGaugeSpecs() throws GaugeExecutionFailedException {
ProcessBuilder builder = new ProcessBuilderFactory(extension).create();
info("Executing command => " + builder.command());
try {
ProcessBuilder builder = createProcessBuilder(new ProcessBuilderFactory(extension));
info("Executing command => " + builder.command());
Process process = builder.start();
executeGaugeSpecs(process);
} catch (IOException e) {
throw new GaugeExecutionFailedException("Gauge or Java runner is not installed! Read http://getgauge.io/documentation/user/current/getting_started/download_and_install.html");
}
}

public void executeGaugeSpecs(Process process) throws GaugeExecutionFailedException {
try {
Util.inheritIO(process.getInputStream(), System.out);
Util.inheritIO(process.getErrorStream(), System.err);
if (process.waitFor() != 0) {
throw new GaugeExecutionFailedException("Execution failed for one or more tests!");
}
} catch (InterruptedException | NullPointerException e) {
throw new GaugeExecutionFailedException(e);
} catch (IOException e) {
throw new GaugeExecutionFailedException("Gauge or Java runner is not installed! Read http://getgauge.io/documentation/user/current/getting_started/download_and_install.html");
}
}

public ProcessBuilder createProcessBuilder(ProcessBuilderFactory factory) {
return factory.create();
}

private void warn(String format, String... args) {
log.warn(String.format(format, args));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void setUp() {
}

@Test
public void shouldLoadProperties() {
public void shouldLoadProperties() throws InterruptedException {
ArrayList<String> expectedCommand = new ArrayList<>();
expectedCommand.add("gauge");
expectedCommand.add("--parallel");
Expand All @@ -43,15 +43,18 @@ public void shouldLoadProperties() {
expectedCommand.add("--verbose");
when(factory.create()).thenReturn(new ProcessBuilder(expectedCommand));

Process process = mock(Process.class);
when(process.waitFor()).thenReturn(0);

GaugeExtension gauge = (GaugeExtension) project.getExtensions().findByName("gauge");
gauge.setInParallel(true);
gauge.setNodes(2);
gauge.setEnv("dev");
gauge.setTags("tag1");
gauge.setAdditionalFlags("--verbose");
task.gauge();
task.executeGaugeSpecs(process);

List<String> command = task.createProcessBuilder(factory).command();
List<String> command = factory.create().command();
assertTrue(command.contains("gauge"));
assertTrue(command.contains("--parallel"));
assertTrue(command.contains("-n"));
Expand All @@ -64,17 +67,20 @@ public void shouldLoadProperties() {
}

@Test
public void shouldLoadSpecsDirPropertyIfFolderExists() {
public void shouldLoadSpecsDirPropertyIfFolderExists() throws InterruptedException {
ArrayList<String> expectedCommand = new ArrayList<>();
expectedCommand.add("gauge");
expectedCommand.add("specsFolder");
when(factory.create()).thenReturn(new ProcessBuilder(expectedCommand));

Process process = mock(Process.class);
when(process.waitFor()).thenReturn(0);

GaugeExtension gauge = (GaugeExtension) project.getExtensions().findByName("gauge");
gauge.setSpecsDir("specsFolder");
task.gauge();
task.executeGaugeSpecs(process);

List<String> command = task.createProcessBuilder(factory).command();
List<String> command = factory.create().command();
assertTrue(command.contains("specsFolder"));
}

Expand All @@ -88,7 +94,7 @@ public void shouldThrowExceptionWhenLoadingSpecsDirPropertyWhenFolderDoesNotExis
gauge.setSpecsDir("nonSpecsFolder");
task.gauge();

List<String> command = task.createProcessBuilder(factory).command();
List<String> command = factory.create().command();
assertTrue(!command.contains("specsFolder"));
}

Expand Down

0 comments on commit 73e6b1e

Please sign in to comment.