Skip to content

Commit

Permalink
SCANGRADLE-143 The scanner for Gradle avoids symbolic links when coll…
Browse files Browse the repository at this point in the history
…ecting files outside of conventional sonar.sources (#230)
  • Loading branch information
alban-auzeill authored Jun 24, 2024
1 parent 898dee3 commit ea9c7de
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/sonarqube/gradle/SourceCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private boolean isCoveredByExistingSources(Path path) {

@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) {
if (!excludedFiles.contains(path) && existingSources.stream().noneMatch(path::equals)) {
if (!basicFileAttributes.isSymbolicLink() && !excludedFiles.contains(path) && existingSources.stream().noneMatch(path::equals)) {
String lowerCaseFileName = path.getFileName().toString().toLowerCase(Locale.ROOT);
if (excludedExtensions.stream().noneMatch(lowerCaseFileName::endsWith)) {
collectedSources.add(path);
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/org/sonarqube/gradle/SourceCollectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,32 @@ void visitorIgnoresJavaAndKotlinFiles() throws IOException {
.doesNotContain(rootJavaFile)
.doesNotContain(rootKotlinFile);
}

@Test
void visitorIgnoresExcludedFiles() throws IOException {
Path pythonScript = simpleProjectBasedDir.resolve("run.py");
pythonScript.toFile().createNewFile();
Path cppFile = simpleProjectBasedDir.resolve("hello.cpp");
cppFile.toFile().createNewFile();

SourceCollector visitor = new SourceCollector(Collections.emptySet(), Collections.emptySet(), Set.of(cppFile), false);
Files.walkFileTree(simpleProjectBasedDir, visitor);
assertThat(visitor.getCollectedSources())
.contains(pythonScript)
.doesNotContain(cppFile);
}

@Test
void visitorIgnoresSymbolicLinks() throws IOException {
Path simpleProjectPom = simpleProjectBasedDir.resolve("pom.xml");
simpleProjectPom.toFile().createNewFile();
Path link = simpleProjectBasedDir.resolve("pom.xml.symbolic.link");
Files.createSymbolicLink(link, simpleProjectPom);

SourceCollector visitor = new SourceCollector(Collections.emptySet(), Collections.emptySet(), Collections.emptySet(), false);
Files.walkFileTree(simpleProjectBasedDir, visitor);
assertThat(visitor.getCollectedSources())
.contains(simpleProjectPom)
.doesNotContain(link);
}
}

0 comments on commit ea9c7de

Please sign in to comment.