You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenericCoverageSensor.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.genericcoverage;
  21. import java.io.File;
  22. import java.util.Arrays;
  23. import java.util.Collections;
  24. import java.util.LinkedHashSet;
  25. import java.util.List;
  26. import java.util.Set;
  27. import java.util.stream.Collectors;
  28. import org.sonar.api.batch.sensor.SensorContext;
  29. import org.sonar.api.batch.sensor.SensorDescriptor;
  30. import org.sonar.api.config.PropertyDefinition;
  31. import org.sonar.api.resources.Qualifiers;
  32. import org.sonar.api.scanner.sensor.ProjectSensor;
  33. import org.sonar.api.utils.log.Logger;
  34. import org.sonar.api.utils.log.Loggers;
  35. import org.sonar.scanner.config.DefaultConfiguration;
  36. import static org.sonar.api.CoreProperties.CATEGORY_CODE_COVERAGE;
  37. public class GenericCoverageSensor implements ProjectSensor {
  38. private static final Logger LOG = Loggers.get(GenericCoverageSensor.class);
  39. static final String REPORT_PATHS_PROPERTY_KEY = "sonar.coverageReportPaths";
  40. private final DefaultConfiguration config;
  41. public GenericCoverageSensor(DefaultConfiguration config) {
  42. this.config = config;
  43. }
  44. public static List<PropertyDefinition> properties() {
  45. return Collections.singletonList(
  46. PropertyDefinition.builder(REPORT_PATHS_PROPERTY_KEY)
  47. .name("Coverage report paths")
  48. .description("List of comma-separated paths (absolute or relative) containing coverage report.")
  49. .category(CATEGORY_CODE_COVERAGE)
  50. .onQualifiers(Qualifiers.PROJECT)
  51. .multiValues(true)
  52. .build());
  53. }
  54. private void loadArrayDeprecated(Set<String> reportPaths, String propertyKey) {
  55. if (config.getOriginalProperties().containsKey(propertyKey)) {
  56. LOG.warn("Property '{}' is deprecated. Please use '{}' instead.", propertyKey, REPORT_PATHS_PROPERTY_KEY);
  57. reportPaths.addAll(Arrays.asList(config.getStringArray(propertyKey)));
  58. }
  59. }
  60. private void loadDeprecated(Set<String> reportPaths, String propertyKey) {
  61. if (config.getOriginalProperties().containsKey(propertyKey)) {
  62. LOG.warn("Property '{}' is deprecated. Please use '{}' instead.", propertyKey, REPORT_PATHS_PROPERTY_KEY);
  63. config.get(propertyKey).ifPresent(reportPaths::add);
  64. }
  65. }
  66. @Override
  67. public void describe(SensorDescriptor descriptor) {
  68. descriptor.name("Generic Coverage Report")
  69. .onlyWhenConfiguration(c -> c.hasKey(REPORT_PATHS_PROPERTY_KEY));
  70. }
  71. @Override
  72. public void execute(SensorContext context) {
  73. Set<String> reportPaths = loadReportPaths();
  74. for (String reportPath : reportPaths) {
  75. File reportFile = context.fileSystem().resolvePath(reportPath);
  76. LOG.info("Parsing {}", reportFile);
  77. GenericCoverageReportParser parser = new GenericCoverageReportParser();
  78. parser.parse(reportFile, context);
  79. LOG.info("Imported coverage data for {} files", parser.numberOfMatchedFiles());
  80. int numberOfUnknownFiles = parser.numberOfUnknownFiles();
  81. if (numberOfUnknownFiles > 0) {
  82. LOG.info("Coverage data ignored for " + numberOfUnknownFiles + " unknown files, including:\n" + parser.firstUnknownFiles().stream().collect(Collectors.joining("\n")));
  83. }
  84. }
  85. }
  86. Set<String> loadReportPaths() {
  87. return new LinkedHashSet<>(Arrays.asList(config.getStringArray(REPORT_PATHS_PROPERTY_KEY)));
  88. }
  89. }