Skip to content

Commit

Permalink
Make sonar.maven.scanAll default to true
Browse files Browse the repository at this point in the history
  • Loading branch information
ADarko22 committed May 14, 2024
1 parent 09625de commit 741a573
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Map<String, String> collectProperties()
Properties userProperties = session.getUserProperties();
Map<String, String> props = mavenProjectConverter.configure(sortedProjects, topLevelProject, userProperties);
props.putAll(propertyDecryptor.decryptProperties(props));

if (shouldCollectAllSources(userProperties)) {
log.info("Parameter " + MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES + " is enabled. The scanner will attempt to collect additional sources.");
if (mavenProjectConverter.isSourceDirsOverridden()) {
Expand All @@ -137,7 +138,8 @@ Map<String, String> collectProperties()
}

private static boolean shouldCollectAllSources(Properties userProperties) {
return Boolean.TRUE.equals(Boolean.parseBoolean(userProperties.getProperty(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES)));
String sonarScanAll = userProperties.getProperty(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, Boolean.TRUE.toString());
return Boolean.TRUE.equals(Boolean.parseBoolean(sonarScanAll));
}

private static String notCollectingAdditionalSourcesBecauseOf(String overriddenProperty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,37 +153,46 @@ void testNullServerVersion() {
}

@Test
void scanAll_property_is_detected_and_applied() throws MojoExecutionException {
void scanAll_property_is_applied_by_default() throws MojoExecutionException {
// When sonar.scanner.scanAll is not set
Map<String, String> collectedProperties = scannerBootstrapper.collectProperties();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
String[] sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
assertThat(sourceDirs).hasSize(2);
assertThat(sourceDirs).hasSize(3);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
verify(log, never()).info("Parameter sonar.maven.scanAll is enabled. The scanner will attempt to collect additional sources.");
verify(scannerBootstrapper, never()).collectAllSources(any());
assertThat(sourceDirs[2]).endsWith(Paths.get("src", "main", "resources", "index.js").toString());
verify(log, times(1)).info("Parameter sonar.maven.scanAll is enabled. The scanner will attempt to collect additional sources.");
verify(scannerBootstrapper, times(1)).collectAllSources(any());
}

@Test
void scanAll_property_is_not_applied_when_set_explicitly() throws MojoExecutionException {
// When sonar.scanner.scanAll is set explicitly to false
Properties withScanAllSetToFalse = new Properties();
withScanAllSetToFalse.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "false");
when(session.getUserProperties()).thenReturn(withScanAllSetToFalse);
collectedProperties = scannerBootstrapper.collectProperties();

Map<String, String> collectedProperties = scannerBootstrapper.collectProperties();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
String[] sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
assertThat(sourceDirs).hasSize(2);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
verify(log, never()).info("Parameter sonar.maven.scanAll is enabled. The scanner will attempt to collect additional sources.");
verify(scannerBootstrapper, never()).collectAllSources(any());
}

@Test
void scanAll_property_is_applied_when_set_explicitly() throws MojoExecutionException {
// When sonar.scanner.scanAll is set explicitly to true
Properties withScanAllSetToTrue = new Properties();
withScanAllSetToTrue.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "true");
when(session.getUserProperties()).thenReturn(withScanAllSetToTrue);
collectedProperties = scannerBootstrapper.collectProperties();

Map<String, String> collectedProperties = scannerBootstrapper.collectProperties();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
String[] sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
assertThat(sourceDirs).hasSize(3);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
Expand All @@ -198,6 +207,7 @@ void should_not_collect_all_sources_when_sonar_sources_is_overridden() throws Mo
Properties withScanAllSetToTrue = new Properties();
withScanAllSetToTrue.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "true");
when(session.getUserProperties()).thenReturn(withScanAllSetToTrue);

// Return the expected directory and notify of overriding
projectProperties.put(ScanProperties.PROJECT_SOURCE_DIRS, Paths.get("src", "main", "resources").toFile().toString());
when(mavenProjectConverter.isSourceDirsOverridden()).thenReturn(true);
Expand Down

0 comments on commit 741a573

Please sign in to comment.