Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCANMAVEN-220: Enable sonar.scanner.scanAll by default #219

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions its/src/test/java/com/sonar/maven/it/suite/MavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ void shouldSupportJeeProjects() {
ORCHESTRATOR.executeBuild(build);

// src/main/webapp is analyzed by web and xml plugin
// including resources, so one more file (ejb-module/src/main/resources/META-INF/ejb-jar.xml)
assertThat(getMeasureAsInteger("com.sonarsource.it.samples.jee:parent", "files")).isEqualTo(9);
// scanning all files, so 2 more files (ejb-module/src/main/resources/META-INF/ejb-jar.xml and web-module/src/main/webapp/index.jsp)
assertThat(getMeasureAsInteger("com.sonarsource.it.samples.jee:parent", "files")).isEqualTo(10);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/it/java-multi-module/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ def sources = 'sonar.sources'
def module1Sources = "org.codehaus.sonar:sample-project-module1.$sources"

assert properties."$module1Sources" == properties."$projectBaseDir" + "/module1/pom.xml"
assert properties."$sources" == properties."$projectBaseDir" + "/pom.xml"
assert properties."$sources" == properties."$projectBaseDir" + "/pom.xml" + "," + properties."$projectBaseDir" + "/verify.groovy"
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 @@ -28,6 +28,8 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.function.Consumer;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
Expand Down Expand Up @@ -111,11 +113,11 @@ void testSQBefore56() {
when(scanner.serverVersion()).thenReturn("5.1");

MojoExecutionException exception = assertThrows(MojoExecutionException.class,
() -> scannerBootstrapper.execute());
() -> scannerBootstrapper.execute());

assertThat(exception)
.hasCauseExactlyInstanceOf(UnsupportedOperationException.class)
.hasMessage(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE);
.hasCauseExactlyInstanceOf(UnsupportedOperationException.class)
.hasMessage(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE);
}

@Test
Expand Down Expand Up @@ -145,68 +147,68 @@ void testNullServerVersion() {
when(scanner.serverVersion()).thenReturn(null);

MojoExecutionException exception = assertThrows(MojoExecutionException.class,
() -> scannerBootstrapper.execute());
() -> scannerBootstrapper.execute());

assertThat(exception)
.hasCauseExactlyInstanceOf(UnsupportedOperationException.class)
.hasMessage(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE);
.hasCauseExactlyInstanceOf(UnsupportedOperationException.class)
.hasMessage(UNSUPPORTED_BELOW_SONARQUBE_56_MESSAGE);
}

@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[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());
verifyCollectedSources(sourceDirs -> {
assertThat(sourceDirs).hasSize(3);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
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 {
setSonarScannerScanAllTo("false");

verifyCollectedSources(sourceDirs -> {
assertThat(sourceDirs).hasSize(2);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
});

// 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();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
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 {
setSonarScannerScanAllTo("true");

verifyCollectedSources(sourceDirs -> {
assertThat(sourceDirs).hasSize(3);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "java").toString());
assertThat(sourceDirs[1]).endsWith(Paths.get("pom.xml").toString());
assertThat(sourceDirs[2]).endsWith(Paths.get("src", "main", "resources", "index.js").toString());
});

// 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();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
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());
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 should_not_collect_all_sources_when_sonar_sources_is_overridden() 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);
setSonarScannerScanAllTo("true");

// 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);

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(1);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "resources").toString());
verifyCollectedSources(sourceDirs -> {
assertThat(sourceDirs).hasSize(1);
assertThat(sourceDirs[0]).endsWith(Paths.get("src", "main", "resources").toString());
});

verify(log, times(1)).info("Parameter sonar.maven.scanAll is enabled. The scanner will attempt to collect additional sources.");
verify(log, times(1)).warn("Parameter sonar.maven.scanAll is enabled but the scanner will not collect additional sources because sonar.sources has been overridden.");
Expand All @@ -215,10 +217,8 @@ void should_not_collect_all_sources_when_sonar_sources_is_overridden() throws Mo

@Test
void should_not_collect_all_sources_when_sonar_tests_is_overridden() 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);
setSonarScannerScanAllTo("true");

// Return the expected directory and notify of overriding
projectProperties.put(ScanProperties.PROJECT_TEST_DIRS, Paths.get("src", "test", "resources").toFile().toString());
when(mavenProjectConverter.isTestDirsOverridden()).thenReturn(true);
Expand All @@ -236,10 +236,7 @@ void should_not_collect_all_sources_when_sonar_tests_is_overridden() throws Mojo

@Test
void an_exception_is_logged_at_warning_level_when_failing_to_crawl_the_filesystem_to_scan_all_sources() throws MojoExecutionException, IOException {
// Enabling the scanAll option explicitly as a scanner option
Properties withScanAllSetToTrue = new Properties();
withScanAllSetToTrue.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "true");
when(session.getUserProperties()).thenReturn(withScanAllSetToTrue);
setSonarScannerScanAllTo("true");

IOException expectedException = new IOException("This is what we expected");
try (MockedStatic<Files> mockedFiles = Mockito.mockStatic(Files.class)) {
Expand All @@ -251,9 +248,7 @@ void an_exception_is_logged_at_warning_level_when_failing_to_crawl_the_filesyste

@Test
void can_collect_sources_with_commas_in_paths() throws MojoExecutionException, IOException {
Properties withScanAllSetToTrue = new Properties();
withScanAllSetToTrue.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, "true");
when(session.getUserProperties()).thenReturn(withScanAllSetToTrue);
setSonarScannerScanAllTo("true");

// Create paths with commas in them
Path root = tmpFolder.toAbsolutePath();
Expand Down Expand Up @@ -333,6 +328,19 @@ void maven_opts_is_not_logged_at_info_level_when_not_absent_from_environment_var
}
}

private void setSonarScannerScanAllTo(String value) {
Properties withScanAllSet = new Properties();
withScanAllSet.put(MavenScannerProperties.PROJECT_SCAN_ALL_SOURCES, value);
when(session.getUserProperties()).thenReturn(withScanAllSet);
}

private void verifyCollectedSources(Consumer<String[]> sourceDirsAssertions) throws MojoExecutionException {
ADarko22 marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> collectedProperties = scannerBootstrapper.collectProperties();
assertThat(collectedProperties).containsKey(ScanProperties.PROJECT_SOURCE_DIRS);
String[] sourceDirs = collectedProperties.get(ScanProperties.PROJECT_SOURCE_DIRS).split(",");
sourceDirsAssertions.accept(sourceDirs);
}

private void verifyCommonCalls() {
verify(scanner).start();
verify(scanner).serverVersion();
Expand Down