From ba3e1b66446e3ea1a33dda2186aca611be3fe9a5 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 26 Apr 2018 21:35:28 +0200 Subject: [PATCH] SONAR-10648 Fix InputFile::relativePath for global sensors SensorStrategy was wrongly instanciated once per module, while it was supposed to be a project level singleton. --- .../main/java/org/sonar/xoo/XooPlugin.java | 4 ++ .../org/sonar/xoo/global/GlobalSensor.java | 45 +++++++++++++++++++ .../java/org/sonar/xoo/XooPluginTest.java | 2 +- .../scanner/scan/ModuleScanContainer.java | 3 -- .../scanner/scan/ProjectScanContainer.java | 3 ++ .../scanner/mediumtest/LogOutputRecorder.java | 4 +- .../mediumtest/fs/FileSystemMediumTest.java | 18 +++++++- .../module_a1/.sonar/sonar-report.json | 1 - 8 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/global/GlobalSensor.java delete mode 100644 sonar-scanner-engine/src/test/resources/mediumtest/xoo/multi-modules-sample/module_a/module_a1/.sonar/sonar-report.json diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java index 922bd8fdda0..7be068fce9c 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java @@ -30,6 +30,7 @@ import org.sonar.xoo.coverage.OverallCoverageSensor; import org.sonar.xoo.coverage.UtCoverageSensor; import org.sonar.xoo.extensions.XooPostJob; import org.sonar.xoo.extensions.XooProjectBuilder; +import org.sonar.xoo.global.GlobalSensor; import org.sonar.xoo.lang.CpdTokenizerSensor; import org.sonar.xoo.lang.LineMeasureSensor; import org.sonar.xoo.lang.MeasureSensor; @@ -165,6 +166,9 @@ public class XooPlugin implements Plugin { if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(6,6))) { context.addExtension(XooBuiltInQualityProfilesDefinition.class); } + if (context.getSonarQubeVersion().isGreaterThanOrEqual(Version.create(6, 4))) { + context.addExtension(GlobalSensor.class); + } } } diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/global/GlobalSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/global/GlobalSensor.java new file mode 100644 index 00000000000..d2ce58f5faa --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/global/GlobalSensor.java @@ -0,0 +1,45 @@ +/* + * 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.xoo.global; + +import org.sonar.api.batch.sensor.Sensor; +import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.api.batch.sensor.SensorDescriptor; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; + +public class GlobalSensor implements Sensor { + + private static final Logger LOG = Loggers.get(GlobalSensor.class); + public static final String ENABLE_PROP = "sonar.scanner.mediumtest.globalSensor"; + + @Override + public void describe(SensorDescriptor descriptor) { + descriptor + .name("Global Sensor") + .global() + .onlyWhenConfiguration(c -> c.hasKey(ENABLE_PROP)); + } + + @Override + public void execute(SensorContext context) { + context.fileSystem().inputFiles(context.fileSystem().predicates().all()).forEach(inputFile -> LOG.info("Global Sensor: {}", inputFile.relativePath())); + } +} diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java index 1e71ad76efc..db75a0732d9 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java @@ -52,6 +52,6 @@ public class XooPluginTest { SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(Version.parse("6.6"), SonarQubeSide.SCANNER); Plugin.Context context = new Plugin.Context(runtime); new XooPlugin().define(context); - assertThat(context.getExtensions()).hasSize(50).contains(CpdTokenizerSensor.class); + assertThat(context.getExtensions()).hasSize(51).contains(CpdTokenizerSensor.class); } } 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 1628dc97d52..7fd78289e43 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 @@ -24,7 +24,6 @@ import org.slf4j.LoggerFactory; import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.FileMetadata; -import org.sonar.api.batch.fs.internal.SensorStrategy; import org.sonar.api.batch.rule.CheckFactory; import org.sonar.api.issue.NoSonarFilter; import org.sonar.api.resources.Project; @@ -140,8 +139,6 @@ public class ModuleScanContainer extends ComponentContainer { IssueFilters.class, CoverageExclusions.class, - SensorStrategy.class, - // rules new RulesProfileProvider(), CheckFactory.class, diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java index 2fa46059167..639fe3ac6a4 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectScanContainer.java @@ -25,6 +25,7 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.InputModuleHierarchy; +import org.sonar.api.batch.fs.internal.SensorStrategy; import org.sonar.api.config.Settings; import org.sonar.api.resources.Languages; import org.sonar.api.resources.ResourceTypes; @@ -190,6 +191,8 @@ public class ProjectScanContainer extends ComponentContainer { ContextPropertiesCache.class, ContextPropertiesPublisher.class, + SensorStrategy.class, + MutableProjectSettings.class, new ProjectSettingsProvider(), diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/LogOutputRecorder.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/LogOutputRecorder.java index 443f4cb09fb..44f7f9a898c 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/LogOutputRecorder.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/LogOutputRecorder.java @@ -19,7 +19,7 @@ */ package org.sonar.scanner.mediumtest; -import com.google.common.collect.HashMultimap; +import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Multimap; import java.util.ArrayList; import java.util.Collection; @@ -29,7 +29,7 @@ import java.util.stream.Collectors; import org.sonar.batch.bootstrapper.LogOutput; public class LogOutputRecorder implements LogOutput { - private final Multimap recordedByLevel = HashMultimap.create(); + private final Multimap recordedByLevel = LinkedHashMultimap.create(); private final List recorded = new LinkedList<>(); private final StringBuffer asString = new StringBuffer(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java index 8a5dada204c..8e683f8f370 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java @@ -43,6 +43,7 @@ import org.sonar.scanner.mediumtest.LogOutputRecorder; import org.sonar.scanner.mediumtest.ScannerMediumTester; import org.sonar.scanner.mediumtest.TaskResult; import org.sonar.xoo.XooPlugin; +import org.sonar.xoo.global.GlobalSensor; import org.sonar.xoo.rule.XooRulesDefinition; import static org.assertj.core.api.Assertions.assertThat; @@ -675,11 +676,26 @@ public class FileSystemMediumTest { .newScanTask(new File(projectDir, "sonar-project.properties")) .execute(); - System.out.println(logs.getAsString()); assertThat(result.inputFiles()).hasSize(4); assertThat(result.inputDirs()).hasSize(4); } + @Test + public void global_sensor_should_see_project_relative_paths() { + File projectDir = new File("src/test/resources/mediumtest/xoo/multi-modules-sample"); + TaskResult result = tester + .newScanTask(new File(projectDir, "sonar-project.properties")) + .property(GlobalSensor.ENABLE_PROP, "true") + .execute(); + + assertThat(result.inputFiles()).hasSize(4); + assertThat(logs.get("INFO")).contains( + "Global Sensor: module_a/module_a1/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo", + "Global Sensor: module_a/module_a2/src/main/xoo/com/sonar/it/samples/modules/a2/HelloA2.xoo", + "Global Sensor: module_b/module_b1/src/main/xoo/com/sonar/it/samples/modules/b1/HelloB1.xoo", + "Global Sensor: module_b/module_b2/src/main/xoo/com/sonar/it/samples/modules/b2/HelloB2.xoo"); + } + @Test public void scanProjectWithCommaInSourcePath() throws IOException { File srcDir = new File(baseDir, "src"); diff --git a/sonar-scanner-engine/src/test/resources/mediumtest/xoo/multi-modules-sample/module_a/module_a1/.sonar/sonar-report.json b/sonar-scanner-engine/src/test/resources/mediumtest/xoo/multi-modules-sample/module_a/module_a1/.sonar/sonar-report.json deleted file mode 100644 index 581142ee53c..00000000000 --- a/sonar-scanner-engine/src/test/resources/mediumtest/xoo/multi-modules-sample/module_a/module_a1/.sonar/sonar-report.json +++ /dev/null @@ -1 +0,0 @@ -{"version":"5.1-SNAPSHOT","issues":[{"key":"0ae8428f-42a6-4b44-befc-512f1846fdad","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":1,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"215429f4-fa2e-4611-a75b-abb4330ea36b","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":4,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"2b5d88b5-acc5-4761-bf47-3d6d46435c59","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":10,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"50fb32b3-61af-4efa-b234-5a5b048d72a1","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":16,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"52911422-9c22-4a40-982f-c7fa779e3b2c","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":2,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"5b0da657-2183-42ae-b0f9-595a0f59de17","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":15,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"70bac047-8559-4c72-bc2d-7584df7e4a0b","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":12,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"8843a921-b9bd-4837-ae0b-423d01b2476d","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":14,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"b227df4a-78e1-4624-9f48-589000d26fe9","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":8,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"b6f8a23f-de5a-4ec2-a549-ace73f0e0667","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":9,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"c7aef709-5210-4ff2-a237-be915ad611bb","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":13,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"c86663e2-6b98-4e9f-80c3-3cdea013f816","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":11,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"cddb488f-37ba-42a6-9f2a-09090c2f1c8e","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":3,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"e1d513d2-b165-41a5-9874-b0dc01fef619","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":6,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"e49d6d6e-3e2d-425b-b531-fe761757b773","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":5,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"},{"key":"f2ba9cdb-7efe-413f-981c-e6421861db46","component":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","line":7,"message":"This issue is generated on each line","severity":"MAJOR","rule":"xoo:OneIssuePerLine","status":"OPEN","isNew":false,"creationDate":"2013-05-01T00:00:00+0200"}],"components":[{"key":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1"},{"key":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","path":"src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo","moduleKey":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1","status":"SAME"},{"key":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1:src/main/xoo/com/sonar/it/samples/modules/a1","path":"src/main/xoo/com/sonar/it/samples/modules/a1","moduleKey":"com.sonarsource.it.samples:multi-modules-sample:module_a:module_a1"}],"rules":[{"key":"xoo:OneIssuePerLine","rule":"OneIssuePerLine","repository":"xoo","name":"One Issue Per Line"}],"users":[]} \ No newline at end of file -- 2.39.5