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.

ZeroCoverageSensor.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.source;
  21. import java.util.Set;
  22. import org.sonar.api.batch.Phase;
  23. import org.sonar.api.batch.fs.FileSystem;
  24. import org.sonar.api.batch.fs.InputFile;
  25. import org.sonar.api.batch.fs.InputFile.Type;
  26. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  27. import org.sonar.api.batch.sensor.SensorContext;
  28. import org.sonar.api.batch.sensor.SensorDescriptor;
  29. import org.sonar.api.batch.sensor.coverage.NewCoverage;
  30. import org.sonar.api.scanner.sensor.ProjectSensor;
  31. import org.sonar.scanner.report.ReportPublisher;
  32. @Phase(name = Phase.Name.POST)
  33. public final class ZeroCoverageSensor implements ProjectSensor {
  34. private final ReportPublisher reportPublisher;
  35. public ZeroCoverageSensor(ReportPublisher reportPublisher) {
  36. this.reportPublisher = reportPublisher;
  37. }
  38. @Override
  39. public void describe(SensorDescriptor descriptor) {
  40. descriptor.name("Zero Coverage Sensor");
  41. }
  42. @Override
  43. public void execute(final SensorContext context) {
  44. FileSystem fs = context.fileSystem();
  45. for (InputFile f : fs.inputFiles(fs.predicates().hasType(Type.MAIN))) {
  46. if (((DefaultInputFile) f).isExcludedForCoverage()) {
  47. continue;
  48. }
  49. if (!isCoverageAlreadyDefined(f)) {
  50. ((DefaultInputFile) f).getExecutableLines().ifPresent(execLines -> storeZeroCoverageForEachExecutableLine(context, f, execLines));
  51. }
  52. }
  53. }
  54. private static void storeZeroCoverageForEachExecutableLine(final SensorContext context, InputFile f, Set<Integer> executableLines) {
  55. NewCoverage newCoverage = context.newCoverage().onFile(f);
  56. for (Integer lineIdx : executableLines) {
  57. if (lineIdx <= f.lines()) {
  58. newCoverage.lineHits(lineIdx, 0);
  59. }
  60. }
  61. newCoverage.save();
  62. }
  63. private boolean isCoverageAlreadyDefined(InputFile f) {
  64. return reportPublisher.getReader().hasCoverage(((DefaultInputFile) f).scannerId());
  65. }
  66. }