Skip to content

Commit

Permalink
Report 100% for empty Coverage objects
Browse files Browse the repository at this point in the history
If a section of code has no branches, we should report 100% branch coverage
not 0% coverage.  This prevents spurious threshold violations for such code.

(This concern also applies to other coverage measures, although in practice
I've only encountered this problem for branches.)

Fixes JENKINS-25076 and JENKINS-29117
  • Loading branch information
kscaldef committed Feb 2, 2017
1 parent 2644b67 commit 174dbd9
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
Binary file modified resources/test/multiple.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/jacoco/model/Coverage.java
Expand Up @@ -66,7 +66,7 @@ public int getPercentage() {
public float getPercentageFloat() {
float numerator = covered;
float denominator = missed + covered;
return denominator <= 0 ? 0 : 100 * (numerator / denominator);
return denominator <= 0 ? 100 : 100 * (numerator / denominator);
}

public CoverageElement.Type getType() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/hudson/plugins/jacoco/CoverageTest.java
Expand Up @@ -37,7 +37,7 @@ public void testNormalConstructorInitializes() throws Exception {

@Test
public void testVacuousCoverage() throws Exception {
Coverage c = new Coverage(0, 0);
assertEquals(0, c.getPercentage());
final Coverage c = new Coverage(0, 0);
assertEquals(100, c.getPercentage());
}
}
Expand Up @@ -152,12 +152,12 @@ public void skipZero() throws IOException

private TestCoverageObject createTestCoverage()
{
TestCoverageObject t5 = new TestCoverageObject().branch(6, 30).line(5000, 19000);
TestCoverageObject t4 = new TestCoverageObject().branch(6, 0).line(5000, 19000).previous(t5);
TestCoverageObject t3 = new TestCoverageObject().branch(6, 35).line(5000, 19000).previous(t4);
TestCoverageObject t2 = new TestCoverageObject().branch(15, 23).line(10000, 15000).previous(t3);
TestCoverageObject t1 = new TestCoverageObject().branch(27, 13).line(12000, 18000).previous(t2);
TestCoverageObject t0 = new TestCoverageObject().previous(t1);
final TestCoverageObject t5 = new TestCoverageObject().branch(6, 30).line(5000, 19000);
final TestCoverageObject t4 = new TestCoverageObject().branch(6, 0).line(5000, 19000).previous(t5);
final TestCoverageObject t3 = new TestCoverageObject().branch(6, 35).line(5000, 19000).previous(t4);
final TestCoverageObject t2 = new TestCoverageObject().branch(15, 23).line(10000, 15000).previous(t3);
final TestCoverageObject t1 = new TestCoverageObject().branch(27, 13).line(12000, 18000).previous(t2);
final TestCoverageObject t0 = new TestCoverageObject().branch(40, 0).previous(t1);
ctl.replay();
return t0;
}
Expand Down
Expand Up @@ -88,12 +88,12 @@ protected synchronized void saveNextBuildNumber() throws IOException {
}
};
assertTrue(jacocoColumn.hasCoverage(mockJob));
assertEquals("0.0", jacocoColumn.getPercent(mockJob));
assertEquals(new BigDecimal("0.0"), jacocoColumn.getLineCoverage(mockJob));
assertEquals("100.0", jacocoColumn.getPercent(mockJob));
assertEquals(new BigDecimal("100.0"), jacocoColumn.getLineCoverage(mockJob));

EasyMock.verify(context);
}

@Test
public void testGetLineColorWithNull() throws Exception {
assertNull(jacocoColumn.getLineColor(null, null));
Expand Down

0 comments on commit 174dbd9

Please sign in to comment.