From 574f27a4b9158d42fdf773ef27418d9f030dd558 Mon Sep 17 00:00:00 2001 From: Paolo Ambrosio Date: Thu, 2 Feb 2012 21:41:10 +0100 Subject: [PATCH] HTML formatter produces empty report if no features run. Closes #191 --- .../cucumber/formatter/HTMLFormatter.java | 6 ++- .../cucumber/formatter/HTMLFormatterTest.java | 54 ++++++++++--------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/cucumber/formatter/HTMLFormatter.java b/core/src/main/java/cucumber/formatter/HTMLFormatter.java index 7fbd39d380..149b24de0b 100644 --- a/core/src/main/java/cucumber/formatter/HTMLFormatter.java +++ b/core/src/main/java/cucumber/formatter/HTMLFormatter.java @@ -103,8 +103,10 @@ public void syntaxError(String state, String event, List legalEvents, St @Override public void done() { - jsOut().append("});"); - copyReportFiles(); + if (!firstFeature) { + jsOut().append("});"); + copyReportFiles(); + } } @Override diff --git a/core/src/test/java/cucumber/formatter/HTMLFormatterTest.java b/core/src/test/java/cucumber/formatter/HTMLFormatterTest.java index 4e9f6b3deb..598c72ee87 100644 --- a/core/src/test/java/cucumber/formatter/HTMLFormatterTest.java +++ b/core/src/test/java/cucumber/formatter/HTMLFormatterTest.java @@ -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; @@ -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 features = CucumberFeature.load(resourceLoader, asList("cucumber/formatter/HTMLFormatterTest.feature"), emptyList()); - List 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 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"); @@ -47,6 +44,25 @@ public void writes_proper_html() throws IOException { } } + private File createFormatterJsReport(final List featurePaths) throws IOException { + final File outputDir = runFeaturesWithFormatter(featurePaths); + final File report = new File(outputDir, "report.js"); + return report; + } + + private File runFeaturesWithFormatter(final List 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 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())); @@ -60,14 +76,4 @@ private static File createTempDirectory() throws IOException { return temp; } - - private class CucumberHTML { - public void undefined() { - - } - - public void DOMFormatter() { - - } - } }