aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2017-04-07 16:34:00 +0200
committerdbmeneses <duarte.meneses@sonarsource.com>2017-04-10 13:33:47 +0200
commit96390533c5793815af68b167750b78ee5e82b2ff (patch)
tree74e5471d68afed31e6687102b8800bc7a66702f1 /sonar-scanner-engine
parent96ab3bcbc0bb8746afccaee6dbdbac2ec757207c (diff)
downloadsonarqube-96390533c5793815af68b167750b78ee5e82b2ff.tar.gz
sonarqube-96390533c5793815af68b167750b78ee5e82b2ff.zip
SONAR-8904 Make it clear why a file with no blocks is not part of the CPD analysis
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java10
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/index/SonarCpdBlockIndex.java11
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java39
3 files changed, 58 insertions, 2 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java
index 34ff6869167..0b964bb62de 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/CpdExecutor.java
@@ -87,7 +87,11 @@ public class CpdExecutor {
@VisibleForTesting
void execute(long timeout) {
total = index.noResources();
- progressReport.start(String.format("Calculating CPD for %d files", total));
+ int filesWithoutBlocks = index.noIndexedFiles() - total;
+ if (filesWithoutBlocks > 0) {
+ LOG.info("{} {} had no CPD blocks", filesWithoutBlocks, pluralize(filesWithoutBlocks));
+ }
+ progressReport.start(String.format("Calculating CPD for %d %s", total, pluralize(total)));
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
Iterator<ResourceBlocks> it = index.iterator();
@@ -106,6 +110,10 @@ public class CpdExecutor {
}
}
+ private static String pluralize(int files) {
+ return files == 1 ? "file" : "files";
+ }
+
@VisibleForTesting
void runCpdAnalysis(ExecutorService executorService, String componentKey, final Collection<Block> fileBlocks, long timeout) {
DefaultInputComponent component = (DefaultInputComponent) componentStore.getByKey(componentKey);
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/index/SonarCpdBlockIndex.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/index/SonarCpdBlockIndex.java
index 052d5df17b3..84c7d92371f 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/index/SonarCpdBlockIndex.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/cpd/index/SonarCpdBlockIndex.java
@@ -29,6 +29,8 @@ import org.sonar.api.CoreProperties;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.config.Settings;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.block.ByteArray;
import org.sonar.duplications.index.AbstractCloneIndex;
@@ -40,7 +42,7 @@ import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.report.ReportPublisher;
public class SonarCpdBlockIndex extends AbstractCloneIndex {
-
+ private static final Logger LOG = Loggers.get(SonarCpdBlockIndex.class);
private final CloneIndex mem = new PackedMemoryCloneIndex();
private final ReportPublisher publisher;
private final Settings settings;
@@ -72,9 +74,16 @@ public class SonarCpdBlockIndex extends AbstractCloneIndex {
for (Block block : blocks) {
mem.insert(block);
}
+ if (blocks.isEmpty()) {
+ LOG.debug("Not enough content in '{}' to have CPD blocks, it will not be part of the duplication detection", inputFile.relativePath());
+ }
indexedFiles.add(inputFile);
}
+ public int noIndexedFiles() {
+ return indexedFiles.size();
+ }
+
public boolean isIndexed(InputFile inputFile) {
return indexedFiles.contains(inputFile);
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java
index 52ee6b58ac5..f75bd498bc3 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java
@@ -37,6 +37,7 @@ import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.scanner.mediumtest.LogOutputRecorder;
import org.sonar.scanner.mediumtest.ScannerMediumTester;
import org.sonar.scanner.mediumtest.TaskResult;
import org.sonar.scanner.protocol.output.ScannerReport;
@@ -62,12 +63,15 @@ public class CpdMediumTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
+ private LogOutputRecorder logRecorder = new LogOutputRecorder();
+
public ScannerMediumTester tester = ScannerMediumTester.builder()
.registerPlugin("xoo", new XooPlugin())
.addDefaultQProfile("xoo", "Sonar Way")
.addRules(new XooRulesDefinition())
// active a rule just to be sure that xoo files are published
.addActiveRule("xoo", "xoo:OneIssuePerFile", null, "One Issue Per File", null, null, null)
+ .setLogOutput(logRecorder)
.build();
private File baseDir;
@@ -82,6 +86,7 @@ public class CpdMediumTest {
@Before
public void prepare() throws IOException {
+ logRecorder.getAll().clear();
tester.start();
baseDir = temp.getRoot();
@@ -220,6 +225,40 @@ public class CpdMediumTest {
}
@Test
+ public void testFilesWithoutBlocks() throws IOException {
+ File srcDir = new File(baseDir, "src");
+ srcDir.mkdir();
+
+ String file1 = "Sample xoo\ncontent\n"
+ + "foo\nbar\ntoto\ntiti\n"
+ + "foo\nbar\ntoto\ntiti\n"
+ + "bar\ntoto\ntiti\n"
+ + "foo\nbar\ntoto\ntiti";
+
+ String file2 = "string\n";
+
+ File xooFile1 = new File(srcDir, "sample1.xoo");
+ FileUtils.write(xooFile1, file1);
+
+ File xooFile2 = new File(srcDir, "sample2.xoo");
+ FileUtils.write(xooFile2, file2);
+
+ TaskResult result = tester.newTask()
+ .properties(builder
+ .put("sonar.sources", "src")
+ .put("sonar.cpd.xoo.minimumTokens", "10")
+ .put("sonar.verbose", "true")
+ .build())
+ .start();
+
+ assertThat(result.inputFiles()).hasSize(2);
+
+ assertThat(logRecorder.getAllAsString()).contains("Not enough content in 'src/sample2.xoo' to have CPD blocks");
+ assertThat(logRecorder.getAllAsString()).contains("1 file had no CPD blocks");
+
+ }
+
+ @Test
public void testExclusions() throws IOException {
File srcDir = new File(baseDir, "src");
srcDir.mkdir();