aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2017-08-01 14:43:55 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2017-08-04 14:58:16 +0200
commite6b28c1ba4e7627f4599590ddfcc21875843ce87 (patch)
tree76d3344308689e5f448ba160f3f4bd90649344cf /sonar-scanner-engine/src
parentef26b6a20f4a813079538b60f97dba53dc4108c1 (diff)
downloadsonarqube-e6b28c1ba4e7627f4599590ddfcc21875843ce87.tar.gz
sonarqube-e6b28c1ba4e7627f4599590ddfcc21875843ce87.zip
Store project relative path inside DefaultIndexedFile
Diffstat (limited to 'sonar-scanner-engine/src')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/SensorsExecutor.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ModuleScanContainer.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java7
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputFileBuilder.java16
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStore.java5
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/SensorStrategy.java38
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdSettingsTest.java11
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java19
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/CoverageExclusionsTest.java1
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/SensorsExecutorTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java6
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java24
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java2
13 files changed, 70 insertions, 65 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/SensorsExecutor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/SensorsExecutor.java
index 88158bfb065..78a596dcd92 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/SensorsExecutor.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/phases/SensorsExecutor.java
@@ -28,10 +28,10 @@ import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
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.resources.Project;
import org.sonar.scanner.bootstrap.ScannerExtensionDictionnary;
import org.sonar.scanner.events.EventBus;
-import org.sonar.scanner.sensor.SensorStrategy;
@ScannerSide
public class SensorsExecutor {
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 b949f802e61..b9c7d779257 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
@@ -25,6 +25,7 @@ import org.sonar.api.batch.AnalysisMode;
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.resources.Project;
import org.sonar.api.scan.filesystem.FileExclusions;
@@ -70,7 +71,6 @@ import org.sonar.scanner.scan.filesystem.StatusDetectionFactory;
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.source.HighlightableBuilder;
import org.sonar.scanner.source.SymbolizableBuilder;
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java
index b120e1fd7a8..4b78ce2f6c6 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java
@@ -163,8 +163,7 @@ public class FileIndexer {
private Void indexFile(Path sourceFile, InputFile.Type type, Progress progress) throws IOException {
// get case of real file without resolving link
Path realAbsoluteFile = sourceFile.toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath().normalize();
- String relativePathStr = PathResolver.relativePath(module.getBaseDir(), realAbsoluteFile);
- if (relativePathStr == null) {
+ if (!realAbsoluteFile.startsWith(module.getBaseDir())) {
LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", realAbsoluteFile.toAbsolutePath(), module.getBaseDir());
return null;
}
@@ -178,7 +177,7 @@ public class FileIndexer {
LOG.warn("File '{}' is ignored because it doesn't belong to the forced language '{}'", realAbsoluteFile.toAbsolutePath(), langDetection.forcedLanguage());
return null;
}
- DefaultInputFile inputFile = inputFileBuilder.create(type, relativePathStr, language);
+ DefaultInputFile inputFile = inputFileBuilder.create(type, realAbsoluteFile, language);
if (!accept(inputFile)) {
progress.increaseExcludedByPatternsCount();
return null;
@@ -188,7 +187,7 @@ public class FileIndexer {
indexFileAndParentDir(inputFile, parentRelativePath);
progress.markAsIndexed(inputFile);
}
- LOG.debug("'{}' indexed {}with language '{}'", relativePathStr, type == Type.TEST ? "as test " : "", inputFile.language());
+ LOG.debug("'{}' indexed {}with language '{}'", relativePath, type == Type.TEST ? "as test " : "", inputFile.language());
inputFileBuilder.checkMetadata(inputFile);
return null;
}
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 d8980917519..aee7714e9fc 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
@@ -25,7 +25,10 @@ import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultIndexedFile;
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.batch.fs.internal.SensorStrategy;
import org.sonar.api.config.Configuration;
+import org.sonar.api.utils.PathUtils;
public class InputFileBuilder {
public static final String PRELOAD_FILE_METADATA_KEY = "sonar.preloadFileMetadata";
@@ -35,9 +38,13 @@ public class InputFileBuilder {
private final MetadataGenerator metadataGenerator;
private final boolean preloadMetadata;
private final ModuleFileSystemInitializer moduleFileSystemInitializer;
+ private final Path projectBaseDir;
+ private final SensorStrategy sensorStrategy;
public InputFileBuilder(DefaultInputModule module, MetadataGenerator metadataGenerator,
- BatchIdGenerator idGenerator, Configuration settings, ModuleFileSystemInitializer moduleFileSystemInitializer) {
+ BatchIdGenerator idGenerator, Configuration settings, ModuleFileSystemInitializer moduleFileSystemInitializer, InputModuleHierarchy hierarchy, SensorStrategy sensorStrategy) {
+ this.sensorStrategy = sensorStrategy;
+ this.projectBaseDir = hierarchy.root().getBaseDir();
this.moduleFileSystemInitializer = moduleFileSystemInitializer;
this.moduleKey = module.key();
this.moduleBaseDir = module.getBaseDir();
@@ -46,8 +53,11 @@ public class InputFileBuilder {
this.preloadMetadata = settings.getBoolean(PRELOAD_FILE_METADATA_KEY).orElse(false);
}
- DefaultInputFile create(InputFile.Type type, String relativePath, @Nullable String language) {
- DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type, language, idGenerator.get());
+ DefaultInputFile create(InputFile.Type type, Path absolutePath, @Nullable String language) {
+ DefaultIndexedFile indexedFile = new DefaultIndexedFile(absolutePath, moduleKey,
+ PathUtils.sanitize(projectBaseDir.relativize(absolutePath).toString()),
+ PathUtils.sanitize(moduleBaseDir.relativize(absolutePath).toString()),
+ type, language, idGenerator.get(), sensorStrategy);
DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(f, moduleFileSystemInitializer.defaultEncoding()));
if (language != null) {
inputFile.setPublished(true);
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStore.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStore.java
index cca6b49fb14..56c926c83e4 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStore.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStore.java
@@ -25,7 +25,7 @@ import org.sonar.api.batch.fs.InputDir;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
-import org.sonar.scanner.sensor.SensorStrategy;
+import org.sonar.api.batch.fs.internal.SensorStrategy;
@ScannerSide
public class ModuleInputComponentStore extends DefaultFileSystem.Cache {
@@ -91,7 +91,8 @@ public class ModuleInputComponentStore extends DefaultFileSystem.Cache {
return inputComponentStore.getFilesByName(filename);
}
- @Override public Iterable<InputFile> getFilesByExtension(String extension) {
+ @Override
+ public Iterable<InputFile> getFilesByExtension(String extension) {
return inputComponentStore.getFilesByExtension(extension);
}
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/SensorStrategy.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/SensorStrategy.java
deleted file mode 100644
index ebdba341751..00000000000
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/SensorStrategy.java
+++ /dev/null
@@ -1,38 +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;
-
-/**
- * A shared, mutable object in the module container.
- * It's used during the execution of sensors to decide whether
- * sensors should be executed once for the entire project, or per-module.
- */
-public class SensorStrategy {
-
- private boolean global = false;
-
- public boolean isGlobal() {
- return global;
- }
-
- public void setGlobal(boolean global) {
- this.global = global;
- }
-}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdSettingsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdSettingsTest.java
index c7774c750e9..111ddeebfc5 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdSettingsTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdSettingsTest.java
@@ -19,19 +19,18 @@
*/
package org.sonar.scanner.cpd;
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
import java.util.Optional;
-
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
import org.sonar.api.config.Configuration;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
public class CpdSettingsTest {
private CpdSettings cpdSettings;
private Configuration configuration;
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 a61330e3657..5f2add6955a 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
@@ -712,4 +712,23 @@ public class FileSystemMediumTest {
assertThat(result.inputFiles()).hasSize(1);
}
+
+ @Test
+ public void detectDuplicatedFilesInDifferentModules() throws IOException {
+ File srcDir = new File(baseDir, "module1/src");
+ srcDir.mkdir();
+
+ File xooFile = new File(srcDir, "sample.xoo");
+ FileUtils.write(xooFile, "Sample xoo\ncontent");
+
+ TaskResult result = tester.newTask()
+ .properties(builder
+ .put("sonar.sources", "module1/src")
+ .put("sonar.modules", "module1")
+ .put("module1.sonar.sources", "src")
+ .build())
+ .execute();
+
+ assertThat(result.inputFiles()).hasSize(1);
+ }
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/CoverageExclusionsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/CoverageExclusionsTest.java
index 166964fcdc7..a6d208b35bb 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/CoverageExclusionsTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/CoverageExclusionsTest.java
@@ -26,7 +26,6 @@ import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.core.config.ExclusionProperties;
-import org.sonar.scanner.phases.CoverageExclusions;
import static org.assertj.core.api.Assertions.assertThat;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/SensorsExecutorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/SensorsExecutorTest.java
index 2a68b02aa16..d56d2be32ed 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/SensorsExecutorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/SensorsExecutorTest.java
@@ -30,11 +30,11 @@ import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
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.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.resources.Project;
import org.sonar.scanner.bootstrap.ScannerExtensionDictionnary;
import org.sonar.scanner.events.EventBus;
-import org.sonar.scanner.sensor.SensorStrategy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java
index 24fa9205c37..1b59e69bd18 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectReactorValidatorTest.java
@@ -19,9 +19,6 @@
*/
package org.sonar.scanner.scan;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -32,6 +29,9 @@ import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.utils.MessageException;
import org.sonar.scanner.analysis.DefaultAnalysisMode;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
public class ProjectReactorValidatorTest {
@Rule
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java
index a4c6a32b6dd..6611f15302b 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java
@@ -30,7 +30,9 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.SensorStrategy;
import org.sonar.api.config.internal.MapSettings;
+import org.sonar.scanner.scan.DefaultInputModuleHierarchy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
@@ -44,13 +46,19 @@ public class InputFileBuilderTest {
private Path workDir;
private InputFileBuilder builder;
+ private SensorStrategy sensorStrategy;
+
@Before
public void setUp() throws IOException {
baseDir = temp.newFolder().toPath();
workDir = temp.newFolder().toPath();
- DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create()
+ DefaultInputModule root = new DefaultInputModule(ProjectDefinition.create()
.setBaseDir(baseDir.toFile())
.setWorkDir(workDir.toFile())
+ .setKey("root"), 0);
+ DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create()
+ .setBaseDir(baseDir.resolve("module1").toFile())
+ .setWorkDir(workDir.toFile())
.setKey("module1"), 0);
MetadataGenerator metadataGenerator = mock(MetadataGenerator.class);
@@ -58,17 +66,25 @@ public class InputFileBuilderTest {
MapSettings settings = new MapSettings();
ModuleFileSystemInitializer moduleFileSystemInitializer = mock(ModuleFileSystemInitializer.class);
when(moduleFileSystemInitializer.defaultEncoding()).thenReturn(StandardCharsets.UTF_8);
- builder = new InputFileBuilder(module, metadataGenerator, idGenerator, settings.asConfig(), moduleFileSystemInitializer);
+ sensorStrategy = new SensorStrategy();
+ builder = new InputFileBuilder(module, metadataGenerator, idGenerator, settings.asConfig(), moduleFileSystemInitializer, new DefaultInputModuleHierarchy(root),
+ sensorStrategy);
}
@Test
public void testBuild() {
- Path filePath = baseDir.resolve("src/File1.xoo");
- DefaultInputFile inputFile = builder.create(Type.MAIN, "src/File1.xoo", null);
+ Path filePath = baseDir.resolve("module1/src/File1.xoo");
+ DefaultInputFile inputFile = builder.create(Type.MAIN, filePath, null);
assertThat(inputFile.moduleKey()).isEqualTo("module1");
assertThat(inputFile.absolutePath()).isEqualTo(filePath.toString().replaceAll("\\\\", "/"));
+ assertThat(inputFile.relativePath()).isEqualTo("src/File1.xoo");
+ assertThat(inputFile.path()).isEqualTo(filePath);
assertThat(inputFile.key()).isEqualTo("module1:src/File1.xoo");
assertThat(inputFile.isPublished()).isFalse();
+
+ sensorStrategy.setGlobal(true);
+
+ assertThat(inputFile.relativePath()).isEqualTo("module1/src/File1.xoo");
}
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java
index 4d2e97d0c23..ff1f957bccf 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java
@@ -27,8 +27,8 @@ import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.SensorStrategy;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
-import org.sonar.scanner.sensor.SensorStrategy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;