]> source.dussan.org Git - sonarqube.git/commitdiff
Do not warn about a file out of basedir if file is not included
authorSaid Tahsin Dane <tasomaniac@gmail.com>
Thu, 5 Mar 2020 21:14:08 +0000 (15:14 -0600)
committersonartech <sonartech@sonarsource.com>
Fri, 6 Mar 2020 20:04:32 +0000 (20:04 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java

index 2ccee4d1528841c3e9214674454ecc86a31a6136..74dbe5bec00d6c5c817f3a29e66933157342e348 100644 (file)
@@ -98,14 +98,6 @@ public class FileIndexer {
     throws IOException {
     // get case of real file without resolving link
     Path realAbsoluteFile = sourceFile.toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath().normalize();
-    if (!realAbsoluteFile.startsWith(project.getBaseDir())) {
-      LOG.warn("File '{}' is ignored. It is not located in project basedir '{}'.", realAbsoluteFile.toAbsolutePath(), project.getBaseDir());
-      return;
-    }
-    if (!realAbsoluteFile.startsWith(module.getBaseDir())) {
-      LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", realAbsoluteFile.toAbsolutePath(), module.getBaseDir());
-      return;
-    }
     Path projectRelativePath = project.getBaseDir().relativize(realAbsoluteFile);
     Path moduleRelativePath = module.getBaseDir().relativize(realAbsoluteFile);
     boolean included = evaluateInclusionsFilters(moduleExclusionFilters, realAbsoluteFile, projectRelativePath, moduleRelativePath, type);
@@ -118,6 +110,14 @@ public class FileIndexer {
       exclusionCounter.increaseByPatternsCount();
       return;
     }
+    if (!realAbsoluteFile.startsWith(project.getBaseDir())) {
+      LOG.warn("File '{}' is ignored. It is not located in project basedir '{}'.", realAbsoluteFile.toAbsolutePath(), project.getBaseDir());
+      return;
+    }
+    if (!realAbsoluteFile.startsWith(module.getBaseDir())) {
+      LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", realAbsoluteFile.toAbsolutePath(), module.getBaseDir());
+      return;
+    }
 
     String language = langDetection.language(realAbsoluteFile, projectRelativePath);
 
index bb62d3454e910700ea3bde238b6cca45d3b72799..7d2bb6cc9fbc79d764bcab32c1f5f4c1d86e203a 100644 (file)
@@ -167,8 +167,7 @@ public class FileSystemMediumTest {
 
     assertThat(logTester.logs()).contains("2 files indexed");
     assertThat(logTester.logs()).contains("'src/sample.xoo' generated metadata with charset 'UTF-8'");
-    assertThat(logTester.logs().stream().collect(joining("\n"))).doesNotContain("'src/sample.java' generated metadata");
-
+    assertThat(String.join("\n", logTester.logs())).doesNotContain("'src/sample.java' generated metadata");
   }
 
   @Test
@@ -225,8 +224,8 @@ public class FileSystemMediumTest {
 
     assertThat(logTester.logs()).contains("3 files indexed");
     assertThat(logTester.logs()).contains("'src/main/sample.xoo' generated metadata with charset 'UTF-8'");
-    assertThat(logTester.logs().stream().collect(joining("\n"))).doesNotContain("'src/main/sample.java' generated metadata");
-    assertThat(logTester.logs().stream().collect(joining("\n"))).doesNotContain("'src/test/sample.java' generated metadata");
+    assertThat(String.join("\n", logTester.logs())).doesNotContain("'src/main/sample.java' generated metadata");
+    assertThat(String.join("\n", logTester.logs())).doesNotContain("'src/test/sample.java' generated metadata");
     DefaultInputFile javaInputFile = (DefaultInputFile) result.inputFile("src/main/sample.java");
 
     thrown.expect(IllegalStateException.class);
@@ -1029,6 +1028,29 @@ public class FileSystemMediumTest {
     assertThat(logTester.logs(LoggerLevel.WARN)).contains("File '" + xooFile2.getAbsolutePath() + "' is ignored. It is not located in project basedir '" + baseDir + "'.");
   }
 
+  @Test
+  public void dont_log_warn_about_files_out_of_basedir_if_they_arent_included() throws IOException {
+    File srcDir = new File(baseDir, "src");
+    srcDir.mkdir();
+
+    File xooFile = new File(srcDir, "sample1.xoo");
+    FileUtils.write(xooFile, "Sample xoo\ncontent", StandardCharsets.UTF_8);
+
+    File outsideBaseDir = temp.newFolder().getCanonicalFile();
+    File xooFile2 = new File(outsideBaseDir, "another.xoo");
+    FileUtils.write(xooFile2, "Sample xoo 2\ncontent", StandardCharsets.UTF_8);
+
+    AnalysisResult result = tester.newAnalysis()
+      .properties(builder
+        .put("sonar.sources", "src," + PathUtils.canonicalPath(xooFile2))
+        .put("sonar.inclusions", "**/sample1.xoo")
+        .build())
+      .execute();
+
+    assertThat(result.inputFiles()).hasSize(1);
+    assertThat(logTester.logs(LoggerLevel.WARN)).doesNotContain("File '" + xooFile2.getAbsolutePath() + "' is ignored. It is not located in project basedir '" + baseDir + "'.");
+  }
+
   @Test
   public void ignore_files_outside_module_basedir() throws IOException {
     File moduleA = new File(baseDir, "moduleA");