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 #789 from Amanjain4269/color-change
Browse files Browse the repository at this point in the history
Do not use color for unchanged coverage #777
  • Loading branch information
uhafner authored Oct 16, 2023
2 parents 6820965 + c0a2b1c commit 1c4eb14
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.apache.commons.lang3.math.Fraction;

import edu.hm.hafner.coverage.Metric;
import edu.hm.hafner.coverage.Metric.MetricTendency;
import edu.hm.hafner.coverage.Node;
import edu.hm.hafner.coverage.Value;
import edu.hm.hafner.echarts.ChartModelConfiguration;
Expand Down Expand Up @@ -544,18 +543,23 @@ public String formatDelta(final Baseline baseline, final Metric metric) {
* @param metric
* the metric to check
*
* @return {@code true} if the trend is positive, {@code false} otherwise
* @return a positive value if the trend is positive, a negative value if the trend is negative, or {@code 0} if there is no significant change in the trend
*/
@SuppressWarnings("unused") // Called by jelly view
public boolean isPositiveTrend(final Baseline baseline, final Metric metric) {
public double getTrend(final Baseline baseline, final Metric metric) {
var delta = getDelta(baseline, metric);
if (delta.isPresent()) {
if (delta.get().compareTo(Fraction.ZERO) > 0) {
return metric.getTendency() == MetricTendency.LARGER_IS_BETTER;
double deltaValue = delta.get().doubleValue();
if (-0.001 < deltaValue && deltaValue < 0.001) {
// for var(--text-color)
return 0;
}
else {
// for var(--red or --green)
return deltaValue;
}
return metric.getTendency() == MetricTendency.SMALLER_IS_BETTER;
}
return true;
return 0; // default to zero
}

/**
Expand Down
7 changes: 5 additions & 2 deletions plugin/src/main/resources/coverage/coverage-summary.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@
<li>${formatter.formatValueWithMetric(value)}
<j:if test="${it.hasDelta(baseline, value.metric)}">
<j:choose>
<j:when test="${it.isPositiveTrend(baseline, value.metric)}">
<j:when test="${it.getTrend(baseline, value.metric) gt 0}">
<j:set var="color" value="var(--green)"/>
</j:when>
<j:otherwise>
<j:when test="${it.getTrend(baseline, value.metric) lt 0}">
<j:set var="color" value="var(--red)"/>
</j:when>
<j:otherwise>
<j:set var="color" value="var(--text-color)"/>
</j:otherwise>
</j:choose>
<span style="color: ${color}">(${it.formatDelta(baseline, value.metric)})</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.TreeMap;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.Fraction;
Expand Down Expand Up @@ -30,6 +31,24 @@
*/
@DefaultLocale("en")
class CoverageBuildActionTest {
private CoverageBuildAction createCoverageBuildActionWithDelta(final Metric metric, final Optional<Fraction> delta) {
Node module = new ModuleNode("module");

var coverageBuilder = new CoverageBuilder();
var percent = coverageBuilder.setMetric(metric).setCovered(1).setMissed(1).build();

module.addValue(percent);

var deltas = new TreeMap<Metric, Fraction>();
delta.ifPresent(d -> deltas.put(metric, d));

var coverages = List.of(percent);

return spy(new CoverageBuildAction(mock(FreeStyleBuild.class), CoverageRecorder.DEFAULT_ID,
StringUtils.EMPTY, StringUtils.EMPTY, module, new QualityGateResult(),
createLog(), "-", deltas, coverages, deltas, coverages, deltas, coverages, false));

Check warning on line 49 in plugin/src/test/java/io/jenkins/plugins/coverage/metrics/steps/CoverageBuildActionTest.java

View check run for this annotation

ci.jenkins.io / CPD

CPD

LOW: Found duplicated code.
Raw output
<pre><code>return spy(new CoverageBuildAction(mock(FreeStyleBuild.class), CoverageRecorder.DEFAULT_ID, StringUtils.EMPTY, StringUtils.EMPTY, module, new QualityGateResult(), createLog(), &#34;-&#34;, deltas, coverages, deltas, coverages, deltas, coverages, false));</code></pre>
}

@Test
void shouldNotLoadResultIfCoverageValuesArePersistedInAction() {
Node module = new ModuleNode("module");
Expand Down Expand Up @@ -88,4 +107,28 @@ void shouldCreateViewModel() {
assertThat(action.getTarget()).extracting(CoverageViewModel::getNode).isSameAs(root);
assertThat(action.getTarget()).extracting(CoverageViewModel::getOwner).isSameAs(action.getOwner());
}

@Test
void shouldReturnPositiveTrendForLineMetric() {
CoverageBuildAction action = createCoverageBuildActionWithDelta(Metric.LINE, Optional.of(Fraction.getFraction(1, 1000)));
assertThat(action.getTrend(Baseline.PROJECT, Metric.LINE)).isPositive();
}

@Test
void shouldReturnNegativeTrendForLineMetric() {
CoverageBuildAction action = createCoverageBuildActionWithDelta(Metric.LINE, Optional.of(Fraction.getFraction(-1, 1000)));
assertThat(action.getTrend(Baseline.PROJECT, Metric.LINE)).isNegative();
}

@Test
void shouldReturnZeroForDeltaWithinBoundaries() {
CoverageBuildAction action = createCoverageBuildActionWithDelta(Metric.LINE, Optional.of(Fraction.getFraction(9, 10_000)));
assertThat(action.getTrend(Baseline.PROJECT, Metric.LINE)).isZero();
}

@Test
void shouldReturnZeroWhenDeltaIsNotPresentForGivenMetric() {
CoverageBuildAction action = createCoverageBuildActionWithDelta(Metric.LINE, Optional.empty());
assertThat(action.getTrend(Baseline.PROJECT, Metric.LINE)).isZero();
}
}

0 comments on commit 1c4eb14

Please sign in to comment.