Skip to content

Commit

Permalink
SCANMAVEN-215 scanAll exludes external report files (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
alban-auzeill authored May 15, 2024
1 parent 990c59d commit d2c79c1
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.execution.MavenSession;
Expand All @@ -49,6 +50,7 @@ public class ScannerBootstrapper {

static final String UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE = "With SonarQube server prior to 5.6, use sonar-maven-plugin <= 3.3";
private static final String SONARCLOUD_HOST_URL = "https://sonarcloud.io";
private static final Pattern REPORT_PROPERTY_PATTERN = Pattern.compile("^sonar\\..*[rR]eportPaths?$");

private final Log log;
private final MavenSession session;
Expand Down Expand Up @@ -147,6 +149,18 @@ private static String notCollectingAdditionalSourcesBecauseOf(String overriddenP
"the scanner will not collect additional sources because " + overriddenProperty + " has been overridden.";
}

private static Set<Path> excludedReportFiles(Map<String, String> props) {
return props.keySet().stream()
.filter(key -> REPORT_PROPERTY_PATTERN.matcher(key).matches())
.map(props::get)
.map(MavenUtils::splitAsCsv)
.flatMap(List::stream)
.map(Paths::get)
.map(Path::toAbsolutePath)
.map(Path::normalize)
.collect(Collectors.toSet());
}

@VisibleForTesting
void collectAllSources(Map<String, String> props) {
String projectBasedir = props.get(ScanProperties.PROJECT_BASEDIR);
Expand All @@ -164,7 +178,9 @@ void collectAllSources(Map<String, String> props) {
Set<Path> existingSources = coveredSources.stream()
.map(Paths::get)
.collect(Collectors.toSet());
SourceCollector visitor = new SourceCollector(existingSources, mavenProjectConverter.getSkippedBasedDirs());
Set<Path> excludedFiles = excludedReportFiles(props);

SourceCollector visitor = new SourceCollector(existingSources, mavenProjectConverter.getSkippedBasedDirs(), excludedFiles);
Files.walkFileTree(Paths.get(projectBasedir), visitor);
collectedSources = visitor.getCollectedSources().stream()
.map(file -> file.toAbsolutePath().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,18 @@ public class SourceCollector implements FileVisitor<Path> {
);
private final Set<Path> existingSources;
private final Set<Path> directoriesToIgnore;
private final Set<Path> excludedFiles;

public Set<Path> getCollectedSources() {
return collectedSources;
}

private final Set<Path> collectedSources = new HashSet<>();

public SourceCollector(Set<Path> existingSources, Set<Path> directoriesToIgnore) {
public SourceCollector(Set<Path> existingSources, Set<Path> directoriesToIgnore, Set<Path> excludedFiles) {
this.existingSources = existingSources;
this.directoriesToIgnore = directoriesToIgnore;
this.excludedFiles = excludedFiles;
}
@Override
public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes basicFileAttributes) throws IOException {
Expand Down Expand Up @@ -119,6 +121,7 @@ private boolean isCoveredByExistingSources(Path path) {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) {
if (
!excludedFiles.contains(path) &&
EXCLUDED_EXTENSIONS.stream().noneMatch(ext -> path.toString().endsWith(ext)) &&
existingSources.stream().noneMatch(path::equals)
) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/sonarsource/scanner/maven/SonarQubeMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.io.IOUtils;
import org.apache.maven.execution.ProjectDependencyGraph;
import org.apache.maven.graph.GraphBuilder;
Expand Down Expand Up @@ -124,6 +127,25 @@ public void reuse_findbugs_exclusions_from_reporting() throws IOException, Excep
assertPropsContains(entry("sonar.findbugs.excludeFilters", new File(baseDir, "findbugs-exclude.xml").getAbsolutePath()));
}

@Test
public void exclude_report_paths_from_scanAll() throws Exception {
File projectBarDir = executeProject("project-with-external-reports", "sonar.maven.scanAll", "true");

String sources = readProps("target/dump.properties")
.entrySet()
.stream()
.filter(e -> e.getKey().toString().equals("sonar.sources"))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);

Set<String> actualListOfSources = Arrays.stream(sources.split(","))
.map(file -> file.replace(projectBarDir.toString(), "").replace("\\", "/"))
.collect(Collectors.toSet());

assertThat(actualListOfSources).containsExactlyInAnyOrder("/other.xml", "/pom.xml");
}

@Test
public void reuse_findbugs_exclusions_from_plugin() throws IOException, Exception {
File baseDir = executeProject("project-with-findbugs-build");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void setup() throws IOException {
void testPrevisitDirectories() throws IOException {
Path srcMainJava = Paths.get("src", "main", "java");
Set<Path> existingSources = Collections.singleton(srcMainJava);
FileVisitor<Path> visitor = new SourceCollector(existingSources, Collections.emptySet());
FileVisitor<Path> visitor = new SourceCollector(existingSources, Collections.emptySet(), Collections.emptySet());


Path gitFolder = Paths.get(".git");
Expand All @@ -82,15 +82,15 @@ void testPrevisitDirectories() throws IOException {
@Test
void visitorCollectsConsistently() throws IOException {
// File in the existing source is not repeated in the collected files
SourceCollector visitor = new SourceCollector(Collections.emptySet(), Collections.emptySet());
SourceCollector visitor = new SourceCollector(Collections.emptySet(), Collections.emptySet(), Collections.emptySet());
Files.walkFileTree(emptyProjectBasedir, visitor);
assertThat(visitor.getCollectedSources()).isEmpty();

SourceCollector otherVisitor = new SourceCollector(Collections.emptySet(), Collections.emptySet());
SourceCollector otherVisitor = new SourceCollector(Collections.emptySet(), Collections.emptySet(), Collections.emptySet());
Files.walkFileTree(singleFileProjectBaseDir, otherVisitor);
assertThat(otherVisitor.getCollectedSources()).containsOnly(singleFileProjectBaseDir.resolve("pom.xml"));

SourceCollector visitorAvoidingPomXml = new SourceCollector(Collections.singleton(singleFileProjectBaseDir.resolve("pom.xml")), Collections.emptySet());
SourceCollector visitorAvoidingPomXml = new SourceCollector(Collections.singleton(singleFileProjectBaseDir.resolve("pom.xml")), Collections.emptySet(), Collections.emptySet());
Files.walkFileTree(singleFileProjectBaseDir, visitorAvoidingPomXml);
assertThat(visitorAvoidingPomXml.getCollectedSources()).isEmpty();
}
Expand All @@ -104,8 +104,8 @@ void visitorIgnoresFilesInDirectoriesToIgnore() throws IOException {
Path fileInSubModule = subModule.resolve("ignore-me.php");
fileInSubModule.toFile().createNewFile();

SourceCollector visitor = new SourceCollector(Collections.emptySet(), Collections.singleton(subModule));
SourceCollector visitor = new SourceCollector(Collections.emptySet(), Collections.singleton(subModule), Collections.emptySet());
Files.walkFileTree(simpleProjectBasedDir, visitor);
assertThat(visitor.getCollectedSources()).doesNotContain(fileInSubModule);
}
}
}
1 change: 1 addition & 0 deletions src/test/projects/project-with-external-reports/other.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root></root>
17 changes: 17 additions & 0 deletions src/test/projects/project-with-external-reports/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.codehaus.sonar</groupId>
<artifactId>project-with-external-reports</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<sonar.scanner.dumpToFile>target/dump.properties</sonar.scanner.dumpToFile>
<sonar.foo.reportPath>src/test/projects/project-with-external-reports/report1.xml</sonar.foo.reportPath>
<sonar.foo.xmlReportPaths>src/test/projects/project-with-external-reports/report2.xml</sonar.foo.xmlReportPaths>
<sonar.java.checkstyle.reportPaths>src/test/projects/project-with-external-reports/report3.xml,src/test/projects/project-with-external-reports/subdir/report4.xml</sonar.java.checkstyle.reportPaths>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root></root>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root></root>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root></root>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root></root>

0 comments on commit d2c79c1

Please sign in to comment.