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.

CoveragePerTestSensor.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.xoo.test;
  21. import com.google.common.base.Splitter;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import org.apache.commons.io.FileUtils;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.sonar.api.batch.DependsUpon;
  30. import org.sonar.api.batch.fs.FilePredicates;
  31. import org.sonar.api.batch.fs.FileSystem;
  32. import org.sonar.api.batch.fs.InputFile;
  33. import org.sonar.api.batch.fs.InputFile.Type;
  34. import org.sonar.api.batch.sensor.Sensor;
  35. import org.sonar.api.batch.sensor.SensorContext;
  36. import org.sonar.api.batch.sensor.SensorDescriptor;
  37. import org.sonar.api.component.ResourcePerspectives;
  38. import org.sonar.api.test.MutableTestCase;
  39. import org.sonar.api.test.MutableTestPlan;
  40. import org.sonar.api.test.MutableTestable;
  41. import org.sonar.api.utils.log.Logger;
  42. import org.sonar.api.utils.log.Loggers;
  43. import org.sonar.xoo.Xoo;
  44. /**
  45. * Parse files *.xoo.testcoverage
  46. */
  47. @DependsUpon("test-exec")
  48. public class CoveragePerTestSensor implements Sensor {
  49. private static final Logger LOG = Loggers.get(CoveragePerTestSensor.class);
  50. private static final String TEST_EXTENSION = ".testcoverage";
  51. private final FileSystem fs;
  52. private final ResourcePerspectives perspectives;
  53. public CoveragePerTestSensor(FileSystem fileSystem, ResourcePerspectives perspectives) {
  54. this.fs = fileSystem;
  55. this.perspectives = perspectives;
  56. }
  57. private void processTestFile(InputFile inputFile, SensorContext context) {
  58. File testExecutionFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + TEST_EXTENSION);
  59. if (testExecutionFile.exists()) {
  60. LOG.debug("Processing " + testExecutionFile.getAbsolutePath());
  61. try {
  62. List<String> lines = FileUtils.readLines(testExecutionFile, fs.encoding().name());
  63. int lineNumber = 0;
  64. MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, inputFile);
  65. for (String line : lines) {
  66. lineNumber++;
  67. if (StringUtils.isBlank(line)) {
  68. continue;
  69. }
  70. if (line.startsWith("#")) {
  71. continue;
  72. }
  73. try {
  74. Iterator<String> split = Splitter.on(";").split(line).iterator();
  75. String name = split.next();
  76. while (split.hasNext()) {
  77. String coveredBlockStr = split.next();
  78. Iterator<String> splitCoveredBlock = Splitter.on(",").split(coveredBlockStr).iterator();
  79. String componentPath = splitCoveredBlock.next();
  80. InputFile coveredFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(componentPath));
  81. MutableTestable testable = perspectives.as(MutableTestable.class, coveredFile);
  82. List<Integer> coveredLines = new ArrayList<>();
  83. while (splitCoveredBlock.hasNext()) {
  84. coveredLines.add(Integer.parseInt(splitCoveredBlock.next()));
  85. }
  86. for (MutableTestCase testCase : testPlan.testCasesByName(name)) {
  87. testCase.setCoverageBlock(testable, coveredLines);
  88. }
  89. }
  90. } catch (Exception e) {
  91. throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testExecutionFile.getAbsolutePath(), e);
  92. }
  93. }
  94. } catch (IOException e) {
  95. throw new RuntimeException(e);
  96. }
  97. }
  98. }
  99. @Override
  100. public void describe(SensorDescriptor descriptor) {
  101. descriptor
  102. .name("Xoo Coverage Per Test Sensor")
  103. .onlyOnLanguages(Xoo.KEY);
  104. }
  105. @Override
  106. public void execute(SensorContext context) {
  107. FilePredicates p = context.fileSystem().predicates();
  108. for (InputFile file : context.fileSystem().inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.TEST)))) {
  109. processTestFile(file, context);
  110. }
  111. }
  112. }