Skip to content

Commit

Permalink
Show message in HTML report when source file can't be found (bazelbui…
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin authored and marchof committed Dec 23, 2018
1 parent d919b8e commit ccad8eb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ <h3>New Features</h3>
<li>Branches added by the Kotlin compiler for coroutines are filtered out
during generation of report
(GitHub <a href="https://github.com/jacoco/jacoco/issues/802">#802</a>).</li>
<li>HTML report shows message when source file can't be found
(GitHub <a href="https://github.com/jacoco/jacoco/issues/801">#801</a>).</li>
</ul>

<h3>Fixed Bugs</h3>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.internal.analysis.ClassCoverageImpl;
import org.jacoco.core.internal.analysis.MethodCoverageImpl;
import org.jacoco.report.internal.ReportOutputFolder;
import org.jacoco.report.internal.html.ILinkable;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -47,6 +49,7 @@ public void testContents() throws Exception {
page.render();

final Document doc = support.parse(output.getFile("Foo.html"));
assertEquals("", support.findStr(doc, "doc/body/p[1]"));
assertEquals("el_method", support.findStr(doc,
"/html/body/table[1]/tbody/tr[1]/td[1]/span/@class"));
assertEquals("a()", support.findStr(doc,
Expand All @@ -57,6 +60,74 @@ public void testContents() throws Exception {
"/html/body/table[1]/tbody/tr[3]/td[1]/span"));
}

@Test
public void should_not_generate_message_when_SourceFileName_not_present()
throws Exception {
page = new ClassPage(node, null, null, rootFolder, context);
page.render();

final Document doc = support.parse(output.getFile("Foo.html"));
assertEquals("", support.findStr(doc, "/html/body/p[1]"));
}

@Test
public void should_generate_message_when_SourceFileName_present_but_no_SourceFilePage()
throws Exception {
node.setSourceFileName("Foo.java");

page = new ClassPage(node, null, null, rootFolder, context);
page.render();

final Document doc = support.parse(output.getFile("Foo.html"));
assertEquals(
"Source file \"org/jacoco/example/Foo.java\" was not found during generation of report.",
support.findStr(doc, "/html/body/p[1]"));
}

@Test
public void should_generate_message_with_default_package_when_SourceFileName_present_but_no_SourceFilePage()
throws Exception {
node = new ClassCoverageImpl("Foo", 123, false);
node.addMethod(new MethodCoverageImpl("a", "()V", null));
node.setSourceFileName("Foo.java");

page = new ClassPage(node, null, null, rootFolder, context);
page.render();

final Document doc = support.parse(output.getFile("Foo.html"));
assertEquals(
"Source file \"Foo.java\" was not found during generation of report.",
support.findStr(doc, "/html/body/p[1]"));
}

@Test
public void should_not_generate_message_when_SourceFileName_and_SourceFilePage_present()
throws Exception {
node.setSourceFileName("Foo.java");

page = new ClassPage(node, null, new SourceLink(), rootFolder, context);
page.render();

final Document doc = support.parse(output.getFile("Foo.html"));
assertEquals("", support.findStr(doc, "/html/body/p[1]"));
}

private class SourceLink implements ILinkable {

public String getLink(final ReportOutputFolder base) {
return "Source.java.html";
}

public String getLinkLabel() {
return "";
}

public String getLinkStyle() {
return null;
}

}

@Test
public void testGetFileName() throws IOException {
page = new ClassPage(node, null, null, rootFolder, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.analysis.IMethodCoverage;
import org.jacoco.report.internal.ReportOutputFolder;
import org.jacoco.report.internal.html.HTMLElement;
import org.jacoco.report.internal.html.IHTMLReportContext;
import org.jacoco.report.internal.html.ILinkable;

Expand Down Expand Up @@ -80,4 +81,20 @@ public String getLinkLabel() {
getNode().getInterfaceNames());
}

@Override
protected void content(HTMLElement body) throws IOException {
if (getNode().getSourceFileName() != null && sourcePage == null) {
final String sourcePath;
if (getNode().getPackageName().length() != 0) {
sourcePath = getNode().getPackageName() + "/" + getNode().getSourceFileName();
} else {
sourcePath = getNode().getSourceFileName();
}
body.p().text("Source file \"" + sourcePath
+ "\" was not found during generation of report.");
}

super.content(body);
}

}

0 comments on commit ccad8eb

Please sign in to comment.