Skip to content

Commit 60bf05b

Browse files
committed
Fix embedding in HTML reports. Closes #412.
1 parent d0de066 commit 60bf05b

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## [Git master](https://github.com/cucumber/cucumber-jvm/compare/v1.1.1...master)
22

3+
* [Core] Embedded data fails to display in HTML reports due to invalid string passed from HTMLFormatter ([#412](https://github.com/cucumber/cucumber-jvm/issues/412) Aslak Hellesøy)
34
* [Scala] Downgrade to scala 2.9.2 - we'll only use stable versions from now on. (Aslak Hellesøy)
45
* [Scala] Passing Scenario reference in Before and After hooks ([#431](https://github.com/cucumber/cucumber-jvm/pull/431) Anshul Bajpai)
56
* [Core] RunCukesTest prevents the execution of other tests ([#304](https://github.com/cucumber/cucumber-jvm/issues/304), [#430](https://github.com/cucumber/cucumber-jvm/pull/430) Mishail)

core/src/main/java/cucumber/runtime/formatter/HTMLFormatter.java

+29-24
Original file line numberDiff line numberDiff line change
@@ -60,37 +60,37 @@ public void uri(String uri) {
6060
.append(JS_FORMATTER_VAR).append(" = new CucumberHTML.DOMFormatter($('.cucumber-report'));");
6161
firstFeature = false;
6262
}
63-
writeToJsReport("uri", "'" + uri + "'");
63+
jsFunctionCall("uri", uri);
6464
}
6565

6666
@Override
6767
public void feature(Feature feature) {
68-
writeToJsReport("feature", feature);
68+
jsFunctionCall("feature", feature);
6969
}
7070

7171
@Override
7272
public void background(Background background) {
73-
writeToJsReport("background", background);
73+
jsFunctionCall("background", background);
7474
}
7575

7676
@Override
7777
public void scenario(Scenario scenario) {
78-
writeToJsReport("scenario", scenario);
78+
jsFunctionCall("scenario", scenario);
7979
}
8080

8181
@Override
8282
public void scenarioOutline(ScenarioOutline scenarioOutline) {
83-
writeToJsReport("scenarioOutline", scenarioOutline);
83+
jsFunctionCall("scenarioOutline", scenarioOutline);
8484
}
8585

8686
@Override
8787
public void examples(Examples examples) {
88-
writeToJsReport("examples", examples);
88+
jsFunctionCall("examples", examples);
8989
}
9090

9191
@Override
9292
public void step(Step step) {
93-
writeToJsReport("step", step);
93+
jsFunctionCall("step", step);
9494
}
9595

9696
@Override
@@ -114,34 +114,24 @@ public void close() {
114114
jsOut().close();
115115
}
116116

117-
private void writeToJsReport(String functionName, String arg) {
118-
String stringArg = gson.toJson(arg);
119-
jsOut().append(JS_FORMATTER_VAR + ".").append(functionName).append("(").append(stringArg).append(");").println();
120-
}
121-
122-
private void writeToJsReport(String functionName, Mappable arg) {
123-
String stringArg = gson.toJson(arg.toMap());
124-
jsOut().append(JS_FORMATTER_VAR + ".").append(functionName).append("(").append(stringArg).append(");").println();
125-
}
126-
127117
@Override
128118
public void result(Result result) {
129-
writeToJsReport("result", result);
119+
jsFunctionCall("result", result);
130120
}
131121

132122
@Override
133123
public void before(Match match, Result result) {
134-
writeToJsReport("before", result);
124+
jsFunctionCall("before", result);
135125
}
136126

137127
@Override
138128
public void after(Match match, Result result) {
139-
writeToJsReport("after", result);
129+
jsFunctionCall("after", result);
140130
}
141131

142132
@Override
143133
public void match(Match match) {
144-
writeToJsReport("match", match);
134+
jsFunctionCall("match", match);
145135
}
146136

147137
@Override
@@ -151,19 +141,34 @@ public void embedding(String mimeType, byte[] data) {
151141
if (extension != null) {
152142
StringBuilder fileName = new StringBuilder("embedded").append(embeddedIndex++).append(".").append(extension);
153143
writeBytesAndClose(data, reportFileOutputStream(fileName.toString()));
154-
writeToJsReport("embedding", new StringBuilder("'").append(mimeType).append("','").append(fileName).append("'").toString());
144+
jsFunctionCall("embedding", mimeType, fileName);
155145
}
156146
}
157147

158148
@Override
159149
public void write(String text) {
160-
writeToJsReport("write", text);
150+
jsFunctionCall("write", text);
151+
}
152+
153+
private void jsFunctionCall(String functionName, Object... args) {
154+
NiceAppendable out = jsOut().append(JS_FORMATTER_VAR + ".").append(functionName).append("(");
155+
boolean comma = false;
156+
for (Object arg : args) {
157+
if (comma) {
158+
out.append(", ");
159+
}
160+
arg = arg instanceof Mappable ? ((Mappable) arg).toMap() : arg;
161+
String stringArg = gson.toJson(arg);
162+
out.append(stringArg);
163+
comma = true;
164+
}
165+
out.append(");").println();
161166
}
162167

163168
private void copyReportFiles() {
164169
for (String textAsset : TEXT_ASSETS) {
165170
InputStream textAssetStream = getClass().getResourceAsStream(textAsset);
166-
if(textAssetStream == null) {
171+
if (textAssetStream == null) {
167172
throw new CucumberException("Couldn't find " + textAsset + ". Is cucumber-html on your classpath? Make sure you have the right version.");
168173
}
169174
String baseName = new File(textAsset).getName();

core/src/test/java/cucumber/runtime/formatter/HTMLFormatterTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package cucumber.runtime.formatter;
22

3+
import gherkin.formatter.model.Comment;
4+
import gherkin.formatter.model.Scenario;
5+
import gherkin.formatter.model.Tag;
6+
import gherkin.util.FixJava;
37
import org.jsoup.Jsoup;
48
import org.jsoup.nodes.Document;
59
import org.jsoup.nodes.Element;
@@ -10,8 +14,10 @@
1014
import org.mozilla.javascript.tools.shell.Global;
1115

1216
import java.io.File;
17+
import java.io.FileNotFoundException;
1318
import java.io.FileReader;
1419
import java.io.IOException;
20+
import java.util.Collections;
1521

1622
import static org.junit.Assert.assertEquals;
1723
import static org.junit.Assert.assertTrue;
@@ -48,9 +54,29 @@ public void writes_valid_report_js() throws IOException {
4854
}
4955
}
5056

57+
@Test
58+
public void includes_uri() throws FileNotFoundException {
59+
String reportJs = FixJava.readReader(new FileReader(new File(outputDir, "report.js")));
60+
assertContains("formatter.uri(\"some\\\\windows\\\\path\\\\some.feature\");", reportJs);
61+
}
62+
63+
@Test
64+
public void included_embedding() throws FileNotFoundException {
65+
String reportJs = FixJava.readReader(new FileReader(new File(outputDir, "report.js")));
66+
assertContains("formatter.embedding(\"image/png\", \"embedded0.png\");", reportJs);
67+
}
68+
69+
private void assertContains(String substring, String string) {
70+
if (string.indexOf(substring) == -1) {
71+
fail(String.format("[%s] not contained in [%s]", substring, string));
72+
}
73+
}
74+
5175
private void runFeaturesWithFormatter(File outputDir) throws IOException {
5276
final HTMLFormatter f = new HTMLFormatter(outputDir);
5377
f.uri("some\\windows\\path\\some.feature");
78+
f.scenario(new Scenario(Collections.<Comment>emptyList(), Collections.<Tag>emptyList(), "Scenario", "some cukes", "", 10, "id"));
79+
f.embedding("image/png", "fakedata".getBytes("US-ASCII"));
5480
f.done();
5581
f.close();
5682
}

0 commit comments

Comments
 (0)