Skip to content

Commit

Permalink
- split into own sensors: use 'sonar.cxx.bullseye.reportPaths', 'sona…
Browse files Browse the repository at this point in the history
…r.cxx.cobertura.reportPaths', 'sonar.cxx.vscoveragexml.reportPaths' or 'sonar.cxx.ctctxt.reportPaths'

- add warning for deprecated configuration property 'sonar.cxx.coverage.reportPath'
- Remove coverage cache: Sensor now reads reports by project and no longer by module. Reports are no longer read multiple times.
- improve logging
- update integration tests to use new configuration properties
  • Loading branch information
guwirth committed Sep 28, 2020
1 parent fe1a8b3 commit 822173b
Show file tree
Hide file tree
Showing 30 changed files with 706 additions and 447 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ private CoverageMeasures() {
// empty
}

static CoverageMeasures create() {
static public CoverageMeasures create() {
return new CoverageMeasures();
}

void setHits(int lineId, int hits) {
public void setHits(int lineId, int hits) {
lineMeasures.computeIfAbsent(lineId, v -> new CoverageMeasure(lineId));
CoverageMeasure coverageMeasure = lineMeasures.get(lineId);
coverageMeasure.setHits(hits);
}

void setConditions(int lineId, int totalConditions, int coveredConditions) {
public void setConditions(int lineId, int totalConditions, int coveredConditions) {
lineMeasures.computeIfAbsent(lineId, v -> new CoverageMeasure(lineId));
CoverageMeasure coverageMeasure = lineMeasures.get(lineId);
coverageMeasure.setConditions(totalConditions, coveredConditions);
Expand All @@ -60,7 +60,7 @@ Collection<CoverageMeasure> getCoverageMeasures() {
return measures.values();
}

Set<Integer> getCoveredLines() {
public Set<Integer> getCoveredLines() {
var coveredLines = new HashSet<Integer>();
lineMeasures.forEach((key, value) -> {
if (value.getHits() != 0) {
Expand All @@ -70,7 +70,7 @@ Set<Integer> getCoveredLines() {
return Collections.unmodifiableSet(coveredLines);
}

Set<Integer> getCoveredConditions() {
public Set<Integer> getCoveredConditions() {
var coveredConditionLines = new HashSet<Integer>();
lineMeasures.forEach((key, value) -> {
if (value.getCoveredConditions() != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import java.io.File;
import java.util.Map;
import javax.xml.stream.XMLStreamException;
import org.sonar.cxx.sensors.utils.ReportException;

/**
* The interface a coverage report parser has to implement in order to be used by CxxCoverageSensor
Expand All @@ -31,16 +31,13 @@ public interface CoverageParser {
/**
* Parses the given report and stores the results in the according builder
*
* @param context of sensor
* @param report with coverage data
* @param coverageData A Map mapping source file names to coverage measures. Has to be used to store the results into.
* Source file names might be relative. In such case they will be resolved against the base directory of SonarQube
* module/project.<br>
* @return coverageData A Map mapping source file names to coverage measures. Has to be used to store the results
* into. Source file names might be relative. In such case they will be resolved against the base directory of
* SonarQube project.<br>
*
* <b>ATTENTION!</b> This map is shared between modules in multi-module projects. Don't try to resolve paths against
* some specific module!
* @throws XMLStreamException javax.xml.stream.XMLStreamException
* @throws ReportException EmptyReportException or InvalidReportException
*/
void parse(File report, Map<String, CoverageMeasures> coverageData) throws XMLStreamException;
Map<String, CoverageMeasures> parse(File report) throws ReportException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2020 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.cxx.sensors.coverage;

import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.coverage.NewCoverage;
import org.sonar.api.utils.PathUtils;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.cxx.sensors.utils.CxxReportSensor;
import org.sonar.cxx.sensors.utils.CxxUtils;
import org.sonar.cxx.sensors.utils.EmptyReportException;
import org.sonar.cxx.sensors.utils.ReportException;

/**
* {@inheritDoc}
*/
public abstract class CoverageSensor extends CxxReportSensor {

private static final Logger LOG = Loggers.get(CoverageSensor.class);

private final CoverageParser parser;
private final String reportPathsKey;

protected CoverageSensor(String reportPathsKey, CoverageParser parser) {
this.reportPathsKey = reportPathsKey;
this.parser = parser;
}

/**
* {@inheritDoc}
*/
@Override
public void executeImpl() {
List<File> reports = getReports(reportPathsKey);
for (var report : reports) {
executeReport(report);
}
}

/**
* @param report to read
*/
protected void executeReport(File report) {
try {
LOG.info("Processing report '{}'", report);
processReport(report);
LOG.info("Processing successful");
} catch (EmptyReportException e) {
LOG.warn(e.getMessage());
} catch (ReportException e) {
CxxUtils.validateRecovery(e.getMessage(), e, context.config());
}
}

protected void processReport(File report) throws ReportException {
var coverageData = parser.parse(report);
if (coverageData.isEmpty()) {
throw new EmptyReportException("Coverage report " + report + " result is empty (parsed by " + parser + ")");
}

saveMeasures(coverageData);
}

protected void saveMeasures(Map<String, CoverageMeasures> coverageMeasures) {
for (var entry : coverageMeasures.entrySet()) {
final String filePath = PathUtils.sanitize(entry.getKey());
if (filePath != null) {
InputFile cxxFile = getInputFileIfInProject(filePath);
LOG.debug("save coverage measure for file: '{}' cxxFile = '{}'", filePath, cxxFile);

if (cxxFile != null) {

NewCoverage newCoverage = context.newCoverage().onFile(cxxFile);

Collection<CoverageMeasure> measures = entry.getValue().getCoverageMeasures();
LOG.debug("Saving '{}' coverage measures for file '{}'", measures.size(), filePath);

measures.forEach((CoverageMeasure measure) -> checkCoverage(newCoverage, measure));

try {
newCoverage.save();
LOG.debug("Saved '{}' coverage measures for file '{}'", measures.size(), filePath);
} catch (RuntimeException e) {
var msg = "Cannot save coverage measures for file '" + filePath + "'";
CxxUtils.validateRecovery(msg, e, context.config());
}
} else {
LOG.debug("Cannot find the file '{}', ignoring coverage measures", filePath);
if (filePath.startsWith(context.fileSystem().baseDir().getAbsolutePath())) {
LOG.warn("Cannot find the file '{}', ignoring coverage measures", filePath);
}
}
} else {
LOG.debug("Cannot sanitize file path '{}'", entry.getKey());
}
}
}

/**
* @param newCoverage
* @param measure
*/
protected void checkCoverage(NewCoverage newCoverage, CoverageMeasure measure) {
try {
newCoverage.lineHits(measure.getLine(), measure.getHits());
newCoverage.conditions(measure.getLine(), measure.getConditions(), measure.getCoveredConditions());
LOG.debug("line '{}' Hits '{}' Conditions '{}:{}'", measure.getLine(), measure.getHits(),
measure.getConditions(), measure.getCoveredConditions());
} catch (RuntimeException e) {
var msg = "Cannot save Conditions Hits for Line '" + measure.getLine() + "'";
CxxUtils.validateRecovery(msg, e, context.config());
}
}

}
Loading

0 comments on commit 822173b

Please sign in to comment.