-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- split into own sensors: use 'sonar.cxx.bullseye.reportPaths', 'sona…
…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
Showing
30 changed files
with
706 additions
and
447 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
cxx-sensors/src/main/java/org/sonar/cxx/sensors/coverage/CoverageSensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.