aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-06-29 10:15:23 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2015-06-29 10:53:10 +0200
commit35960287f74244d80373eaf3af47a14cefc7bcf8 (patch)
tree5197eaf07ca2e23ece847ae482288c11f07fc34c /sonar-batch
parent3b86b3cd173c9aeae215a80b7134d81a8c7a16c8 (diff)
downloadsonarqube-35960287f74244d80373eaf3af47a14cefc7bcf8.tar.gz
sonarqube-35960287f74244d80373eaf3af47a14cefc7bcf8.zip
SONAR-6200 Apply coverage exclusions on new Sensor API
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java13
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageExclusions.java11
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/mediumtest/coverage/CoverageMediumTest.java37
3 files changed, 46 insertions, 15 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java
index b97c94f492b..0277408cbe4 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/sensor/DefaultSensorStorage.java
@@ -21,6 +21,8 @@ package org.sonar.batch.sensor;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
+import java.util.Map;
+import java.util.Set;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputPath;
@@ -53,8 +55,8 @@ import org.sonar.api.source.Symbol;
import org.sonar.api.utils.KeyValueFormat;
import org.sonar.batch.duplication.DuplicationCache;
import org.sonar.batch.index.BatchComponent;
-import org.sonar.batch.index.DefaultIndex;
import org.sonar.batch.index.BatchComponentCache;
+import org.sonar.batch.index.DefaultIndex;
import org.sonar.batch.issue.ModuleIssues;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReport.Range;
@@ -65,9 +67,6 @@ import org.sonar.batch.sensor.coverage.CoverageExclusions;
import org.sonar.batch.source.DefaultSymbol;
import org.sonar.core.component.ComponentKeys;
-import java.util.Map;
-import java.util.Set;
-
public class DefaultSensorStorage implements SensorStorage {
private final MetricFinder metricFinder;
@@ -109,8 +108,7 @@ public class DefaultSensorStorage implements SensorStorage {
measureToSave.setFromCore(measure.isFromCore());
InputFile inputFile = newMeasure.inputFile();
if (inputFile != null) {
- Formula formula = newMeasure.metric() instanceof org.sonar.api.measures.Metric ?
- ((org.sonar.api.measures.Metric) newMeasure.metric()).getFormula() : null;
+ Formula formula = newMeasure.metric() instanceof org.sonar.api.measures.Metric ? ((org.sonar.api.measures.Metric) newMeasure.metric()).getFormula() : null;
if (formula instanceof SumChildDistributionFormula
&& !Scopes.isHigherThanOrEquals(Scopes.FILE, ((SumChildDistributionFormula) formula).getMinimumScopeToPersist())) {
measureToSave.setPersistenceMode(PersistenceMode.MEMORY);
@@ -246,6 +244,9 @@ public class DefaultSensorStorage implements SensorStorage {
@Override
public void store(DefaultCoverage defaultCoverage) {
File file = getFile(defaultCoverage.inputFile());
+ if (coverageExclusions.hasMatchingPattern(file)) {
+ return;
+ }
CoverageType type = defaultCoverage.type();
if (defaultCoverage.linesToCover() > 0) {
sonarIndex.addMeasure(file, new org.sonar.api.measures.Measure(type.linesToCover(), (double) defaultCoverage.linesToCover()));
diff --git a/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageExclusions.java b/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageExclusions.java
index a44bd7c5df0..89343b7274a 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageExclusions.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/sensor/coverage/CoverageExclusions.java
@@ -22,6 +22,10 @@ package org.sonar.batch.sensor.coverage;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
@@ -32,11 +36,6 @@ import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Resource;
import org.sonar.api.utils.WildcardPattern;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Set;
-
public class CoverageExclusions {
private static final Logger LOG = LoggerFactory.getLogger(CoverageExclusions.class);
@@ -106,7 +105,7 @@ public class CoverageExclusions {
return this.coverageMetrics.contains(metric);
}
- private boolean hasMatchingPattern(Resource resource) {
+ public boolean hasMatchingPattern(Resource resource) {
boolean found = false;
Iterator<WildcardPattern> iterator = resourcePatterns.iterator();
while (!found && iterator.hasNext()) {
diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/coverage/CoverageMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/coverage/CoverageMediumTest.java
index b286255c05d..e740ea47c00 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/coverage/CoverageMediumTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/coverage/CoverageMediumTest.java
@@ -20,6 +20,8 @@
package org.sonar.batch.mediumtest.coverage;
import com.google.common.collect.ImmutableMap;
+import java.io.File;
+import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
@@ -33,9 +35,6 @@ import org.sonar.batch.mediumtest.BatchMediumTester;
import org.sonar.batch.mediumtest.TaskResult;
import org.sonar.xoo.XooPlugin;
-import java.io.File;
-import java.io.IOException;
-
import static org.assertj.core.api.Assertions.assertThat;
public class CoverageMediumTest {
@@ -111,4 +110,36 @@ public class CoverageMediumTest {
.withValue("2=1"));
}
+ @Test
+ public void exclusions() throws IOException {
+
+ File baseDir = temp.newFolder();
+ File srcDir = new File(baseDir, "src");
+ srcDir.mkdir();
+
+ File xooFile = new File(srcDir, "sample.xoo");
+ File xooUtCoverageFile = new File(srcDir, "sample.xoo.coverage");
+ FileUtils.write(xooFile, "function foo() {\n if (a && b) {\nalert('hello');\n}\n}");
+ FileUtils.write(xooUtCoverageFile, "2:2:2:1\n3:1");
+
+ TaskResult result = tester.newTask()
+ .properties(ImmutableMap.<String, String>builder()
+ .put("sonar.task", "scan")
+ .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
+ .put("sonar.projectKey", "com.foo.project")
+ .put("sonar.projectName", "Foo Project")
+ .put("sonar.projectVersion", "1.0-SNAPSHOT")
+ .put("sonar.projectDescription", "Description of Foo Project")
+ .put("sonar.sources", "src")
+ .put("sonar.coverage.exclusions", "**/sample.xoo")
+ .build())
+ .start();
+
+ InputFile file = result.inputFile("src/sample.xoo");
+ assertThat(result.coverageFor(file, 2)).isNull();
+
+ assertThat(result.allMeasures()).extracting("metric.key").doesNotContain(CoreMetrics.LINES_TO_COVER, CoreMetrics.UNCOVERED_LINES, CoreMetrics.CONDITIONS_TO_COVER,
+ CoreMetrics.COVERED_CONDITIONS_BY_LINE);
+ }
+
}