From 6cedf507a95545611c31813a427f43291beeadfc Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 31 Jul 2017 16:15:44 +0200 Subject: SONAR-9557 Fix coverage exclusions for global sensors --- .../scanner/deprecated/test/TestPlanBuilder.java | 2 +- .../scanner/phases/AbstractPhaseExecutor.java | 33 +++++- .../sonar/scanner/phases/CoverageExclusions.java | 73 +++++++++++++ .../sonar/scanner/phases/IssuesPhaseExecutor.java | 6 +- .../sonar/scanner/phases/PublishPhaseExecutor.java | 5 +- .../sonar/scanner/report/ComponentsPublisher.java | 2 +- .../sonar/scanner/scan/ModuleScanContainer.java | 2 +- .../scan/filesystem/InputComponentStore.java | 2 +- .../scanner/scan/filesystem/InputFileBuilder.java | 2 +- .../java/org/sonar/scanner/scm/ScmPublisher.java | 2 +- .../sonar/scanner/sensor/DefaultSensorContext.java | 2 +- .../sonar/scanner/sensor/DefaultSensorStorage.java | 118 ++++++++++----------- .../sensor/coverage/CoverageExclusions.java | 79 -------------- .../scanner/sensor/coverage/package-info.java | 21 ---- .../sonar/scanner/source/ZeroCoverageSensor.java | 8 +- 15 files changed, 176 insertions(+), 181 deletions(-) create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/CoverageExclusions.java delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/coverage/CoverageExclusions.java delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/coverage/package-info.java (limited to 'sonar-scanner-engine/src/main/java/org/sonar') diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/test/TestPlanBuilder.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/test/TestPlanBuilder.java index 3f13c7d8958..f4ca4ff12c8 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/test/TestPlanBuilder.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/test/TestPlanBuilder.java @@ -43,7 +43,7 @@ public class TestPlanBuilder extends PerspectiveBuilder { if (component.isFile()) { DefaultInputFile inputFile = (DefaultInputFile) component; if (inputFile.type() == Type.TEST) { - inputFile.setPublish(true); + inputFile.setPublished(true); if (!testPlanByFile.containsKey(inputFile)) { testPlanByFile.put(inputFile, new DefaultTestPlan()); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/AbstractPhaseExecutor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/AbstractPhaseExecutor.java index c759111ae38..94e992c64d8 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/AbstractPhaseExecutor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/AbstractPhaseExecutor.java @@ -21,8 +21,11 @@ package org.sonar.scanner.phases; import org.sonar.api.batch.SensorContext; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.InputModuleHierarchy; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; import org.sonar.scanner.events.BatchStepEvent; import org.sonar.scanner.events.EventBus; import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader; @@ -32,6 +35,8 @@ import org.sonar.scanner.scan.filesystem.FileIndexer; public abstract class AbstractPhaseExecutor { + private static final Logger LOG = Loggers.get(AbstractPhaseExecutor.class); + private final EventBus eventBus; private final PostJobsExecutor postJobsExecutor; private final InitializersExecutor initializersExecutor; @@ -42,10 +47,11 @@ public abstract class AbstractPhaseExecutor { private final IssueExclusionsLoader issueExclusionsLoader; private final InputModuleHierarchy hierarchy; private final FileIndexer fileIndexer; + private final CoverageExclusions coverageExclusions; public AbstractPhaseExecutor(InitializersExecutor initializersExecutor, PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor, SensorContext sensorContext, InputModuleHierarchy hierarchy, EventBus eventBus, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier, - IssueExclusionsLoader issueExclusionsLoader, FileIndexer fileIndexer) { + IssueExclusionsLoader issueExclusionsLoader, FileIndexer fileIndexer, CoverageExclusions coverageExclusions) { this.postJobsExecutor = postJobsExecutor; this.initializersExecutor = initializersExecutor; this.sensorsExecutor = sensorsExecutor; @@ -56,6 +62,7 @@ public abstract class AbstractPhaseExecutor { this.issueExclusionsLoader = issueExclusionsLoader; this.hierarchy = hierarchy; this.fileIndexer = fileIndexer; + this.coverageExclusions = coverageExclusions; } /** @@ -66,7 +73,7 @@ public abstract class AbstractPhaseExecutor { executeInitializersPhase(); - // Index and lock the filesystem + // Index the filesystem indexFs(); // Log detected languages and their profiles after FS is indexed and languages detected @@ -75,6 +82,9 @@ public abstract class AbstractPhaseExecutor { // Initialize issue exclusions initIssueExclusions(); + // Initialize coverage exclusions + initCoverageExclusions(); + sensorsExecutor.execute(sensorContext); afterSensors(); @@ -86,6 +96,25 @@ public abstract class AbstractPhaseExecutor { eventBus.fireEvent(new ProjectAnalysisEvent(module, false)); } + private void initCoverageExclusions() { + if (coverageExclusions.shouldExecute()) { + String stepName = "Init coverage exclusions"; + eventBus.fireEvent(new BatchStepEvent(stepName, true)); + coverageExclusions.log(); + + for (InputFile inputFile : fs.inputFiles(fs.predicates().all())) { + boolean excluded = coverageExclusions.isExcluded(inputFile); + if (excluded) { + ((DefaultInputFile) inputFile).setExcludedForCoverage(true); + LOG.debug("File {} excluded for coverage", inputFile.relativePath()); + } + } + + eventBus.fireEvent(new BatchStepEvent(stepName, false)); + } + + } + protected void afterSensors() { } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/CoverageExclusions.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/CoverageExclusions.java new file mode 100644 index 00000000000..97675a88960 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/CoverageExclusions.java @@ -0,0 +1,73 @@ +/* + * 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.phases; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; +import java.util.Collection; +import java.util.Iterator; +import javax.annotation.concurrent.Immutable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.config.Configuration; +import org.sonar.api.utils.WildcardPattern; + +@Immutable +public class CoverageExclusions { + private static final Logger LOG = LoggerFactory.getLogger(CoverageExclusions.class); + + private Collection exclusionPatterns; + + public CoverageExclusions(Configuration settings) { + Builder builder = ImmutableList.builder(); + for (String pattern : settings.getStringArray(CoreProperties.PROJECT_COVERAGE_EXCLUSIONS_PROPERTY)) { + builder.add(WildcardPattern.create(pattern)); + } + exclusionPatterns = builder.build(); + } + + void log() { + log("Excluded sources for coverage: ", exclusionPatterns); + } + + boolean isExcluded(InputFile file) { + boolean found = false; + Iterator iterator = exclusionPatterns.iterator(); + while (!found && iterator.hasNext()) { + found = iterator.next().match(file.relativePath()); + } + return found; + } + + private static void log(String title, Collection patterns) { + if (!patterns.isEmpty()) { + LOG.info(title); + for (WildcardPattern pattern : patterns) { + LOG.info(" " + pattern); + } + } + } + + public boolean shouldExecute() { + return !exclusionPatterns.isEmpty(); + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/IssuesPhaseExecutor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/IssuesPhaseExecutor.java index 50ef68ab33f..4f40b8b7930 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/IssuesPhaseExecutor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/IssuesPhaseExecutor.java @@ -42,8 +42,10 @@ public final class IssuesPhaseExecutor extends AbstractPhaseExecutor { public IssuesPhaseExecutor(InitializersExecutor initializersExecutor, PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor, SensorContext sensorContext, EventBus eventBus, IssuesReports jsonReport, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier, - IssueExclusionsLoader issueExclusionsLoader, IssueTransition localIssueTracking, InputModuleHierarchy moduleHierarchy, FileIndexer fileIndexer) { - super(initializersExecutor, postJobsExecutor, sensorsExecutor, sensorContext, moduleHierarchy, eventBus, fs, profileVerifier, issueExclusionsLoader, fileIndexer); + IssueExclusionsLoader issueExclusionsLoader, IssueTransition localIssueTracking, InputModuleHierarchy moduleHierarchy, FileIndexer fileIndexer, + CoverageExclusions coverageExclusions) { + super(initializersExecutor, postJobsExecutor, sensorsExecutor, sensorContext, moduleHierarchy, eventBus, fs, profileVerifier, issueExclusionsLoader, fileIndexer, + coverageExclusions); this.eventBus = eventBus; this.issuesReport = jsonReport; this.localIssueTracking = localIssueTracking; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/PublishPhaseExecutor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/PublishPhaseExecutor.java index e0c22ad936e..5deb5629bca 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/PublishPhaseExecutor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/PublishPhaseExecutor.java @@ -40,8 +40,9 @@ public final class PublishPhaseExecutor extends AbstractPhaseExecutor { public PublishPhaseExecutor(InitializersExecutor initializersExecutor, PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor, SensorContext sensorContext, EventBus eventBus, ReportPublisher reportPublisher, DefaultModuleFileSystem fs, QProfileVerifier profileVerifier, - IssueExclusionsLoader issueExclusionsLoader, CpdExecutor cpdExecutor, ScmPublisher scm, InputModuleHierarchy hierarchy, FileIndexer fileIndexer) { - super(initializersExecutor, postJobsExecutor, sensorsExecutor, sensorContext, hierarchy, eventBus, fs, profileVerifier, issueExclusionsLoader, fileIndexer); + IssueExclusionsLoader issueExclusionsLoader, CpdExecutor cpdExecutor, ScmPublisher scm, InputModuleHierarchy hierarchy, FileIndexer fileIndexer, + CoverageExclusions coverageExclusions) { + super(initializersExecutor, postJobsExecutor, sensorsExecutor, sensorContext, hierarchy, eventBus, fs, profileVerifier, issueExclusionsLoader, fileIndexer, coverageExclusions); this.eventBus = eventBus; this.reportPublisher = reportPublisher; this.cpdExecutor = cpdExecutor; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java index 395b6e4b63b..f12ce7f7186 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java @@ -140,7 +140,7 @@ public class ComponentsPublisher implements ReportPublisherStep { } else if (component instanceof DefaultInputFile) { // skip files not marked for publishing DefaultInputFile inputFile = (DefaultInputFile) component; - return !inputFile.publish(); + return !inputFile.isPublished(); } return false; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java index d43666bf93f..b949f802e61 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java @@ -47,6 +47,7 @@ import org.sonar.scanner.issue.ignore.pattern.IssueInclusionPatternInitializer; import org.sonar.scanner.issue.ignore.pattern.PatternMatcher; import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader; import org.sonar.scanner.phases.AbstractPhaseExecutor; +import org.sonar.scanner.phases.CoverageExclusions; import org.sonar.scanner.phases.InitializersExecutor; import org.sonar.scanner.phases.IssuesPhaseExecutor; import org.sonar.scanner.phases.PostJobsExecutor; @@ -70,7 +71,6 @@ import org.sonar.scanner.scan.report.IssuesReports; import org.sonar.scanner.sensor.DefaultSensorStorage; import org.sonar.scanner.sensor.SensorOptimizer; import org.sonar.scanner.sensor.SensorStrategy; -import org.sonar.scanner.sensor.coverage.CoverageExclusions; import org.sonar.scanner.source.HighlightableBuilder; import org.sonar.scanner.source.SymbolizableBuilder; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java index ffc0b2f7a0b..c43c8b05025 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java @@ -78,7 +78,7 @@ public class InputComponentStore { public Iterable allFilesToPublish() { return inputFileCache.values().stream() .map(f -> (DefaultInputFile) f) - .filter(DefaultInputFile::publish)::iterator; + .filter(DefaultInputFile::isPublished)::iterator; } public Iterable allFiles() { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputFileBuilder.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputFileBuilder.java index 7913cb711f9..1248407e2b1 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputFileBuilder.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputFileBuilder.java @@ -68,7 +68,7 @@ public class InputFileBuilder { DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type, language, idGenerator.get()); DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(f, moduleFileSystemInitializer.defaultEncoding())); if (language != null) { - inputFile.setPublish(true); + inputFile.setPublished(true); } return inputFile; diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java index 28d8a400b61..edfb8ce56a6 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java @@ -95,7 +95,7 @@ public final class ScmPublisher { List filesToBlame = new LinkedList<>(); for (InputFile f : componentStore.inputFiles()) { DefaultInputFile inputFile = (DefaultInputFile) f; - if (!inputFile.publish()) { + if (!inputFile.isPublished()) { continue; } if (configuration.forceReloadAll() || f.status() != Status.SAME) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java index 1889bbaab94..af4b631149b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorContext.java @@ -175,6 +175,6 @@ public class DefaultSensorContext implements SensorContext { @Override public void markForPublishing(InputFile inputFile) { DefaultInputFile file = (DefaultInputFile) inputFile; - file.setPublish(true); + file.setPublished(true); } } 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 c6fc08c8ce4..fe00b8e2913 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 @@ -19,6 +19,51 @@ */ package org.sonar.scanner.sensor; +import com.google.common.annotations.VisibleForTesting; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.sonar.api.batch.fs.InputComponent; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.TextRange; +import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.api.batch.measure.Metric; +import org.sonar.api.batch.measure.MetricFinder; +import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage; +import org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens; +import org.sonar.api.batch.sensor.error.AnalysisError; +import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting; +import org.sonar.api.batch.sensor.internal.SensorStorage; +import org.sonar.api.batch.sensor.issue.Issue; +import org.sonar.api.batch.sensor.measure.Measure; +import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; +import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable; +import org.sonar.api.config.Configuration; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.utils.KeyValueFormat; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.core.metric.ScannerMetrics; +import org.sonar.duplications.block.Block; +import org.sonar.duplications.internal.pmd.PmdBlockChunker; +import org.sonar.scanner.cpd.deprecated.DefaultCpdBlockIndexer; +import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; +import org.sonar.scanner.issue.ModuleIssues; +import org.sonar.scanner.protocol.output.FileStructure; +import org.sonar.scanner.protocol.output.ScannerReport; +import org.sonar.scanner.protocol.output.ScannerReportWriter; +import org.sonar.scanner.report.ReportPublisher; +import org.sonar.scanner.report.ScannerReportUtils; +import org.sonar.scanner.repository.ContextPropertiesCache; +import org.sonar.scanner.scan.measure.MeasureCache; + import static java.util.stream.Collectors.toList; import static org.sonar.api.measures.CoreMetrics.BRANCH_COVERAGE; import static org.sonar.api.measures.CoreMetrics.COMMENTED_OUT_CODE_LINES_KEY; @@ -66,54 +111,6 @@ import static org.sonar.api.measures.CoreMetrics.TEST_SUCCESS_DENSITY_KEY; import static org.sonar.api.measures.CoreMetrics.UNCOVERED_CONDITIONS; import static org.sonar.api.measures.CoreMetrics.UNCOVERED_LINES; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.TextRange; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.measure.Metric; -import org.sonar.api.batch.measure.MetricFinder; -import org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage; -import org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens; -import org.sonar.api.batch.sensor.error.AnalysisError; -import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting; -import org.sonar.api.batch.sensor.internal.SensorStorage; -import org.sonar.api.batch.sensor.issue.Issue; -import org.sonar.api.batch.sensor.measure.Measure; -import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; -import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable; -import org.sonar.api.config.Configuration; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.utils.KeyValueFormat; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.core.metric.ScannerMetrics; -import org.sonar.duplications.block.Block; -import org.sonar.duplications.internal.pmd.PmdBlockChunker; -import org.sonar.scanner.cpd.deprecated.DefaultCpdBlockIndexer; -import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; -import org.sonar.scanner.issue.ModuleIssues; -import org.sonar.scanner.protocol.output.FileStructure; -import org.sonar.scanner.protocol.output.ScannerReport; -import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.report.ReportPublisher; -import org.sonar.scanner.report.ScannerReportUtils; -import org.sonar.scanner.repository.ContextPropertiesCache; -import org.sonar.scanner.scan.measure.MeasureCache; -import org.sonar.scanner.sensor.coverage.CoverageExclusions; - -import com.google.common.annotations.VisibleForTesting; - public class DefaultSensorStorage implements SensorStorage { private static final Logger LOG = Loggers.get(DefaultSensorStorage.class); @@ -143,7 +140,6 @@ public class DefaultSensorStorage implements SensorStorage { private final MetricFinder metricFinder; private final ModuleIssues moduleIssues; - private final CoverageExclusions coverageExclusions; private final ReportPublisher reportPublisher; private final MeasureCache measureCache; private final SonarCpdBlockIndex index; @@ -155,13 +151,12 @@ public class DefaultSensorStorage implements SensorStorage { private final Set> byLineMetrics = new HashSet<>(); private final Set alreadyLogged = new HashSet<>(); - public DefaultSensorStorage(MetricFinder metricFinder, ModuleIssues moduleIssues, Configuration settings, CoverageExclusions coverageExclusions, + public DefaultSensorStorage(MetricFinder metricFinder, ModuleIssues moduleIssues, Configuration settings, ReportPublisher reportPublisher, MeasureCache measureCache, SonarCpdBlockIndex index, ContextPropertiesCache contextPropertiesCache, ScannerMetrics scannerMetrics) { this.metricFinder = metricFinder; this.moduleIssues = moduleIssues; this.settings = settings; - this.coverageExclusions = coverageExclusions; this.reportPublisher = reportPublisher; this.measureCache = measureCache; this.index = index; @@ -205,7 +200,7 @@ public class DefaultSensorStorage implements SensorStorage { @Override public void store(Measure newMeasure) { if (newMeasure.inputComponent() instanceof DefaultInputFile) { - ((DefaultInputFile) newMeasure.inputComponent()).setPublish(true); + ((DefaultInputFile) newMeasure.inputComponent()).setPublished(true); } saveMeasure(newMeasure.inputComponent(), (DefaultMeasure) newMeasure); } @@ -221,7 +216,7 @@ public class DefaultSensorStorage implements SensorStorage { public void saveMeasure(InputComponent component, DefaultMeasure measure) { if (component.isFile()) { - ((DefaultInputFile) component).setPublish(true); + ((DefaultInputFile) component).setPublished(true); } if (isDeprecatedMetric(measure.metric().key())) { @@ -256,7 +251,7 @@ public class DefaultSensorStorage implements SensorStorage { if (!component.isFile()) { throw new UnsupportedOperationException("Saving coverage measure is only allowed on files. Attempt to save '" + metric.key() + "' on '" + component.key() + "'"); } - if (coverageExclusions.isExcluded((InputFile) component)) { + if (((DefaultInputFile) component).isExcludedForCoverage()) { return; } saveCoverageMetricInternal((InputFile) component, metric, measureToSave); @@ -357,7 +352,7 @@ public class DefaultSensorStorage implements SensorStorage { @Override public void store(Issue issue) { if (issue.primaryLocation().inputComponent() instanceof DefaultInputFile) { - ((DefaultInputFile) issue.primaryLocation().inputComponent()).setPublish(true); + ((DefaultInputFile) issue.primaryLocation().inputComponent()).setPublished(true); } moduleIssues.initAndAddIssue(issue); } @@ -366,7 +361,7 @@ public class DefaultSensorStorage implements SensorStorage { public void store(DefaultHighlighting highlighting) { ScannerReportWriter writer = reportPublisher.getWriter(); DefaultInputFile inputFile = (DefaultInputFile) highlighting.inputFile(); - inputFile.setPublish(true); + inputFile.setPublished(true); int componentRef = inputFile.batchId(); if (writer.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef)) { throw new UnsupportedOperationException("Trying to save highlighting twice for the same file is not supported: " + inputFile.absolutePath()); @@ -391,7 +386,7 @@ public class DefaultSensorStorage implements SensorStorage { public void store(DefaultSymbolTable symbolTable) { ScannerReportWriter writer = reportPublisher.getWriter(); DefaultInputFile inputFile = (DefaultInputFile) symbolTable.inputFile(); - inputFile.setPublish(true); + inputFile.setPublished(true); int componentRef = inputFile.batchId(); if (writer.hasComponentData(FileStructure.Domain.SYMBOLS, componentRef)) { throw new UnsupportedOperationException("Trying to save symbol table twice for the same file is not supported: " + symbolTable.inputFile().absolutePath()); @@ -423,10 +418,7 @@ public class DefaultSensorStorage implements SensorStorage { @Override public void store(DefaultCoverage defaultCoverage) { DefaultInputFile inputFile = (DefaultInputFile) defaultCoverage.inputFile(); - inputFile.setPublish(true); - if (coverageExclusions.isExcluded(inputFile)) { - return; - } + inputFile.setPublished(true); if (defaultCoverage.linesToCover() > 0) { saveCoverageMetricInternal(inputFile, LINES_TO_COVER, new DefaultMeasure().forMetric(LINES_TO_COVER).withValue(defaultCoverage.linesToCover())); saveCoverageMetricInternal(inputFile, UNCOVERED_LINES, @@ -449,7 +441,7 @@ public class DefaultSensorStorage implements SensorStorage { @Override public void store(DefaultCpdTokens defaultCpdTokens) { DefaultInputFile inputFile = (DefaultInputFile) defaultCpdTokens.inputFile(); - inputFile.setPublish(true); + inputFile.setPublished(true); PmdBlockChunker blockChunker = new PmdBlockChunker(getBlockSize(inputFile.language())); List blocks = blockChunker.chunk(inputFile.key(), defaultCpdTokens.getTokenLines()); index.insert(inputFile, blocks); @@ -462,7 +454,7 @@ public class DefaultSensorStorage implements SensorStorage { @Override public void store(AnalysisError analysisError) { - ((DefaultInputFile) analysisError.inputFile()).setPublish(true); + ((DefaultInputFile) analysisError.inputFile()).setPublished(true); // no op } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/coverage/CoverageExclusions.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/coverage/CoverageExclusions.java deleted file mode 100644 index 5af99184414..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/coverage/CoverageExclusions.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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.sensor.coverage; - -import java.util.Collection; -import java.util.Iterator; - -import javax.annotation.concurrent.Immutable; - -import org.picocontainer.Startable; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.config.Configuration; -import org.sonar.api.utils.WildcardPattern; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableList.Builder; - -@Immutable -public class CoverageExclusions implements Startable { - private static final Logger LOG = LoggerFactory.getLogger(CoverageExclusions.class); - - private Collection exclusionPatterns; - - public CoverageExclusions(Configuration settings) { - Builder builder = ImmutableList.builder(); - for (String pattern : settings.getStringArray(CoreProperties.PROJECT_COVERAGE_EXCLUSIONS_PROPERTY)) { - builder.add(WildcardPattern.create(pattern)); - } - exclusionPatterns = builder.build(); - } - - @Override - public void start() { - log("Excluded sources for coverage: ", exclusionPatterns); - } - - @Override - public void stop() { - // Nothing to do - } - - public boolean isExcluded(InputFile file) { - boolean found = false; - Iterator iterator = exclusionPatterns.iterator(); - while (!found && iterator.hasNext()) { - found = iterator.next().match(file.relativePath()); - } - return found; - } - - private static void log(String title, Collection patterns) { - if (!patterns.isEmpty()) { - LOG.info(title); - for (WildcardPattern pattern : patterns) { - LOG.info(" " + pattern); - } - } - } -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/coverage/package-info.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/coverage/package-info.java deleted file mode 100644 index ffff81bed9b..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/coverage/package-info.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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. - */ -@javax.annotation.ParametersAreNonnullByDefault -package org.sonar.scanner.sensor.coverage; 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 47f10cabe8f..5732f0bacae 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 @@ -28,6 +28,7 @@ import org.sonar.api.batch.Phase; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Type; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.measure.Metric; import org.sonar.api.batch.sensor.Sensor; import org.sonar.api.batch.sensor.SensorContext; @@ -39,7 +40,6 @@ import org.sonar.api.measures.CoreMetrics; import org.sonar.api.utils.KeyValueFormat; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.scanner.scan.measure.MeasureCache; -import org.sonar.scanner.sensor.coverage.CoverageExclusions; @Phase(name = Phase.Name.POST) public final class ZeroCoverageSensor implements Sensor { @@ -59,11 +59,9 @@ public final class ZeroCoverageSensor implements Sensor { } private final MeasureCache measureCache; - private final CoverageExclusions coverageExclusions; - public ZeroCoverageSensor(MeasureCache measureCache, CoverageExclusions exclusions) { + public ZeroCoverageSensor(MeasureCache measureCache) { this.measureCache = measureCache; - this.coverageExclusions = exclusions; } @Override @@ -76,7 +74,7 @@ public final class ZeroCoverageSensor implements Sensor { public void execute(final SensorContext context) { FileSystem fs = context.fileSystem(); for (InputFile f : fs.inputFiles(fs.predicates().hasType(Type.MAIN))) { - if (coverageExclusions.isExcluded(f)) { + if (((DefaultInputFile) f).isExcludedForCoverage()) { continue; } if (!isCoverageMeasuresAlreadyDefined(f)) { -- cgit v1.2.3