From ada5b9d39a29eb6679526a41e4caecff2415bc9a Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 9 Mar 2017 13:45:52 +0100 Subject: [PATCH] SONAR-8623 Fix regression with project level measures --- .../org/sonar/xoo/lang/MeasureSensor.java | 20 +++--- .../deprecated/test/TestPlanBuilder.java | 5 ++ .../scanner/report/MeasuresPublisher.java | 71 ++++++++++--------- .../measures/MeasuresMediumTest.java | 27 +++++++ 4 files changed, 81 insertions(+), 42 deletions(-) diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/MeasureSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/MeasureSensor.java index fedab7dfebf..8255691c0d2 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/MeasureSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/MeasureSensor.java @@ -25,6 +25,7 @@ import java.io.Serializable; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; +import org.sonar.api.batch.fs.InputComponent; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.measure.MetricFinder; import org.sonar.api.batch.sensor.Sensor; @@ -50,9 +51,7 @@ public class MeasureSensor implements Sensor { this.metricFinder = metricFinder; } - private void processFileMeasures(InputFile inputFile, SensorContext context) { - File ioFile = inputFile.file(); - File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION); + private void processFileMeasures(InputComponent component, File measureFile, SensorContext context) { if (measureFile.exists()) { LOG.debug("Processing " + measureFile.getAbsolutePath()); try { @@ -63,7 +62,7 @@ public class MeasureSensor implements Sensor { if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } - processMeasure(inputFile, context, measureFile, lineNumber, line); + processMeasure(component, context, measureFile, lineNumber, line); } } catch (IOException e) { throw new IllegalStateException(e); @@ -71,25 +70,25 @@ public class MeasureSensor implements Sensor { } } - private void processMeasure(InputFile inputFile, SensorContext context, File measureFile, int lineNumber, String line) { + private void processMeasure(InputComponent component, SensorContext context, File measureFile, int lineNumber, String line) { try { String metricKey = StringUtils.substringBefore(line, ":"); String value = line.substring(metricKey.length() + 1); - saveMeasure(context, inputFile, metricKey, value); + saveMeasure(context, component, metricKey, value); } catch (Exception e) { LOG.error("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e); throw new IllegalStateException("Error processing line " + lineNumber + " of file " + measureFile.getAbsolutePath(), e); } } - private void saveMeasure(SensorContext context, InputFile xooFile, String metricKey, String value) { + private void saveMeasure(SensorContext context, InputComponent component, String metricKey, String value) { org.sonar.api.batch.measure.Metric metric = metricFinder.findByKey(metricKey); if (metric == null) { throw new IllegalStateException("Unknow metric with key: " + metricKey); } NewMeasure newMeasure = context.newMeasure() .forMetric(metric) - .on(xooFile); + .on(component); if (Boolean.class.equals(metric.valueType())) { newMeasure.withValue(Boolean.parseBoolean(value)); } else if (Integer.class.equals(metric.valueType())) { @@ -116,7 +115,10 @@ public class MeasureSensor implements Sensor { @Override public void execute(SensorContext context) { for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) { - processFileMeasures(file, context); + File ioFile = file.file(); + File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION); + processFileMeasures(file, measureFile, context); } + processFileMeasures(context.module(), new File(context.fileSystem().baseDir(), "module" + MEASURES_EXTENSION), context); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/test/TestPlanBuilder.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/test/TestPlanBuilder.java index 54d28a1b500..3f13c7d8958 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/test/TestPlanBuilder.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/deprecated/test/TestPlanBuilder.java @@ -53,4 +53,9 @@ public class TestPlanBuilder extends PerspectiveBuilder { return null; } + @CheckForNull + public DefaultTestPlan getTestPlanByFile(InputFile inputFile) { + return testPlanByFile.get(inputFile); + } + } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MeasuresPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MeasuresPublisher.java index 535708283d2..6a1ce8aa896 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MeasuresPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MeasuresPublisher.java @@ -25,11 +25,11 @@ import java.util.Collections; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.StreamSupport; - import org.sonar.api.batch.fs.InputComponent; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Type; import org.sonar.api.batch.fs.internal.DefaultInputComponent; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.measure.Metric; import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; import org.sonar.api.measures.CoreMetrics; @@ -82,25 +82,30 @@ public class MeasuresPublisher implements ReportPublisherStep { public void publish(ScannerReportWriter writer) { final ScannerReport.Measure.Builder builder = ScannerReport.Measure.newBuilder(); - for (final InputComponent c : componentStore.allFilesToPublish()) { + for (final InputComponent c : componentStore.all()) { DefaultInputComponent component = (DefaultInputComponent) c; - // Recompute all coverage measures from line data to take into account the possible merge of several reports - updateCoverageFromLineData(component); - // Recompute test execution measures from MutableTestPlan to take into account the possible merge of several reports - updateTestExecutionFromTestPlan(component); + if (component.isFile()) { + DefaultInputFile file = (DefaultInputFile) component; + // Recompute all coverage measures from line data to take into account the possible merge of several reports + updateCoverageFromLineData(file); + // Recompute test execution measures from MutableTestPlan to take into account the possible merge of several reports + updateTestExecutionFromTestPlan(file); + } Iterable> scannerMeasures = measureCache.byComponentKey(component.key()); - writer.writeComponentMeasures(component.batchId(), StreamSupport.stream(scannerMeasures.spliterator(), false) - .map(input -> { - if (input.value() == null) { - throw new IllegalArgumentException( - String.format("Measure on metric '%s' and component '%s' has no value, but it's not allowed", input.metric().key(), component.key())); - } - builder.clear(); - builder.setMetricKey(input.metric().key()); - setValueAccordingToType(builder, input); - return builder.build(); - }).collect(Collectors.toList())); + if (scannerMeasures.iterator().hasNext()) { + writer.writeComponentMeasures(component.batchId(), StreamSupport.stream(scannerMeasures.spliterator(), false) + .map(input -> { + if (input.value() == null) { + throw new IllegalArgumentException( + String.format("Measure on metric '%s' and component '%s' has no value, but it's not allowed", input.metric().key(), component.key())); + } + builder.clear(); + builder.setMetricKey(input.metric().key()); + setValueAccordingToType(builder, input); + return builder.build(); + }).collect(Collectors.toList())); + } } } @@ -122,48 +127,48 @@ public class MeasuresPublisher implements ReportPublisherStep { } } - private void updateTestExecutionFromTestPlan(final InputComponent component) { - final MutableTestPlan testPlan = testPlanBuilder.loadPerspective(MutableTestPlan.class, component); + private void updateTestExecutionFromTestPlan(final InputFile inputFile) { + final MutableTestPlan testPlan = testPlanBuilder.getTestPlanByFile(inputFile); if (testPlan == null || Iterables.isEmpty(testPlan.testCases())) { return; } long nonSkippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() != Status.SKIPPED).count(); - measureCache.put(component.key(), TESTS_KEY, new DefaultMeasure().forMetric(TESTS).withValue((int) nonSkippedTests)); + measureCache.put(inputFile.key(), TESTS_KEY, new DefaultMeasure().forMetric(TESTS).withValue((int) nonSkippedTests)); long executionTime = StreamSupport.stream(testPlan.testCases().spliterator(), false).mapToLong(t -> t.durationInMs() != null ? t.durationInMs().longValue() : 0L).sum(); - measureCache.put(component.key(), TEST_EXECUTION_TIME_KEY, new DefaultMeasure().forMetric(TEST_EXECUTION_TIME).withValue(executionTime)); + measureCache.put(inputFile.key(), TEST_EXECUTION_TIME_KEY, new DefaultMeasure().forMetric(TEST_EXECUTION_TIME).withValue(executionTime)); long errorTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.ERROR).count(); - measureCache.put(component.key(), TEST_ERRORS_KEY, new DefaultMeasure().forMetric(TEST_ERRORS).withValue((int) errorTests)); + measureCache.put(inputFile.key(), TEST_ERRORS_KEY, new DefaultMeasure().forMetric(TEST_ERRORS).withValue((int) errorTests)); long skippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.SKIPPED).count(); - measureCache.put(component.key(), SKIPPED_TESTS_KEY, new DefaultMeasure().forMetric(SKIPPED_TESTS).withValue((int) skippedTests)); + measureCache.put(inputFile.key(), SKIPPED_TESTS_KEY, new DefaultMeasure().forMetric(SKIPPED_TESTS).withValue((int) skippedTests)); long failedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.FAILURE).count(); - measureCache.put(component.key(), TEST_FAILURES_KEY, new DefaultMeasure().forMetric(TEST_FAILURES).withValue((int) failedTests)); + measureCache.put(inputFile.key(), TEST_FAILURES_KEY, new DefaultMeasure().forMetric(TEST_FAILURES).withValue((int) failedTests)); } - private void updateCoverageFromLineData(final InputComponent component) { - if (!component.isFile() || ((InputFile) component).type() != Type.MAIN) { + private void updateCoverageFromLineData(final InputFile inputFile) { + if (inputFile.type() != Type.MAIN) { return; } - DefaultMeasure lineHitsMeasure = (DefaultMeasure) measureCache.byMetric(component.key(), CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY); + DefaultMeasure lineHitsMeasure = (DefaultMeasure) measureCache.byMetric(inputFile.key(), CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY); if (lineHitsMeasure != null) { Map lineHits = KeyValueFormat.parseIntInt(lineHitsMeasure.value()); - measureCache.put(component.key(), LINES_TO_COVER_KEY, new DefaultMeasure().forMetric(LINES_TO_COVER).withValue(lineHits.keySet().size())); - measureCache.put(component.key(), UNCOVERED_LINES_KEY, + measureCache.put(inputFile.key(), LINES_TO_COVER_KEY, new DefaultMeasure().forMetric(LINES_TO_COVER).withValue(lineHits.keySet().size())); + measureCache.put(inputFile.key(), UNCOVERED_LINES_KEY, new DefaultMeasure().forMetric(UNCOVERED_LINES).withValue((int) lineHits.values() .stream() .filter(hit -> hit == 0) .count())); } - DefaultMeasure conditionsMeasure = (DefaultMeasure) measureCache.byMetric(component.key(), CoreMetrics.CONDITIONS_BY_LINE_KEY); - DefaultMeasure coveredConditionsMeasure = (DefaultMeasure) measureCache.byMetric(component.key(), CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY); + DefaultMeasure conditionsMeasure = (DefaultMeasure) measureCache.byMetric(inputFile.key(), CoreMetrics.CONDITIONS_BY_LINE_KEY); + DefaultMeasure coveredConditionsMeasure = (DefaultMeasure) measureCache.byMetric(inputFile.key(), CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY); if (conditionsMeasure != null) { Map conditions = KeyValueFormat.parseIntInt(conditionsMeasure.value()); Map coveredConditions = coveredConditionsMeasure != null ? KeyValueFormat.parseIntInt(coveredConditionsMeasure.value()) : Collections.emptyMap(); - measureCache.put(component.key(), CONDITIONS_TO_COVER_KEY, new DefaultMeasure().forMetric(CONDITIONS_TO_COVER).withValue(conditions + measureCache.put(inputFile.key(), CONDITIONS_TO_COVER_KEY, new DefaultMeasure().forMetric(CONDITIONS_TO_COVER).withValue(conditions .values() .stream() .mapToInt(Integer::intValue) .sum())); - measureCache.put(component.key(), UNCOVERED_CONDITIONS_KEY, + measureCache.put(inputFile.key(), UNCOVERED_CONDITIONS_KEY, new DefaultMeasure().forMetric(UNCOVERED_CONDITIONS) .withValue((int) conditions.keySet() .stream() diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/measures/MeasuresMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/measures/MeasuresMediumTest.java index 72da5d44a54..5dd45050f3e 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/measures/MeasuresMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/measures/MeasuresMediumTest.java @@ -200,4 +200,31 @@ public class MeasuresMediumTest { .containsExactly(tuple("ncloc_data", 0, "1=1;4=1")); } + @Test + public void projectLevelMeasures() throws IOException { + File xooFile = new File(srcDir, "sample.xoo"); + FileUtils.write(xooFile, "Sample xoo\n\n\ncontent"); + + File projectMeasures = new File(baseDir, "module.measures"); + FileUtils.write(projectMeasures, "tests:10"); + + TaskResult result = tester.newTask() + .properties(ImmutableMap.builder() + .put("sonar.task", "scan") + .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) + .put("sonar.projectKey", "com.foo.project") + .put("sonar.projectName", "Foo Project") + .put("sonar.projectVersion", "1.0-SNAPSHOT") + .put("sonar.projectDescription", "Description of Foo Project") + .put("sonar.sources", "src") + .build()) + .start(); + + Map> allMeasures = result.allMeasures(); + + assertThat(allMeasures.get("com.foo.project")) + .extracting("metricKey", "intValue.value", "stringValue.value") + .containsExactly(tuple("tests", 10, "")); + } + } -- 2.39.5