Skip to content

Commit

Permalink
Deleting gauge project directory in after scenario hook #16
Browse files Browse the repository at this point in the history
  • Loading branch information
kashishm committed Feb 7, 2017
1 parent 70a7f97 commit 0c99a84
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 46 deletions.
50 changes: 11 additions & 39 deletions src/test/java/com/thoughtworks/gauge/test/common/GaugeProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

public abstract class GaugeProject {

private static final List<String> PRODUCT_ENVS = Arrays.asList("GAUGE_ROOT", "GAUGE_HOME");
private static final List<String> PRODUCT_ENVS = Arrays.asList("GAUGE_ROOT", "GAUGE_HOME");
private static final String PRODUCT_PREFIX = "GAUGE_";
static final String PRINT_PARAMS = "print params";
static final String THROW_EXCEPTION = "throw exception";
Expand All @@ -37,8 +37,6 @@ protected GaugeProject(String language, String projName) throws IOException {
currentProject = this;

this.projectDir = Files.createTempDirectory(projName + projectCount++ + "_").toFile();
projectDir.deleteOnExit();
registerShutDownHook();
}

public static GaugeProject getCurrentProject() {
Expand Down Expand Up @@ -116,10 +114,10 @@ public Specification createSpecification(String specsDirName, String name) throw

private File getSpecFile(String name, String dirPath) {
name = Util.getSpecName(name);
return getFile(name, dirPath,".spec");
return getFile(name, dirPath, ".spec");
}

private File getFile(String name, String dirPath,String extension) {
private File getFile(String name, String dirPath, String extension) {
if (!new File(projectDir, dirPath).exists()) {
new File(projectDir, dirPath).mkdirs();
}
Expand All @@ -130,12 +128,12 @@ private File getSpecFile(String name) {
return getSpecFile(name, "");
}

public File createCsv(String name, String dirPath){
return getFile(name,specsDirName,".csv");
public File createCsv(String name, String dirPath) {
return getFile(name, specsDirName, ".csv");
}

public File createTxt(String name, String dirPath){
return getFile(name,specsDirName,".txt");
public File createTxt(String name, String dirPath) {
return getFile(name, specsDirName, ".txt");
}

public Specification findSpecification(String specName) {
Expand Down Expand Up @@ -308,41 +306,15 @@ private void filterConflictingEnv(ProcessBuilder processBuilder) {
.forEach(env -> processBuilder.environment().put(env, ""));
}

private void registerShutDownHook() {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
Path directory = Paths.get(projectDir.getAbsolutePath());
try {
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
// ignore
}
}
});
}

public void refactorStep(String oldStep, String newStep) throws Exception {
ExecutionSummary result = currentProject.executeRefactor(oldStep, newStep);
if (!result.getSuccess()) {
System.out.println(currentProject.getLastProcessStdout());
}
}

public static void implement(Table impl, TableRow row,boolean appendCode) throws Exception {
if(impl.getColumnNames().contains("implementation")) {
public static void implement(Table impl, TableRow row, boolean appendCode) throws Exception {
if (impl.getColumnNames().contains("implementation")) {
StepImpl stepImpl = new StepImpl(row.getCell("step text"), row.getCell("implementation"), Boolean.parseBoolean(row.getCell("continue on failure")), appendCode, row.getCell("error type"));
currentProject.implementStep(stepImpl);
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/com/thoughtworks/gauge/test/common/Hooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.thoughtworks.gauge.test.common;

import com.thoughtworks.gauge.AfterScenario;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

import static com.thoughtworks.gauge.test.common.GaugeProject.currentProject;

public class Hooks {
@AfterScenario
public void delete() {
File dir = GaugeProject.getCurrentProject().getProjectDir();
try {
FileUtils.deleteDirectory(dir);
} catch (IOException e) {
System.err.println(String.format("Could not delete project directory %s", dir.getAbsolutePath()));
}
}

@AfterScenario
public void tearDown() {
if (currentProject.getService() != null)
currentProject.getService().getGaugeProcess().destroy();
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.thoughtworks.gauge.test.implementation.api;

import com.thoughtworks.gauge.*;
import com.thoughtworks.gauge.Step;
import com.thoughtworks.gauge.StepValue;
import com.thoughtworks.gauge.Table;
import com.thoughtworks.gauge.TableRow;
import com.thoughtworks.gauge.connection.GaugeConnection;

import java.io.IOException;
Expand Down Expand Up @@ -81,12 +84,6 @@ public void verifyRefactoring() {
assertThat(refactorResponse.getFilesChangedList()).isEmpty();
}

@AfterScenario
public void tearDown() {
if (currentProject.getService() != null)
currentProject.getService().getGaugeProcess().destroy();
}

private List<String> getSteps(Table table) {
List<String> columnNames = table.getColumnNames();
final List<String> steps = new ArrayList<>();
Expand Down

0 comments on commit 0c99a84

Please sign in to comment.