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;
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 {
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);
}
}
- 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<Serializable> metric = metricFinder.findByKey(metricKey);
if (metric == null) {
throw new IllegalStateException("Unknow metric with key: " + metricKey);
}
NewMeasure<Serializable> 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())) {
@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);
}
}
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;
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<DefaultMeasure<?>> 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()));
+ }
}
}
}
}
- 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<Integer>().forMetric(TESTS).withValue((int) nonSkippedTests));
+ measureCache.put(inputFile.key(), TESTS_KEY, new DefaultMeasure<Integer>().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<Long>().forMetric(TEST_EXECUTION_TIME).withValue(executionTime));
+ measureCache.put(inputFile.key(), TEST_EXECUTION_TIME_KEY, new DefaultMeasure<Long>().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<Integer>().forMetric(TEST_ERRORS).withValue((int) errorTests));
+ measureCache.put(inputFile.key(), TEST_ERRORS_KEY, new DefaultMeasure<Integer>().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<Integer>().forMetric(SKIPPED_TESTS).withValue((int) skippedTests));
+ measureCache.put(inputFile.key(), SKIPPED_TESTS_KEY, new DefaultMeasure<Integer>().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<Integer>().forMetric(TEST_FAILURES).withValue((int) failedTests));
+ measureCache.put(inputFile.key(), TEST_FAILURES_KEY, new DefaultMeasure<Integer>().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<String> lineHitsMeasure = (DefaultMeasure<String>) measureCache.byMetric(component.key(), CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY);
+ DefaultMeasure<String> lineHitsMeasure = (DefaultMeasure<String>) measureCache.byMetric(inputFile.key(), CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY);
if (lineHitsMeasure != null) {
Map<Integer, Integer> lineHits = KeyValueFormat.parseIntInt(lineHitsMeasure.value());
- measureCache.put(component.key(), LINES_TO_COVER_KEY, new DefaultMeasure<Integer>().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<Integer>().forMetric(LINES_TO_COVER).withValue(lineHits.keySet().size()));
+ measureCache.put(inputFile.key(), UNCOVERED_LINES_KEY,
new DefaultMeasure<Integer>().forMetric(UNCOVERED_LINES).withValue((int) lineHits.values()
.stream()
.filter(hit -> hit == 0)
.count()));
}
- DefaultMeasure<String> conditionsMeasure = (DefaultMeasure<String>) measureCache.byMetric(component.key(), CoreMetrics.CONDITIONS_BY_LINE_KEY);
- DefaultMeasure<String> coveredConditionsMeasure = (DefaultMeasure<String>) measureCache.byMetric(component.key(), CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY);
+ DefaultMeasure<String> conditionsMeasure = (DefaultMeasure<String>) measureCache.byMetric(inputFile.key(), CoreMetrics.CONDITIONS_BY_LINE_KEY);
+ DefaultMeasure<String> coveredConditionsMeasure = (DefaultMeasure<String>) measureCache.byMetric(inputFile.key(), CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY);
if (conditionsMeasure != null) {
Map<Integer, Integer> conditions = KeyValueFormat.parseIntInt(conditionsMeasure.value());
Map<Integer, Integer> coveredConditions = coveredConditionsMeasure != null ? KeyValueFormat.parseIntInt(coveredConditionsMeasure.value()) : Collections.emptyMap();
- measureCache.put(component.key(), CONDITIONS_TO_COVER_KEY, new DefaultMeasure<Integer>().forMetric(CONDITIONS_TO_COVER).withValue(conditions
+ measureCache.put(inputFile.key(), CONDITIONS_TO_COVER_KEY, new DefaultMeasure<Integer>().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<Integer>().forMetric(UNCOVERED_CONDITIONS)
.withValue((int) conditions.keySet()
.stream()