diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-07-24 16:03:02 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-07 11:44:06 +0200 |
commit | 62c163c5b6e4d42117a8ba08dcadbfb27a7a27c5 (patch) | |
tree | 07c7c3188182272c39364afd91405377ce02f1ec | |
parent | 94f2c5e8e859ff49d7a68d50d13b5b4f402ef28e (diff) | |
download | sonarqube-62c163c5b6e4d42117a8ba08dcadbfb27a7a27c5.tar.gz sonarqube-62c163c5b6e4d42117a8ba08dcadbfb27a7a27c5.zip |
SONAR-9600 Disable "force coverage to zero" in incremental mode
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scanner/source/ZeroCoverageSensor.java | 34 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scanner/source/ZeroCoverageSensorTest.java | 46 |
2 files changed, 61 insertions, 19 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/source/ZeroCoverageSensor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/source/ZeroCoverageSensor.java index 5732f0bacae..e788a597f51 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/source/ZeroCoverageSensor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/source/ZeroCoverageSensor.java @@ -22,8 +22,8 @@ package org.sonar.scanner.source; import com.google.common.collect.Sets; import java.util.Map; import java.util.Set; -import java.util.function.Function; import java.util.stream.StreamSupport; +import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.Phase; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; @@ -38,30 +38,23 @@ import org.sonar.api.batch.sensor.coverage.NewCoverage; import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.utils.KeyValueFormat; -import org.sonar.core.util.stream.MoreCollectors; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; import org.sonar.scanner.scan.measure.MeasureCache; +import static org.sonar.core.util.stream.MoreCollectors.toSet; + @Phase(name = Phase.Name.POST) public final class ZeroCoverageSensor implements Sensor { - private static final class MeasureToMetricKey implements Function<DefaultMeasure<?>, String> { - @Override - public String apply(DefaultMeasure<?> input) { - return input.metric().key(); - } - } - - private static final class MetricToKey implements Function<Metric, String> { - @Override - public String apply(Metric input) { - return input.key(); - } - } + private static final Logger LOG = Loggers.get(ZeroCoverageSensor.class); private final MeasureCache measureCache; + private final AnalysisMode mode; - public ZeroCoverageSensor(MeasureCache measureCache) { + public ZeroCoverageSensor(MeasureCache measureCache, AnalysisMode mode) { this.measureCache = measureCache; + this.mode = mode; } @Override @@ -72,6 +65,10 @@ public final class ZeroCoverageSensor implements Sensor { @Override public void execute(final SensorContext context) { + if (mode.isIncremental()) { + LOG.debug("Incremental mode: not forcing coverage to zero"); + return; + } FileSystem fs = context.fileSystem(); for (InputFile f : fs.inputFiles(fs.predicates().hasType(Type.MAIN))) { if (((DefaultInputFile) f).isExcludedForCoverage()) { @@ -100,9 +97,8 @@ public final class ZeroCoverageSensor implements Sensor { private boolean isCoverageMeasuresAlreadyDefined(InputFile f) { Set<String> metricKeys = StreamSupport.stream(measureCache.byComponentKey(f.key()).spliterator(), false) - .map(new MeasureToMetricKey()).collect(MoreCollectors.toSet()); - Function<Metric, String> metricToKey = new MetricToKey(); - Set<String> allCoverageMetricKeys = CoverageType.UNIT.allMetrics().stream().map(metricToKey).collect(MoreCollectors.toSet()); + .map(m -> m.metric().key()).collect(toSet()); + Set<String> allCoverageMetricKeys = CoverageType.UNIT.allMetrics().stream().map(Metric::key).collect(toSet()); return !Sets.intersection(metricKeys, allCoverageMetricKeys).isEmpty(); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/source/ZeroCoverageSensorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/source/ZeroCoverageSensorTest.java new file mode 100644 index 00000000000..3b30721750f --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/source/ZeroCoverageSensorTest.java @@ -0,0 +1,46 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.scanner.source; + +import org.junit.Rule; +import org.junit.Test; +import org.sonar.api.batch.AnalysisMode; +import org.sonar.api.utils.log.LogTester; +import org.sonar.api.utils.log.LoggerLevel; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ZeroCoverageSensorTest { + + @Rule + public LogTester logTester = new LogTester(); + + @Test + public void dontForceCoverageInIncrementalMode() { + AnalysisMode analysisMode = mock(AnalysisMode.class); + when(analysisMode.isIncremental()).thenReturn(true); + ZeroCoverageSensor zeroCoverageSensor = new ZeroCoverageSensor(null, null, analysisMode); + zeroCoverageSensor.execute(null); + assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Incremental mode: not forcing coverage to zero"); + } + +} |