From: Said Tahsin Dane Date: Thu, 5 Mar 2020 21:14:08 +0000 (-0600) Subject: Do not warn about a file out of basedir if file is not included X-Git-Tag: 8.3.0.34182~154 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ea65d148b2fc1b28b055dc58760b8966e406028f;p=sonarqube.git Do not warn about a file out of basedir if file is not included --- 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 2ccee4d1528..74dbe5bec00 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 @@ -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); 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 bb62d3454e9..7d2bb6cc9fb 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 @@ -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");