diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-01 16:31:07 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2017-08-04 14:58:16 +0200 |
commit | ac7edbe5890ae7921593dda7520157dca652860d (patch) | |
tree | f08f51e73ae249e74c4e9a4a33ef6b004fe177f4 /sonar-scanner-engine | |
parent | e6b28c1ba4e7627f4599590ddfcc21875843ce87 (diff) | |
download | sonarqube-ac7edbe5890ae7921593dda7520157dca652860d.tar.gz sonarqube-ac7edbe5890ae7921593dda7520157dca652860d.zip |
SONAR-9574 Same file indexed in different modules
Diffstat (limited to 'sonar-scanner-engine')
6 files changed, 39 insertions, 40 deletions
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 4b78ce2f6c6..33cd3749c1b 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 @@ -31,9 +31,7 @@ import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Collections; -import java.util.HashSet; import java.util.List; -import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -43,7 +41,6 @@ import java.util.concurrent.atomic.AtomicInteger; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.batch.ScannerSide; -import org.sonar.api.batch.fs.IndexedFile; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Type; import org.sonar.api.batch.fs.InputFileFilter; @@ -184,8 +181,8 @@ public class FileIndexer { } String parentRelativePath = getParentRelativePath(inputFile); synchronized (this) { - indexFileAndParentDir(inputFile, parentRelativePath); progress.markAsIndexed(inputFile); + indexFileAndParentDir(inputFile, parentRelativePath); } LOG.debug("'{}' indexed {}with language '{}'", relativePath, type == Type.TEST ? "as test " : "", inputFile.language()); inputFileBuilder.checkMetadata(inputFile); @@ -271,16 +268,16 @@ public class FileIndexer { } private class Progress { - private final Set<Path> indexed = new HashSet<>(); + private AtomicInteger indexedCount = new AtomicInteger(0); private AtomicInteger excludedByPatternsCount = new AtomicInteger(0); - void markAsIndexed(IndexedFile inputFile) { - if (indexed.contains(inputFile.path())) { + void markAsIndexed(DefaultInputFile inputFile) { + if (componentStore.getFile(inputFile.getProjectRelativePath()) != null) { throw MessageException.of("File " + inputFile + " can't be indexed twice. Please check that inclusion/exclusion patterns produce " + "disjoint sets for main and test files"); } - indexed.add(inputFile.path()); - progressReport.message(indexed.size() + " " + pluralizeFiles(indexed.size()) + " indexed... (last one was " + inputFile.relativePath() + ")"); + int count = indexedCount.incrementAndGet(); + progressReport.message(count + " " + pluralizeFiles(count) + " indexed... (last one was " + inputFile.relativePath() + ")"); } void increaseExcludedByPatternsCount() { @@ -292,7 +289,7 @@ public class FileIndexer { } int count() { - return indexed.size(); + return indexedCount.get(); } } 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 bec0e176f10..ee1b34a8d95 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 @@ -110,7 +110,7 @@ public class InputComponentStore { public InputComponentStore remove(InputFile inputFile) { DefaultInputFile file = (DefaultInputFile) inputFile; - inputFileCache.remove(file.moduleKey(), inputFile.relativePath()); + inputFileCache.remove(file.moduleKey(), file.getModuleRelativePath()); return this; } @@ -123,8 +123,8 @@ public class InputComponentStore { public InputComponentStore put(InputFile inputFile) { DefaultInputFile file = (DefaultInputFile) inputFile; addToLanguageCache(file); - inputFileCache.put(file.moduleKey(), inputFile.relativePath(), inputFile); - globalInputFileCache.put(getProjectRelativePath(file), inputFile); + inputFileCache.put(file.moduleKey(), file.getModuleRelativePath(), inputFile); + globalInputFileCache.put(file.getProjectRelativePath(), inputFile); inputComponents.put(inputFile.key(), inputFile); filesByNameCache.put(inputFile.filename(), inputFile); filesByExtensionCache.put(FileExtensionPredicate.getExtension(inputFile), inputFile); @@ -142,15 +142,12 @@ public class InputComponentStore { public InputComponentStore put(InputDir inputDir) { DefaultInputDir dir = (DefaultInputDir) inputDir; inputDirCache.put(dir.moduleKey(), inputDir.relativePath(), inputDir); + // FIXME an InputDir can be already indexed by another module globalInputDirCache.put(getProjectRelativePath(dir), inputDir); inputComponents.put(inputDir.key(), inputDir); return this; } - private String getProjectRelativePath(DefaultInputFile file) { - return PathResolver.relativePath(getProjectBaseDir(), file.path()); - } - private String getProjectRelativePath(DefaultInputDir dir) { return PathResolver.relativePath(getProjectBaseDir(), dir.path()); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java index 76e59e815f4..f3a47b46ec8 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java @@ -95,13 +95,13 @@ public class DefaultFileLinesContextTest { @Test public void validateLineGreaterThanZero() { - thrown.expectMessage("Line number should be positive for file [moduleKey=foo, relative=src/foo.php, basedir=foo]."); + thrown.expectMessage("Line number should be positive for file src/foo.php."); fileLineMeasures.setIntValue(HITS_METRIC_KEY, 0, 2); } @Test public void validateLineLowerThanLineCount() { - thrown.expectMessage("Line 4 is out of range for file [moduleKey=foo, relative=src/foo.php, basedir=foo]. File has 3 lines"); + thrown.expectMessage("Line 4 is out of range for file src/foo.php. File has 3 lines"); fileLineMeasures.setIntValue(HITS_METRIC_KEY, 4, 2); } 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 5f2add6955a..a48b94bf2a1 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 @@ -562,12 +562,32 @@ public class FileSystemMediumTest { FileUtils.write(xooFile, "Sample xoo\ncontent"); thrown.expect(MessageException.class); - thrown.expectMessage("can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files"); + thrown.expectMessage("File src/sample.xoo can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files"); tester.newTask() .properties(builder .put("sonar.sources", "src,src/sample.xoo") .build()) .execute(); + } + + // SONAR-9574 + @Test + public void failForDuplicateInputFileInDifferentModules() throws IOException { + File srcDir = new File(baseDir, "module1/src"); + srcDir.mkdir(); + + File xooFile = new File(srcDir, "sample.xoo"); + FileUtils.write(xooFile, "Sample xoo\ncontent"); + + thrown.expect(MessageException.class); + thrown.expectMessage("File module1/src/sample.xoo can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files"); + tester.newTask() + .properties(builder + .put("sonar.sources", "module1/src") + .put("sonar.modules", "module1") + .put("module1.sonar.sources", "src") + .build()) + .execute(); } @@ -713,22 +733,4 @@ 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/mediumtest/fs/ProjectBuilderMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java index 35eab16b110..bf86ea126c1 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java @@ -190,7 +190,7 @@ public class ProjectBuilderMediumTest { } private File prepareProject() throws IOException { - File baseDir = temp.getRoot(); + File baseDir = temp.newFolder(); File module1Dir = new File(baseDir, "module1"); module1Dir.mkdir(); 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 6611f15302b..482b215cee0 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 @@ -21,6 +21,7 @@ package org.sonar.scanner.scan.filesystem; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import org.junit.Before; import org.junit.Rule; @@ -56,8 +57,10 @@ public class InputFileBuilderTest { .setBaseDir(baseDir.toFile()) .setWorkDir(workDir.toFile()) .setKey("root"), 0); + Path moduleBaseDir = baseDir.resolve("module1"); + Files.createDirectories(moduleBaseDir); DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create() - .setBaseDir(baseDir.resolve("module1").toFile()) + .setBaseDir(moduleBaseDir.toFile()) .setWorkDir(workDir.toFile()) .setKey("module1"), 0); |