Skip to content

Commit

Permalink
HTML formatter produces empty report if no features run. Closes #191
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloambrosio committed Feb 2, 2012
1 parent bab2b13 commit 574f27a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
6 changes: 4 additions & 2 deletions core/src/main/java/cucumber/formatter/HTMLFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ public void syntaxError(String state, String event, List<String> legalEvents, St

@Override
public void done() {
jsOut().append("});");
copyReportFiles();
if (!firstFeature) {
jsOut().append("});");
copyReportFiles();
}
}

@Override
Expand Down
54 changes: 30 additions & 24 deletions core/src/test/java/cucumber/formatter/HTMLFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import cucumber.io.ClasspathResourceLoader;
import cucumber.runtime.Backend;
import cucumber.runtime.Runtime;
import cucumber.runtime.model.CucumberFeature;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.EcmaError;
Expand All @@ -17,28 +16,26 @@
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

public class HTMLFormatterTest {

@Test
public void writes_proper_html() throws IOException {
File dir = createTempDirectory();
HTMLFormatter f = new HTMLFormatter(dir);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader);
List<CucumberFeature> features = CucumberFeature.load(resourceLoader, asList("cucumber/formatter/HTMLFormatterTest.feature"), emptyList());
List<String> gluePaths = emptyList();
Runtime runtime = new Runtime(resourceLoader, gluePaths, classLoader, asList(mock(Backend.class)), false);
runtime.run(features.get(0), f, f);
f.done();
f.close();
public void noFeaturesProduceEmptyReport() throws IOException {
final List<String> noFeatures = emptyList();
final File report = createFormatterJsReport(noFeatures);

assertEquals(0, report.length());
}

// Let's verify that the JS we wrote parses nicely
@Test
public void oneFeatureProducesValidJavascript() throws IOException {
final File report = createFormatterJsReport(asList("cucumber/formatter/HTMLFormatterTest.feature"));

Context cx = Context.enter();
Global scope = new Global(cx);
File report = new File(dir, "report.js");
try {
cx.evaluateReader(scope, new FileReader(report), report.getAbsolutePath(), 1, null);
fail("Should have failed");
Expand All @@ -47,6 +44,25 @@ public void writes_proper_html() throws IOException {
}
}

private File createFormatterJsReport(final List<String> featurePaths) throws IOException {
final File outputDir = runFeaturesWithFormatter(featurePaths);
final File report = new File(outputDir, "report.js");
return report;
}

private File runFeaturesWithFormatter(final List<String> featurePaths) throws IOException {
final File dir = createTempDirectory();
final HTMLFormatter f = new HTMLFormatter(dir);
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
final ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader);
final List<String> gluePaths = emptyList();
final Runtime runtime = new Runtime(resourceLoader, gluePaths, classLoader, asList(mock(Backend.class)), false);
runtime.run(featurePaths, emptyList(), f, f);
f.done();
f.close();
return dir;
}

private static File createTempDirectory() throws IOException {
File temp = File.createTempFile("temp", Long.toString(System.nanoTime()));

Expand All @@ -60,14 +76,4 @@ private static File createTempDirectory() throws IOException {

return temp;
}

private class CucumberHTML {
public void undefined() {

}

public void DOMFormatter() {

}
}
}

0 comments on commit 574f27a

Please sign in to comment.