use of org.sonar.api.batch.sensor.coverage.NewCoverage in project sonarqube by SonarSource.
the class ZeroCoverageSensor method storeZeroCoverageForEachExecutableLine.
private static void storeZeroCoverageForEachExecutableLine(final SensorContext context, InputFile f, DefaultMeasure<String> execLines) {
NewCoverage newCoverage = context.newCoverage().onFile(f);
Map<Integer, Integer> lineMeasures = KeyValueFormat.parseIntInt((String) execLines.value());
for (Map.Entry<Integer, Integer> lineMeasure : lineMeasures.entrySet()) {
int lineIdx = lineMeasure.getKey();
if (lineIdx <= f.lines() && lineMeasure.getValue() > 0) {
newCoverage.lineHits(lineIdx, 0);
}
}
newCoverage.save();
}
use of org.sonar.api.batch.sensor.coverage.NewCoverage in project sonar-java by SonarSource.
the class UnitTestAnalyzer method readExecutionData.
private void readExecutionData(@Nullable File jacocoExecutionData, SensorContext context) {
File newJacocoExecutionData = jacocoExecutionData;
if (newJacocoExecutionData == null || !newJacocoExecutionData.isFile()) {
JaCoCoExtensions.LOG.info("Project coverage is set to 0% as no JaCoCo execution data has been dumped: {}", newJacocoExecutionData);
newJacocoExecutionData = null;
}
ExecutionDataVisitor executionDataVisitor = new ExecutionDataVisitor();
jacocoReportReader = new JacocoReportReader(newJacocoExecutionData).readJacocoReport(executionDataVisitor, executionDataVisitor);
boolean collectedCoveragePerTest = readCoveragePerTests(executionDataVisitor);
CoverageBuilder coverageBuilder = jacocoReportReader.analyzeFiles(executionDataVisitor.getMerged(), classFilesCache.values());
int analyzedResources = 0;
for (ISourceFileCoverage coverage : coverageBuilder.getSourceFiles()) {
InputFile inputFile = getResource(coverage);
if (inputFile != null) {
NewCoverage newCoverage = context.newCoverage().onFile(inputFile);
analyzeFile(newCoverage, inputFile, coverage);
newCoverage.save();
analyzedResources++;
}
}
if (analyzedResources == 0) {
JaCoCoExtensions.LOG.warn("Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?");
} else if (collectedCoveragePerTest) {
JaCoCoExtensions.LOG.info("Information about coverage per test has been collected.");
} else if (newJacocoExecutionData != null) {
JaCoCoExtensions.LOG.info("No information about coverage per test.");
}
}
use of org.sonar.api.batch.sensor.coverage.NewCoverage in project sonar-go by SonarSource.
the class GoCoverageReport method saveFileCoverage.
private static void saveFileCoverage(SensorContext sensorContext, FileCoverage fileCoverage) {
String absolutePath = fileCoverage.absolutePath.toString();
FileSystem fileSystem = sensorContext.fileSystem();
InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().hasAbsolutePath(absolutePath));
if (inputFile != null) {
LOG.debug("Saving coverage measures for file '{}'", absolutePath);
NewCoverage newCoverage = sensorContext.newCoverage().onFile(inputFile);
for (Map.Entry<Integer, LineCoverage> entry : fileCoverage.lineMap.entrySet()) {
newCoverage.lineHits(entry.getKey(), entry.getValue().hits);
}
newCoverage.save();
} else {
LOG.warn("File '{}' is not included in the project, ignoring coverage", absolutePath);
}
}
use of org.sonar.api.batch.sensor.coverage.NewCoverage in project sonarqube by SonarSource.
the class AbstractCoverageSensor method processCoverage.
private void processCoverage(InputFile inputFile, SensorContext context) {
File coverageFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + getCoverageExtension());
if (coverageFile.exists()) {
LOG.debug("Processing " + coverageFile.getAbsolutePath());
try {
List<String> lines = FileUtils.readLines(coverageFile, context.fileSystem().encoding().name());
NewCoverage coverageBuilder = context.newCoverage().onFile(inputFile);
int lineNumber = 0;
for (String line : lines) {
lineNumber++;
if (StringUtils.isBlank(line)) {
continue;
}
if (line.startsWith("#")) {
continue;
}
try {
Iterator<String> split = Splitter.on(":").split(line).iterator();
int lineId = Integer.parseInt(split.next());
int lineHits = Integer.parseInt(split.next());
coverageBuilder.lineHits(lineId, lineHits);
if (split.hasNext()) {
int conditions = Integer.parseInt(split.next());
int coveredConditions = Integer.parseInt(split.next());
coverageBuilder.conditions(lineId, conditions, coveredConditions);
}
} catch (Exception e) {
throw new IllegalStateException("Error processing line " + lineNumber + " of file " + coverageFile.getAbsolutePath(), e);
}
}
coverageBuilder.save();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
use of org.sonar.api.batch.sensor.coverage.NewCoverage in project sonarqube by SonarSource.
the class GenericCoverageReportParser method parseFiles.
private void parseFiles(SMInputCursor fileCursor, SensorContext context) throws XMLStreamException {
while (fileCursor.getNext() != null) {
checkElementName(fileCursor, "file");
String filePath = mandatoryAttribute(fileCursor, "path");
InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath));
if (inputFile == null || inputFile.language() == null) {
numberOfUnknownFiles++;
if (numberOfUnknownFiles <= MAX_STORED_UNKNOWN_FILE_PATHS) {
firstUnknownFiles.add(filePath);
}
if (inputFile != null) {
LOG.debug("Skipping file '{}' in the generic coverage report because it doesn't have a known language", filePath);
}
continue;
}
matchedFileKeys.add(inputFile.key());
NewCoverage newCoverage = context.newCoverage().onFile(inputFile);
SMInputCursor lineToCoverCursor = fileCursor.childElementCursor();
while (lineToCoverCursor.getNext() != null) {
parseLineToCover(lineToCoverCursor, newCoverage);
}
newCoverage.save();
}
}
Aggregations