選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

TestExecutionAndCoveragePublisher.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.report;
  21. import com.google.common.collect.Iterables;
  22. import java.util.HashSet;
  23. import java.util.Set;
  24. import java.util.stream.StreamSupport;
  25. import org.sonar.api.batch.fs.InputComponent;
  26. import org.sonar.api.batch.fs.internal.DefaultInputComponent;
  27. import org.sonar.api.test.CoverageBlock;
  28. import org.sonar.api.test.MutableTestCase;
  29. import org.sonar.api.test.MutableTestPlan;
  30. import org.sonar.api.test.TestCase;
  31. import org.sonar.scanner.deprecated.test.DefaultTestable;
  32. import org.sonar.scanner.deprecated.test.TestPlanBuilder;
  33. import org.sonar.scanner.protocol.output.ScannerReport;
  34. import org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail;
  35. import org.sonar.scanner.protocol.output.ScannerReport.Test;
  36. import org.sonar.scanner.protocol.output.ScannerReport.Test.TestStatus;
  37. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  38. import org.sonar.scanner.scan.branch.BranchConfiguration;
  39. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  40. import static java.util.stream.Collectors.toList;
  41. public class TestExecutionAndCoveragePublisher implements ReportPublisherStep {
  42. private final InputComponentStore componentStore;
  43. private final TestPlanBuilder testPlanBuilder;
  44. private final BranchConfiguration branchConfiguration;
  45. public TestExecutionAndCoveragePublisher(InputComponentStore componentStore, TestPlanBuilder testPlanBuilder, BranchConfiguration branchConfiguration) {
  46. this.componentStore = componentStore;
  47. this.testPlanBuilder = testPlanBuilder;
  48. this.branchConfiguration = branchConfiguration;
  49. }
  50. @Override
  51. public void publish(ScannerReportWriter writer) {
  52. if (branchConfiguration.isShortOrPullRequest()) {
  53. return;
  54. }
  55. final ScannerReport.Test.Builder testBuilder = ScannerReport.Test.newBuilder();
  56. final ScannerReport.CoverageDetail.Builder builder = ScannerReport.CoverageDetail.newBuilder();
  57. final ScannerReport.CoverageDetail.CoveredFile.Builder coveredBuilder = ScannerReport.CoverageDetail.CoveredFile.newBuilder();
  58. for (final InputComponent c : componentStore.all()) {
  59. DefaultInputComponent component = (DefaultInputComponent) c;
  60. final MutableTestPlan testPlan = testPlanBuilder.loadPerspective(MutableTestPlan.class, component);
  61. if (testPlan == null || Iterables.isEmpty(testPlan.testCases())) {
  62. continue;
  63. }
  64. final Set<String> testNamesWithCoverage = new HashSet<>();
  65. writer.writeTests(component.scannerId(),
  66. StreamSupport.stream(testPlan.testCases().spliterator(), false)
  67. .map(testCase -> toProtobufTest(testBuilder, testNamesWithCoverage, testCase))
  68. .collect(toList()));
  69. writer.writeCoverageDetails(component.scannerId(), testNamesWithCoverage.stream()
  70. .map(testName -> toProtobufCoverageDetails(builder, coveredBuilder, testPlan, testName))
  71. .collect(toList()));
  72. }
  73. }
  74. private CoverageDetail toProtobufCoverageDetails(final ScannerReport.CoverageDetail.Builder builder, final ScannerReport.CoverageDetail.CoveredFile.Builder coveredBuilder,
  75. final MutableTestPlan testPlan, String testName) {
  76. // Take first test with provided name
  77. MutableTestCase testCase = testPlan.testCasesByName(testName).iterator().next();
  78. builder.clear();
  79. builder.setTestName(testName);
  80. for (CoverageBlock block : testCase.coverageBlocks()) {
  81. coveredBuilder.clear();
  82. DefaultInputComponent c = (DefaultInputComponent) componentStore.getByKey(((DefaultTestable) block.testable()).inputFile().key());
  83. coveredBuilder.setFileRef(c.scannerId());
  84. for (int line : block.lines()) {
  85. coveredBuilder.addCoveredLine(line);
  86. }
  87. builder.addCoveredFile(coveredBuilder.build());
  88. }
  89. return builder.build();
  90. }
  91. private static Test toProtobufTest(final ScannerReport.Test.Builder testBuilder, final Set<String> testNamesWithCoverage, MutableTestCase testCase) {
  92. testBuilder.clear();
  93. testBuilder.setName(testCase.name());
  94. if (testCase.doesCover()) {
  95. testNamesWithCoverage.add(testCase.name());
  96. }
  97. Long durationInMs = testCase.durationInMs();
  98. if (durationInMs != null) {
  99. testBuilder.setDurationInMs(durationInMs.longValue());
  100. }
  101. String msg = testCase.message();
  102. if (msg != null) {
  103. testBuilder.setMsg(msg);
  104. }
  105. String stack = testCase.stackTrace();
  106. if (stack != null) {
  107. testBuilder.setStacktrace(stack);
  108. }
  109. TestCase.Status status = testCase.status();
  110. if (status != null) {
  111. testBuilder.setStatus(TestStatus.valueOf(status.name()));
  112. }
  113. return testBuilder.build();
  114. }
  115. }