Skip to content

Commit

Permalink
fix: npe when no coverage is reported
Browse files Browse the repository at this point in the history
  • Loading branch information
kronenthaler committed Dec 19, 2022
1 parent 7b9961d commit 0e683dd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ sonar-example-plugin.iml
.project
.settings/
.scannerwork
report.xml
coverage.xml
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.github.kronenthaler.sonarqube.plugins.coveragevariation</groupId>
<artifactId>sonar-coveragevariation-plugin</artifactId>
<packaging>sonar-plugin</packaging>
<version>3.0.2</version>
<version>3.0.3</version>

<name>Coverage Variation Plugin for SonarQube 9.x</name>
<description>Plugin calculates the variation of the coverage between the current scan and the main branch's coverage</description>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.kronenthaler.sonarqube.plugins.coveragevariation.measures;

import org.sonar.api.ce.measure.Component;
import org.sonar.api.ce.measure.Measure;
import org.sonar.api.ce.measure.MeasureComputer;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.server.ServerSide;
Expand All @@ -19,7 +20,7 @@ public class CoverageVariation implements MeasureComputer {
public MeasureComputerDefinition define(MeasureComputerDefinitionContext def) {
return def.newDefinitionBuilder()
.setInputMetrics(CoreMetrics.COVERAGE.getKey(), CoverageVariationMetrics.PREVIOUS_COVERAGE.key())
.setOutputMetrics(CoverageVariationMetrics.COVERAGE_VARIATION.key())
.setOutputMetrics(CoverageVariationMetrics.COVERAGE_VARIATION.key(), CoverageVariationMetrics.NEW_COVERAGE_VARIATION.key())
.build();
}

Expand All @@ -31,9 +32,12 @@ public void compute(MeasureComputerContext context) {
return;
}

Double scanCoverage = context.getMeasure(CoreMetrics.COVERAGE.getKey()).getDoubleValue();
Double currentCoverage = context.getMeasure(CoverageVariationMetrics.PREVIOUS_COVERAGE.getKey()).getDoubleValue();
Measure scanCoverageMeasure = context.getMeasure(CoreMetrics.COVERAGE.getKey());
Measure currentCoverageMeasure = context.getMeasure(CoverageVariationMetrics.PREVIOUS_COVERAGE.getKey());
Double scanCoverage = scanCoverageMeasure != null ? scanCoverageMeasure.getDoubleValue() : 0.0;
Double currentCoverage = currentCoverageMeasure != null ? currentCoverageMeasure.getDoubleValue() : 0.0;
context.addMeasure(CoverageVariationMetrics.COVERAGE_VARIATION.key(), scanCoverage - currentCoverage);
context.addMeasure(CoverageVariationMetrics.NEW_COVERAGE_VARIATION.key(), scanCoverage - currentCoverage);

// for debug purposes
log.info("-------------------------------------------------------------");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public class CoverageVariationMetrics implements Metrics {
.setDescription("Difference of the current coverage vs the on new code coverage.")
.setDirection(Metric.DIRECTION_NONE)
.setQualitative(false)
.setHidden(false)
.setDomain(CoreMetrics.DOMAIN_COVERAGE)
.create();

public static final Metric<Double> NEW_COVERAGE_VARIATION = new Metric.Builder("new_coverage_variation", "Coverage variation", Metric.ValueType.PERCENT)
.setDescription("Difference of the current overall coverage vs the on new code overall coverage on branches and pull requests.")
.setDirection(Metric.DIRECTION_NONE)
.setQualitative(false)
.setHidden(false)
.setDomain(CoreMetrics.DOMAIN_COVERAGE)
.create();

Expand All @@ -31,6 +40,6 @@ public class CoverageVariationMetrics implements Metrics {

@Override
public List<Metric> getMetrics() {
return asList(COVERAGE_VARIATION, PREVIOUS_COVERAGE);
return asList(NEW_COVERAGE_VARIATION, COVERAGE_VARIATION, PREVIOUS_COVERAGE);
}
}
6 changes: 6 additions & 0 deletions src/main/python/test2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ def __init__(self):
def f(self):
z = self.y * self.x
return z

def y2(self):
return self.y * self.y

def x2(self):
return self.x * self.x
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ public void testComputeOnProject() {
verify(context, times(1)).addMeasure(CoverageVariationMetrics.COVERAGE_VARIATION.key(), -1.0);
}

@Test
public void testComputeOnProjectWithNoCoverageReported() {
Component component = mock(Component.class);
when(component.getType()).thenReturn(Component.Type.PROJECT);

Measure previousMeasure = mock(Measure.class);
when(previousMeasure.getDoubleValue()).thenReturn(90.0);

MeasureComputer.MeasureComputerContext context = mock(MeasureComputer.MeasureComputerContext.class);
when(context.getComponent()).thenReturn(component);
when(context.getMeasure(CoverageVariationMetrics.PREVIOUS_COVERAGE.getKey())).thenReturn(previousMeasure);
when(context.getMeasure(CoreMetrics.COVERAGE.getKey())).thenReturn(null);
doNothing().when(context).addMeasure(isA(String.class), isA(Double.class));

CoverageVariation target = new CoverageVariation();
target.compute(context);

verify(context, times(1)).addMeasure(CoverageVariationMetrics.COVERAGE_VARIATION.key(), -90.0);
}

@Test
public void testComputeSensorDisabled() {
Component component = mock(Component.class);
Expand Down

0 comments on commit 0e683dd

Please sign in to comment.