aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2020-08-04 13:09:30 -0500
committersonartech <sonartech@sonarsource.com>2020-08-06 20:06:09 +0000
commitcef2b46aa0d547dd1404fb7d01b0ee6b642276cf (patch)
treee870eeeda5ff62d6c6d66480fd9ba720bfaa729c /sonar-scanner-engine
parent0c2f6292c648f0ab951f6fe2700d6e445464e3bf (diff)
downloadsonarqube-cef2b46aa0d547dd1404fb7d01b0ee6b642276cf.tar.gz
sonarqube-cef2b46aa0d547dd1404fb7d01b0ee6b642276cf.zip
SONAR-13734 Coverage might be missing when merging multiple reports
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java10
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java25
2 files changed, 30 insertions, 5 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
index 9eb4b8664e8..0a9345c9c8f 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
@@ -21,11 +21,11 @@ package org.sonar.scanner.sensor;
import java.io.Serializable;
import java.util.HashSet;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
+import java.util.TreeMap;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.sonar.api.batch.fs.InputComponent;
@@ -332,7 +332,7 @@ public class DefaultSensorStorage implements SensorStorage {
DefaultInputFile inputFile = (DefaultInputFile) defaultCoverage.inputFile();
inputFile.setPublished(true);
- Map<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine = reloadExistingCoverage(inputFile);
+ SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine = reloadExistingCoverage(inputFile);
int lineCount = inputFile.lines();
mergeLineCoverageValues(lineCount, defaultCoverage.hitsByLine(), coveragePerLine, (value, builder) -> builder.setHits(builder.getHits() || value > 0));
@@ -345,8 +345,8 @@ public class DefaultSensorStorage implements SensorStorage {
}
- private Map<Integer, ScannerReport.LineCoverage.Builder> reloadExistingCoverage(DefaultInputFile inputFile) {
- Map<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine = new LinkedHashMap<>();
+ private SortedMap<Integer, ScannerReport.LineCoverage.Builder> reloadExistingCoverage(DefaultInputFile inputFile) {
+ SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine = new TreeMap<>();
try (CloseableIterator<ScannerReport.LineCoverage> lineCoverageCloseableIterator = reportPublisher.getReader().readComponentCoverage(inputFile.scannerId())) {
while (lineCoverageCloseableIterator.hasNext()) {
final ScannerReport.LineCoverage lineCoverage = lineCoverageCloseableIterator.next();
@@ -360,7 +360,7 @@ public class DefaultSensorStorage implements SensorStorage {
void apply(Integer value, ScannerReport.LineCoverage.Builder builder);
}
- private void mergeLineCoverageValues(int lineCount, SortedMap<Integer, Integer> valueByLine, Map<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine,
+ private void mergeLineCoverageValues(int lineCount, SortedMap<Integer, Integer> valueByLine, SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine,
LineCoverageOperation op) {
for (Map.Entry<Integer, Integer> lineMeasure : valueByLine.entrySet()) {
int lineIdx = lineMeasure.getKey();
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
index 8e876f810f5..837a54c7776 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
@@ -21,6 +21,8 @@ package org.sonar.scanner.sensor;
import java.io.File;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -36,6 +38,7 @@ import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode;
+import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
import org.sonar.api.batch.sensor.issue.ExternalIssue;
@@ -113,6 +116,28 @@ public class DefaultSensorStorageTest {
}
@Test
+ public void should_merge_coverage() {
+ DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setLines(5).build();
+
+ DefaultCoverage coverage = new DefaultCoverage(underTest);
+ coverage.onFile(file).lineHits(3, 1);
+
+ DefaultCoverage coverage2 = new DefaultCoverage(underTest);
+ coverage2.onFile(file).lineHits(1, 1);
+
+ underTest.store(coverage);
+ underTest.store(coverage2);
+
+ List<ScannerReport.LineCoverage> lineCoverage = new ArrayList<>();
+ reportReader.readComponentCoverage(file.scannerId()).forEachRemaining(lineCoverage::add);
+ assertThat(lineCoverage).containsExactly(
+ // should be sorted by line
+ ScannerReport.LineCoverage.newBuilder().setLine(1).setHits(true).build(),
+ ScannerReport.LineCoverage.newBuilder().setLine(3).setHits(true).build());
+
+ }
+
+ @Test
public void shouldFailIfUnknownMetric() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();