Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #201 from jenkinsci/branch-coverage-checks-api
Browse files Browse the repository at this point in the history
Use new delta values for branch coverage in checks title
  • Loading branch information
uhafner authored May 26, 2021
2 parents f52a516 + aaa0557 commit 4399614
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private String extractChecksTitle(final CoverageResult result) {
title.append(extractChecksTitle("Line", "target branch", lineCoverage,
result.getCoverageDelta(CoverageElement.LINE)));
} else if (lastRatios.containsKey(CoverageElement.LINE)) {
title.append(extractChecksTitle("Line", "last successful build", lineCoverage,
title.append(extractChecksTitle("Line", "last successful build", lineCoverage,
lineCoverage - lastRatios.get(CoverageElement.LINE).getPercentageFloat()));
} else {
title.append(extractChecksTitle("Line", "", lineCoverage, 0));
Expand All @@ -129,7 +129,10 @@ private String extractChecksTitle(final CoverageResult result) {

if (result.getCoverage(CoverageElement.CONDITIONAL) != null) {
float branchCoverage = result.getCoverage(CoverageElement.CONDITIONAL).getPercentageFloat();
if (lastRatios.containsKey(CoverageElement.CONDITIONAL)) {
if (result.getReferenceBuildUrl() != null) {
title.append(extractChecksTitle("Branch", "target branch", branchCoverage,
result.getCoverageDelta(CoverageElement.CONDITIONAL)));
} else if (lastRatios.containsKey(CoverageElement.CONDITIONAL)) {
title.append(extractChecksTitle("Branch", "last successful build", branchCoverage,
branchCoverage - lastRatios.get(CoverageElement.CONDITIONAL).getPercentageFloat()));
} else {
Expand All @@ -145,7 +148,7 @@ private String extractChecksTitle(final CoverageResult result) {
}

private String extractChecksTitle(final String elementName, final String targetBuildName,
final float coverage, final float coverageDiff) {
final float coverage, final float coverageDiff) {
StringBuilder title = new StringBuilder()
.append(elementName)
.append(String.format(": %.2f", coverage))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.jenkins.plugins.coverage;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

import org.junit.BeforeClass;
import org.junit.Test;

import org.jvnet.localizer.Localizable;
Expand Down Expand Up @@ -31,6 +33,11 @@ public class CoverageChecksPublisherTest {
private static final String LAST_SUCCESSFUL_BUILD_LINK = "http://127.0.0.1:8080/job/pipeline-coding-style/view/change-requests/job/PR-3/110/";
private static final String HEALTH_REPORT = "Coverage Healthy score is 100%";

@BeforeClass
public static void enforceEnglishLocale() {
Locale.setDefault(Locale.ENGLISH);
}

@Test
public void shouldConstructChecksDetailsWithLineAndMethodCoverage() {
ChecksDetails expectedDetails = new ChecksDetailsBuilder()
Expand Down Expand Up @@ -72,7 +79,7 @@ public void shouldConstructChecksDetailsWithIncreasedLineCoverageAndConditionalC
.withDetailsURL(JENKINS_BASE_URL + "/" + BUILD_LINK + COVERAGE_URL_NAME)
.withOutput(new ChecksOutputBuilder()
.withTitle("Line: 50.00% (+10.00% against target branch). " +
"Branch: 50.00% (+20.00% against last successful build).")
"Branch: 50.00% (+15.00% against target branch).")
.withSummary("* ### [Target branch build](" + JENKINS_BASE_URL + "/" + TARGET_BUILD_LINK + ")\n"
+ "* ### [Last successful build](" + JENKINS_BASE_URL + "/" + LAST_SUCCESSFUL_BUILD_LINK + ")\n"
+ "## " + HEALTH_REPORT + ".")
Expand All @@ -85,7 +92,8 @@ public void shouldConstructChecksDetailsWithIncreasedLineCoverageAndConditionalC
.build())
.build();

CoverageResult result = createCoverageResult((float)0.4, (float)0.3, (float)0.5, (float)0.5, TARGET_BUILD_LINK, +10);
CoverageResult result = createCoverageResult((float)0.4, (float)0.3, (float)0.5, (float)0.5, TARGET_BUILD_LINK,
+10, +15);
CoverageAction action = new CoverageAction(result);

Localizable localizable = mock(Localizable.class);
Expand All @@ -107,11 +115,11 @@ public void shouldConstructChecksDetailsWithDecreasedLineCoverageAndConditionalC
.withDetailsURL(JENKINS_BASE_URL + "/job/pipeline-coding-style/job/PR-3/49/coverage")
.withOutput(new ChecksOutputBuilder()
.withTitle("Line: 50.00% (-10.00% against target branch). " +
"Branch: 50.00% (-20.00% against last successful build).")
"Branch: 50.00% (-15.00% against target branch).")
.withSummary("* ### [Target branch build](" + JENKINS_BASE_URL + "/" + TARGET_BUILD_LINK + ")\n"
+ "* ### [Last successful build](" + JENKINS_BASE_URL + "/" + LAST_SUCCESSFUL_BUILD_LINK + ")\n"
+ "## " + HEALTH_REPORT + ".")
.withText("## Conditional\n* :white_check_mark: Coverage: 50%\n* :arrow_down: Trend: 20%\n"
.withText("## Conditional\n* :white_check_mark: Coverage: 50%\n* :arrow_down: Trend: 15%\n"
+ "## Line\n* :white_check_mark: Coverage: 50%\n* :arrow_down: Trend: 10%\n")
.withText("||Conditional|Line|\n" +
"|:-:|:-:|:-:|\n" +
Expand All @@ -120,7 +128,8 @@ public void shouldConstructChecksDetailsWithDecreasedLineCoverageAndConditionalC
.build())
.build();

CoverageResult result = createCoverageResult((float)0.6, (float)0.7, (float)0.5, (float)0.5, TARGET_BUILD_LINK, -10);
CoverageResult result = createCoverageResult((float)0.6, (float)0.7, (float)0.5, (float)0.5, TARGET_BUILD_LINK,
-10, -15);
CoverageAction action = new CoverageAction(result);

Localizable localizable = mock(Localizable.class);
Expand All @@ -142,7 +151,7 @@ public void shouldConstructChecksDetailsWithUnchangedLineAndConditionalCoverage(
.withDetailsURL(JENKINS_BASE_URL + "/job/pipeline-coding-style/job/PR-3/49/coverage")
.withOutput(new ChecksOutputBuilder()
.withTitle("Line: 60.00% (+0.00% against target branch). " +
"Branch: 40.00% (+0.00% against last successful build).")
"Branch: 40.00% (+0.00% against target branch).")
.withSummary("* ### [Target branch build](" + JENKINS_BASE_URL + "/" + TARGET_BUILD_LINK + ")\n"
+ "* ### [Last successful build](" + JENKINS_BASE_URL + "/" + LAST_SUCCESSFUL_BUILD_LINK + ")\n"
+ "## " + HEALTH_REPORT + ".")
Expand All @@ -153,7 +162,8 @@ public void shouldConstructChecksDetailsWithUnchangedLineAndConditionalCoverage(
.build())
.build();

CoverageResult result = createCoverageResult((float)0.6, (float)0.4, (float)0.6, (float)0.4, TARGET_BUILD_LINK, 0);
CoverageResult result = createCoverageResult((float)0.6, (float)0.4, (float)0.6, (float)0.4, TARGET_BUILD_LINK, 0,
0);
CoverageAction action = new CoverageAction(result);

Localizable localizable = mock(Localizable.class);
Expand Down Expand Up @@ -187,7 +197,7 @@ public void shouldUseLastSuccessfulBuildForLineCoverageIfNoTargetBranchIsCompare
.build())
.build();

CoverageResult result = createCoverageResult((float)0.5, (float)0.3, (float)0.6, (float)0.4, null, 0);
CoverageResult result = createCoverageResult((float)0.5, (float)0.3, (float)0.6, (float)0.4, null, 0, -10);
CoverageAction action = new CoverageAction(result);

Localizable localizable = mock(Localizable.class);
Expand Down Expand Up @@ -267,16 +277,17 @@ public void shouldReportNoLineOrBranchCoverageInChecksTitle() {
}

private CoverageResult createCoverageResult(final float lastLineCoverage, final float lastConditionCoverage,
final float lineCoverage, final float conditionCoverage,
final String targetBuildLink, final float targetBuildDiff) {
final float lineCoverage, final float conditionCoverage,
final String targetBuildLink, final float targetLineDiff, final float targetBranchDiff) {
Run build = mock(Run.class);
Run lastBuild = mock(Run.class);
CoverageResult lastResult = createCoverageResult(lastLineCoverage, lastConditionCoverage);
CoverageResult result = createCoverageResult(lineCoverage, conditionCoverage);

when(result.getPreviousResult()).thenReturn(lastResult);
when(result.getReferenceBuildUrl()).thenReturn(targetBuildLink);
when(result.getCoverageDelta(CoverageElement.LINE)).thenReturn(targetBuildDiff);
when(result.getCoverageDelta(CoverageElement.LINE)).thenReturn(targetLineDiff);
when(result.getCoverageDelta(CoverageElement.CONDITIONAL)).thenReturn(targetBranchDiff);
when(result.getOwner()).thenReturn(build);
when(build.getUrl()).thenReturn(BUILD_LINK);
when(build.getPreviousSuccessfulBuild()).thenReturn(lastBuild);
Expand Down

0 comments on commit 4399614

Please sign in to comment.