]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20006 - fix warning on analysis for use of sonar.tests.exclusions
authorBenjamin Campomenosi <109955405+benjamin-campomenosi-sonarsource@users.noreply.github.com>
Thu, 27 Jul 2023 11:28:23 +0000 (13:28 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 27 Jul 2023 20:03:45 +0000 (20:03 +0000)
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/AbstractExclusionFilters.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/AbstractExclusionFiltersTest.java

index febec2f183b9d986cbfe7c08228344722538cf77..83394034a0179c26cd592bf01e408e13b5230166 100644 (file)
@@ -45,8 +45,7 @@ public abstract class AbstractExclusionFilters {
   private static final String WARNING_ALIAS_PROPERTY_USAGE = "Use of %s detected. While being taken into account, the only supported property is %s." +
     " Consider updating your configuration.";
 
-  private static final String WARNING_LEGACY_AND_ALIAS_PROPERTIES_USAGE =
-    "Use of %s and %s at the same time. %s is taken into account. Consider updating your configuration";
+  private static final String WARNING_LEGACY_AND_ALIAS_PROPERTIES_USAGE = "Use of %s and %s at the same time. %s is taken into account. Consider updating your configuration";
 
   private final AnalysisWarnings analysisWarnings;
   private final String[] sourceInclusions;
@@ -61,20 +60,41 @@ public abstract class AbstractExclusionFilters {
 
   protected AbstractExclusionFilters(AnalysisWarnings analysisWarnings, Function<String, String[]> configProvider) {
     this.analysisWarnings = analysisWarnings;
+
     this.sourceInclusions = inclusions(configProvider, CoreProperties.PROJECT_INCLUSIONS_PROPERTY);
-    this.sourceExclusions = exclusions(configProvider, CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_EXCLUSIONS_PROPERTY);
+    this.sourceExclusions = initSourceExclusions(configProvider);
+    this.testInclusions = initTestInclusions(configProvider);
+    this.testExclusions = initTestExclusions(configProvider);
 
+    this.mainInclusionsPattern = prepareMainInclusions(this.sourceInclusions);
+    this.mainExclusionsPattern = prepareMainExclusions(this.sourceExclusions, this.testInclusions);
+    this.testInclusionsPattern = prepareTestInclusions(this.testInclusions);
+    this.testExclusionsPattern = prepareTestExclusions(this.testExclusions);
+  }
+
+  private String[] initSourceExclusions(Function<String, String[]> configProvider) {
+    String[] projectSourceExclusion = exclusions(configProvider, CoreProperties.PROJECT_EXCLUSIONS_PROPERTY);
+    String[] globalSourceExclusion = exclusions(configProvider, CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY);
+    return Stream.concat(Arrays.stream(projectSourceExclusion), Arrays.stream(globalSourceExclusion))
+      .map(String::trim)
+      .toArray(String[]::new);
+  }
+
+  private String[] initTestInclusions(Function<String, String[]> configProvider) {
     String[] testInclusionsFromLegacy = inclusions(configProvider, PROJECT_TEST_INCLUSIONS_PROPERTY);
     String[] testInclusionsFromAlias = inclusions(configProvider, PROJECT_TESTS_INCLUSIONS_PROPERTY);
-    this.testInclusions = keepInclusionTestBetweenLegacyAndAliasProperties(testInclusionsFromLegacy, testInclusionsFromAlias);
-    String[] testExclusionsFromLegacy = exclusions(configProvider, CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY, PROJECT_TEST_EXCLUSIONS_PROPERTY);
-    String[] testExclusionsFromAlias = exclusions(configProvider, CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY, PROJECT_TESTS_EXCLUSIONS_PROPERTY);
-    this.testExclusions = keepExclusionTestBetweenLegacyAndAliasProperties(testExclusionsFromLegacy, testExclusionsFromAlias);
+    return keepInclusionTestBetweenLegacyAndAliasProperties(testInclusionsFromLegacy, testInclusionsFromAlias);
+  }
 
-    this.mainInclusionsPattern = prepareMainInclusions(sourceInclusions);
-    this.mainExclusionsPattern = prepareMainExclusions(sourceExclusions, this.testInclusions);
-    this.testInclusionsPattern = prepareTestInclusions(this.testInclusions);
-    this.testExclusionsPattern = prepareTestExclusions(this.testExclusions);
+  private String[] initTestExclusions(Function<String, String[]> configProvider) {
+    String[] testExclusionsFromLegacy = exclusions(configProvider, CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY);
+    String[] testExclusionsFromAlias = exclusions(configProvider, CoreProperties.PROJECT_TESTS_EXCLUSIONS_PROPERTY);
+    String[] testExclusionsKept = keepExclusionTestBetweenLegacyAndAliasProperties(testExclusionsFromLegacy, testExclusionsFromAlias);
+
+    String[] testExclusionsFromGlobal = exclusions(configProvider, CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY);
+    return Stream.concat(Arrays.stream(testExclusionsKept), Arrays.stream(testExclusionsFromGlobal))
+      .map(String::trim)
+      .toArray(String[]::new);
   }
 
   private String[] keepExclusionTestBetweenLegacyAndAliasProperties(String[] fromLegacyProperty, String[] fromAliasProperty) {
@@ -101,7 +121,7 @@ public abstract class AbstractExclusionFilters {
     return fromLegacyProperty;
   }
 
-  private  void logWarningForAliasUsage(String legacyProperty, String aliasProperty) {
+  private void logWarningForAliasUsage(String legacyProperty, String aliasProperty) {
     logWarning(format(WARNING_ALIAS_PROPERTY_USAGE, aliasProperty, legacyProperty));
   }
 
@@ -129,10 +149,9 @@ public abstract class AbstractExclusionFilters {
       .toArray(String[]::new);
   }
 
-  private String[] exclusions(Function<String, String[]> configProvider, String globalExclusionsProperty, String exclusionsProperty) {
-    String[] globalExclusions = configProvider.apply(globalExclusionsProperty);
-    String[] exclusions = configProvider.apply(exclusionsProperty);
-    return Stream.concat(Arrays.stream(globalExclusions), Arrays.stream(exclusions))
+  private String[] exclusions(Function<String, String[]> configProvider, String property) {
+    String[] exclusions = configProvider.apply(property);
+    return Arrays.stream(exclusions)
       .map(StringUtils::trim)
       .toArray(String[]::new);
   }
index cfe8cd20d3290a8b716da312eeae9e27e1ce96ab..48fe416def596615af2a9277f04ef3eba9ddcb8a 100644 (file)
@@ -37,6 +37,7 @@ import org.sonar.api.testfixtures.log.LogTester;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.sonar.api.CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY;
 import static org.sonar.api.CoreProperties.PROJECT_TESTS_EXCLUSIONS_PROPERTY;
 import static org.sonar.api.CoreProperties.PROJECT_TESTS_INCLUSIONS_PROPERTY;
 import static org.sonar.api.CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY;
@@ -98,6 +99,17 @@ public class AbstractExclusionFiltersTest {
     verify(analysisWarnings).addUnique(expectedWarn);
   }
 
+  @Test
+  public void AbstractExclusionFilters_whenUsedGlobalTestPropertyAndProjectTestLegacyProperty_shouldNotLogAliasWarning() {
+    settings.setProperty(PROJECT_TEST_EXCLUSIONS_PROPERTY, "**/*Dao.java");
+    settings.setProperty(GLOBAL_TEST_EXCLUSIONS_PROPERTY, "**/*Dao.java");
+    settings.setProperty(PROJECT_TEST_INCLUSIONS_PROPERTY, "**/*Dao.java");
+    new AbstractExclusionFilters(analysisWarnings, settings.asConfig()::getStringArray) {
+    };
+
+    assertThat(logTester.logs(Level.WARN)).isEmpty();
+  }
+
   @Test
   public void should_keepLegacyValue_when_legacyAndAliasPropertiesAreUsedForTestInclusions() {
     settings.setProperty(PROJECT_TESTS_INCLUSIONS_PROPERTY, "**/*Dao.java");
@@ -135,4 +147,4 @@ public class AbstractExclusionFiltersTest {
       .contains(expectedWarn);
     verify(analysisWarnings).addUnique(expectedWarn);
   }
-}
\ No newline at end of file
+}