diff --git a/.gitignore b/.gitignore index 72b0467..6ddaae8 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,5 @@ sonar-example-plugin.iml .project .settings/ .scannerwork +report.xml +coverage.xml diff --git a/pom.xml b/pom.xml index dc55236..f8df13b 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.github.kronenthaler.sonarqube.plugins.coveragevariation sonar-coveragevariation-plugin sonar-plugin - 3.0.2 + 3.0.3 Coverage Variation Plugin for SonarQube 9.x Plugin calculates the variation of the coverage between the current scan and the main branch's coverage diff --git a/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/CoverageVariation.java b/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/CoverageVariation.java index afe0e9e..56b1033 100644 --- a/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/CoverageVariation.java +++ b/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/CoverageVariation.java @@ -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; @@ -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(); } @@ -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("-------------------------------------------------------------"); diff --git a/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/CoverageVariationMetrics.java b/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/CoverageVariationMetrics.java index fd8668b..9ee7da2 100644 --- a/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/CoverageVariationMetrics.java +++ b/src/main/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/measures/CoverageVariationMetrics.java @@ -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 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(); @@ -31,6 +40,6 @@ public class CoverageVariationMetrics implements Metrics { @Override public List getMetrics() { - return asList(COVERAGE_VARIATION, PREVIOUS_COVERAGE); + return asList(NEW_COVERAGE_VARIATION, COVERAGE_VARIATION, PREVIOUS_COVERAGE); } } diff --git a/src/main/python/test2.py b/src/main/python/test2.py index 1e605dc..bdfe278 100644 --- a/src/main/python/test2.py +++ b/src/main/python/test2.py @@ -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 diff --git a/src/test/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/tests/CoverageVariationTests.java b/src/test/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/tests/CoverageVariationTests.java index f10b94d..fc47407 100644 --- a/src/test/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/tests/CoverageVariationTests.java +++ b/src/test/java/com/github/kronenthaler/sonarqube/plugins/coveragevariation/tests/CoverageVariationTests.java @@ -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);