From 990c59de98754be7212d237dc9c4f9fcdfa128f0 Mon Sep 17 00:00:00 2001 From: Angelo Buono Date: Tue, 14 May 2024 17:01:39 +0200 Subject: [PATCH] SCANMAVEN-220: Enable sonar.scanner.scanAll by default (#219) --- .../com/sonar/maven/it/suite/MavenTest.java | 4 +- src/it/java-multi-module/verify.groovy | 2 +- .../maven/bootstrap/ScannerBootstrapper.java | 4 +- .../bootstrap/ScannerBootstrapperTest.java | 120 ++++++++++-------- 4 files changed, 70 insertions(+), 60 deletions(-) diff --git a/its/src/test/java/com/sonar/maven/it/suite/MavenTest.java b/its/src/test/java/com/sonar/maven/it/suite/MavenTest.java index 545e29b0..c3fdb3e7 100644 --- a/its/src/test/java/com/sonar/maven/it/suite/MavenTest.java +++ b/its/src/test/java/com/sonar/maven/it/suite/MavenTest.java @@ -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); } /** diff --git a/src/it/java-multi-module/verify.groovy b/src/it/java-multi-module/verify.groovy index 312a79d4..f9e5da8f 100644 --- a/src/it/java-multi-module/verify.groovy +++ b/src/it/java-multi-module/verify.groovy @@ -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" \ No newline at end of file +assert properties."$sources" == properties."$projectBaseDir" + "/pom.xml" + "," + properties."$projectBaseDir" + "/verify.groovy" diff --git a/src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java b/src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java index f9504bcc..4b2b8a46 100644 --- a/src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java +++ b/src/main/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapper.java @@ -122,6 +122,7 @@ Map collectProperties() Properties userProperties = session.getUserProperties(); Map 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()) { @@ -137,7 +138,8 @@ Map 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) { diff --git a/src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java b/src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java index e165ea49..67946421 100644 --- a/src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java +++ b/src/test/java/org/sonarsource/scanner/maven/bootstrap/ScannerBootstrapperTest.java @@ -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; @@ -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 @@ -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 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 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."); @@ -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); @@ -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 mockedFiles = Mockito.mockStatic(Files.class)) { @@ -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(); @@ -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 sourceDirsAssertions) throws MojoExecutionException { + Map 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();