diff options
author | Benjamin Campomenosi <benjamin.campomenosi@sonarsource.com> | 2023-01-06 10:56:52 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-01-09 20:03:09 +0000 |
commit | 36e6b56323a014d6254e8a5b68a90fabc18f8969 (patch) | |
tree | d7534793f2defc611c5986f5b1eebc774b15b093 /sonar-scanner-engine/src/main/java/org/sonar/scanner/scan | |
parent | 3c926d8640a863171e842f69b863a489f42f297b (diff) | |
download | sonarqube-36e6b56323a014d6254e8a5b68a90fabc18f8969.tar.gz sonarqube-36e6b56323a014d6254e8a5b68a90fabc18f8969.zip |
SONAR-16491 add aliases for sonar.test.inclusions and sonar.test.exclusions
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner/scan')
4 files changed, 86 insertions, 22 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/AbstractExclusionFilters.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/AbstractExclusionFilters.java index 09f2ec19a25..b5ade4c59ed 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/AbstractExclusionFilters.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/AbstractExclusionFilters.java @@ -29,12 +29,26 @@ import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.PathPattern; +import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import static java.lang.String.format; +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; +import static org.sonar.api.CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY; + public abstract class AbstractExclusionFilters { private static final Logger LOG = Loggers.get(AbstractExclusionFilters.class); + 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 final AnalysisWarnings analysisWarnings; private final String[] sourceInclusions; private final String[] testInclusions; private final String[] sourceExclusions; @@ -45,15 +59,59 @@ public abstract class AbstractExclusionFilters { private PathPattern[] testInclusionsPattern; private PathPattern[] testExclusionsPattern; - protected AbstractExclusionFilters(Function<String, String[]> configProvider) { + protected AbstractExclusionFilters(AnalysisWarnings analysisWarnings, Function<String, String[]> configProvider) { + this.analysisWarnings = analysisWarnings; this.sourceInclusions = inclusions(configProvider, CoreProperties.PROJECT_INCLUSIONS_PROPERTY); - this.testInclusions = inclusions(configProvider, CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY); this.sourceExclusions = exclusions(configProvider, CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_EXCLUSIONS_PROPERTY); - this.testExclusions = exclusions(configProvider, CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY); + + 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); + this.mainInclusionsPattern = prepareMainInclusions(sourceInclusions); - this.mainExclusionsPattern = prepareMainExclusions(sourceExclusions, testInclusions); - this.testInclusionsPattern = prepareTestInclusions(testInclusions); - this.testExclusionsPattern = prepareTestExclusions(testExclusions); + this.mainExclusionsPattern = prepareMainExclusions(sourceExclusions, this.testInclusions); + this.testInclusionsPattern = prepareTestInclusions(this.testInclusions); + this.testExclusionsPattern = prepareTestExclusions(this.testExclusions); + } + + private String[] keepExclusionTestBetweenLegacyAndAliasProperties(String[] fromLegacyProperty, String[] fromAliasProperty) { + if (fromAliasProperty.length == 0) { + return fromLegacyProperty; + } + if (fromLegacyProperty.length == 0) { + logWarningForAliasUsage(PROJECT_TEST_EXCLUSIONS_PROPERTY, PROJECT_TESTS_EXCLUSIONS_PROPERTY); + return fromAliasProperty; + } + logWarningForLegacyAndAliasUsage(PROJECT_TEST_EXCLUSIONS_PROPERTY, PROJECT_TESTS_EXCLUSIONS_PROPERTY); + return fromLegacyProperty; + } + + private String[] keepInclusionTestBetweenLegacyAndAliasProperties(String[] fromLegacyProperty, String[] fromAliasProperty) { + if (fromAliasProperty.length == 0) { + return fromLegacyProperty; + } + if (fromLegacyProperty.length == 0) { + logWarningForAliasUsage(PROJECT_TEST_INCLUSIONS_PROPERTY, PROJECT_TESTS_INCLUSIONS_PROPERTY); + return fromAliasProperty; + } + logWarningForLegacyAndAliasUsage(PROJECT_TEST_INCLUSIONS_PROPERTY, PROJECT_TESTS_INCLUSIONS_PROPERTY); + return fromLegacyProperty; + } + + private void logWarningForAliasUsage(String legacyProperty, String aliasProperty) { + logWarning(format(WARNING_ALIAS_PROPERTY_USAGE, aliasProperty, legacyProperty)); + } + + private void logWarningForLegacyAndAliasUsage(String legacyProperty, String aliasProperty) { + logWarning(format(WARNING_LEGACY_AND_ALIAS_PROPERTIES_USAGE, legacyProperty, aliasProperty, legacyProperty)); + } + + private void logWarning(String warning) { + LOG.warn(warning); + analysisWarnings.addUnique(warning); } public void log(String indent) { @@ -149,11 +207,11 @@ public abstract class AbstractExclusionFilters { /** * <p>Checks if the file should be excluded as a parent directory of excluded files and subdirectories.</p> - * + * * @param absolutePath The full path of the file. * @param relativePath The relative path of the file. - * @param baseDir The base directory of the project. - * @param type The file type. + * @param baseDir The base directory of the project. + * @param type The file type. * @return True if the file should be excluded, false otherwise. */ public boolean isExcludedAsParentDirectoryOfExcludedChildren(Path absolutePath, Path relativePath, Path baseDir, InputFile.Type type) { diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleExclusionFilters.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleExclusionFilters.java index f5683486e8e..28d1f600830 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleExclusionFilters.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ModuleExclusionFilters.java @@ -19,12 +19,14 @@ */ package org.sonar.scanner.scan.filesystem; +import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.scanner.scan.ModuleConfiguration; public class ModuleExclusionFilters extends AbstractExclusionFilters { - public ModuleExclusionFilters(ModuleConfiguration moduleConfiguration) { - super(moduleConfiguration::getStringArray); + + public ModuleExclusionFilters(ModuleConfiguration moduleConfiguration, AnalysisWarnings analysisWarnings) { + super(analysisWarnings, moduleConfiguration::getStringArray); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectExclusionFilters.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectExclusionFilters.java index ad6cb085cb1..386e66828d9 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectExclusionFilters.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectExclusionFilters.java @@ -20,11 +20,12 @@ package org.sonar.scanner.scan.filesystem; import org.sonar.api.config.Configuration; +import org.sonar.api.notifications.AnalysisWarnings; public class ProjectExclusionFilters extends AbstractExclusionFilters { - public ProjectExclusionFilters(Configuration projectConfig) { - super(projectConfig::getStringArray); + public ProjectExclusionFilters(Configuration projectConfig, AnalysisWarnings analysisWarnings) { + super(analysisWarnings, projectConfig::getStringArray); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFileIndexer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFileIndexer.java index ea2d488f085..46336597f6d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFileIndexer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFileIndexer.java @@ -43,6 +43,7 @@ import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Type; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.scm.IgnoreCommand; +import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; @@ -77,13 +78,15 @@ public class ProjectFileIndexer { private final FileIndexer fileIndexer; private final IgnoreCommand ignoreCommand; private final boolean useScmExclusion; + private final AnalysisWarnings analysisWarnings; private ProgressReport progressReport; public ProjectFileIndexer(InputComponentStore componentStore, ProjectExclusionFilters exclusionFilters, SonarGlobalPropertiesFilter sonarGlobalPropertiesFilter, InputModuleHierarchy inputModuleHierarchy, GlobalConfiguration globalConfig, GlobalServerSettings globalServerSettings, ProjectServerSettings projectServerSettings, - FileIndexer fileIndexer, ProjectCoverageAndDuplicationExclusions projectCoverageAndDuplicationExclusions, ScmConfiguration scmConfiguration) { + FileIndexer fileIndexer, ProjectCoverageAndDuplicationExclusions projectCoverageAndDuplicationExclusions, ScmConfiguration scmConfiguration, + AnalysisWarnings analysisWarnings) { this.componentStore = componentStore; this.sonarGlobalPropertiesFilter = sonarGlobalPropertiesFilter; this.inputModuleHierarchy = inputModuleHierarchy; @@ -94,6 +97,7 @@ public class ProjectFileIndexer { this.projectExclusionFilters = exclusionFilters; this.projectCoverageAndDuplicationExclusions = projectCoverageAndDuplicationExclusions; this.scmConfiguration = scmConfiguration; + this.analysisWarnings = analysisWarnings; this.ignoreCommand = loadIgnoreCommand(); this.useScmExclusion = ignoreCommand != null; } @@ -147,7 +151,7 @@ public class ProjectFileIndexer { private void index(DefaultInputModule module, ExclusionCounter exclusionCounter) { // Emulate creation of module level settings ModuleConfiguration moduleConfig = new ModuleConfigurationProvider(sonarGlobalPropertiesFilter).provide(globalConfig, module, globalServerSettings, projectServerSettings); - ModuleExclusionFilters moduleExclusionFilters = new ModuleExclusionFilters(moduleConfig); + ModuleExclusionFilters moduleExclusionFilters = new ModuleExclusionFilters(moduleConfig, analysisWarnings); ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions = new ModuleCoverageAndDuplicationExclusions(moduleConfig); if (componentStore.allModules().size() > 1) { LOG.info("Indexing files of module '{}'", module.getName()); @@ -169,7 +173,7 @@ public class ProjectFileIndexer { private static void logPaths(String label, Path baseDir, List<Path> paths) { if (!paths.isEmpty()) { StringBuilder sb = new StringBuilder(label); - for (Iterator<Path> it = paths.iterator(); it.hasNext();) { + for (Iterator<Path> it = paths.iterator(); it.hasNext(); ) { Path file = it.next(); Optional<String> relativePathToBaseDir = PathResolver.relativize(baseDir, file); if (!relativePathToBaseDir.isPresent()) { @@ -225,10 +229,10 @@ public class ProjectFileIndexer { * <p>Exclusions patterns are checked both at project and module level.</p> * * @param moduleExclusionFilters The exclusion filters. - * @param realAbsoluteFile The path to be checked. - * @param projectBaseDir The project base directory. - * @param moduleBaseDir The module base directory. - * @param type The input file type. + * @param realAbsoluteFile The path to be checked. + * @param projectBaseDir The project base directory. + * @param moduleBaseDir The module base directory. + * @param type The input file type. * @return True if path is an excluded directory, false otherwise. */ private static boolean isExcludedDirectory(ModuleExclusionFilters moduleExclusionFilters, Path realAbsoluteFile, Path projectBaseDir, Path moduleBaseDir, @@ -283,8 +287,7 @@ public class ProjectFileIndexer { * </p> * * @param file a reference to the file - * @param exc the I/O exception that prevented the file from being visited - * + * @param exc the I/O exception that prevented the file from being visited * @throws IOException */ @Override |