]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3677 fix re-indexation of files
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 10 Oct 2013 12:04:55 +0000 (14:04 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 10 Oct 2013 12:04:55 +0000 (14:04 +0200)
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileCache.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileCacheTest.java

index f27abed0145fa2164d4e5a5aafa14b6cbbde86ea..944bf3d973f8b0e44a9c5a23fd2b65943a27abd7 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.api.scan.filesystem.ModuleFileSystem;
 import org.sonar.api.scan.filesystem.PathResolver;
 import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
 
+import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import java.io.File;
 import java.nio.charset.Charset;
@@ -84,7 +85,7 @@ public class FileIndex implements BatchComponent {
     logger.info("Index files");
     // TODO log configuration too (replace FileSystemLogger)
 
-    IndexStatus status = new IndexStatus(cache.filePathsOfModule(fileSystem.moduleKey()));
+    IndexStatus status = new IndexStatus(cache.fileRelativePaths(fileSystem.moduleKey()));
 
     for (File sourceDir : fileSystem.sourceDirs()) {
       indexDirectory(fileSystem, status, sourceDir, InputFile.TYPE_SOURCE);
@@ -111,31 +112,34 @@ public class FileIndex implements BatchComponent {
     for (File file : files) {
       String relativePath = pathResolver.relativePath(fileSystem.baseDir(), file);
       if (!cache.containsFile(fileSystem.moduleKey(), relativePath)) {
-        InputFile input = newInputFile(fileSystem, sourceDir, type, file);
-        if (accept(input)) {
+        InputFile input = newInputFile(fileSystem, sourceDir, type, file, relativePath);
+        if (input != null && accept(input)) {
           cache.put(fileSystem.moduleKey(), input);
-          status.markAsIndexed(relativePath);
         }
       }
+      status.markAsIndexed(relativePath);
     }
   }
 
+  @CheckForNull
+  private InputFile newInputFile(ModuleFileSystem fileSystem, File sourceDir, String type, File file, String baseRelativePath) {
+    // File extension must be kept case-sensitive
+    String extension = FilenameUtils.getExtension(file.getName());
+    String lang = languageRecognizer.ofExtension(extension);
+    if (lang == null) {
+      return null;
+    }
 
-  private InputFile newInputFile(ModuleFileSystem fileSystem, File sourceDir, String type, File file) {
     try {
       Map<String, String> attributes = Maps.newHashMap();
+      set(attributes, InputFile.ATTRIBUTE_EXTENSION, extension);
       set(attributes, InputFile.ATTRIBUTE_TYPE, type);
+      set(attributes, InputFile.ATTRIBUTE_LANGUAGE, lang);
 
       // paths
-      String baseRelativePath = pathResolver.relativePath(fileSystem.baseDir(), file);
       set(attributes, InputFile.ATTRIBUTE_SOURCEDIR_PATH, FilenameUtils.normalize(sourceDir.getCanonicalPath(), true));
       set(attributes, InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, pathResolver.relativePath(sourceDir, file));
 
-      // File extension must be kept case-sensitive
-      String extension = FilenameUtils.getExtension(file.getName());
-      set(attributes, InputFile.ATTRIBUTE_EXTENSION, extension);
-      set(attributes, InputFile.ATTRIBUTE_LANGUAGE, languageRecognizer.ofExtension(extension));
-
       // hash + status
       initStatus(file, fileSystem.sourceCharset(), baseRelativePath, attributes);
 
index 75be9ad46431af287777ea6e3b25738b1e56f9db..bdae94fb996aa890e35b521f199e72a5de7e0193 100644 (file)
@@ -57,7 +57,7 @@ public class InputFileCache implements BatchComponent {
     return cache.allValues();
   }
 
-  public Set<String> filePathsOfModule(String moduleKey) {
+  public Set<String> fileRelativePaths(String moduleKey) {
     return cache.keySet(moduleKey);
   }
 
@@ -66,7 +66,7 @@ public class InputFileCache implements BatchComponent {
   }
 
   public InputFileCache put(String moduleKey, InputFile file) {
-    cache.put(moduleKey, file.path(), file);
+    cache.put(moduleKey, file.relativePath(), file);
     return this;
   }
 }
index 3aca556e9c18e5efd7100b444409c7c9770200fa..3b576e8a3fcaa279218e8830ff6789e1b2417594 100644 (file)
@@ -25,6 +25,7 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
+import org.sonar.api.scan.filesystem.InputFile;
 import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
 import org.sonar.batch.index.Caches;
 
@@ -56,6 +57,10 @@ public class InputFileCacheTest {
     assertThat(cache.byModule("struts")).hasSize(1);
     assertThat(cache.byModule("struts-core")).hasSize(1);
     assertThat(cache.all()).hasSize(2);
+    for (InputFile inputFile : cache.all()) {
+      assertThat(inputFile.relativePath()).startsWith("src/main/java");
+    }
+    assertThat(cache.fileRelativePaths("struts-core")).containsOnly("src/main/java/Foo.java");
 
     cache.removeModule("struts");
     assertThat(cache.byModule("struts")).hasSize(0);