Search in sources :

Example 1 with NewCoverage

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();
}
Also used : NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) Map(java.util.Map)

Example 2 with NewCoverage

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.");
    }
}
Also used : NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) InputFile(org.sonar.api.batch.fs.InputFile)

Example 3 with NewCoverage

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);
    }
}
Also used : NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) FileSystem(org.sonar.api.batch.fs.FileSystem) HashMap(java.util.HashMap) Map(java.util.Map) InputFile(org.sonar.api.batch.fs.InputFile)

Example 4 with NewCoverage

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);
        }
    }
}
Also used : NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) IOException(java.io.IOException)

Example 5 with NewCoverage

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();
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

NewCoverage (org.sonar.api.batch.sensor.coverage.NewCoverage)7 InputFile (org.sonar.api.batch.fs.InputFile)4 Map (java.util.Map)3 File (java.io.File)2 HashMap (java.util.HashMap)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)1 CoverageBuilder (org.jacoco.core.analysis.CoverageBuilder)1 ISourceFileCoverage (org.jacoco.core.analysis.ISourceFileCoverage)1 FileSystem (org.sonar.api.batch.fs.FileSystem)1 NewSignificantCode (org.sonar.api.batch.sensor.code.NewSignificantCode)1 DefaultSignificantCode (org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode)1 DefaultCoverage (org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage)1 NewCpdTokens (org.sonar.api.batch.sensor.cpd.NewCpdTokens)1 DefaultCpdTokens (org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens)1 AnalysisError (org.sonar.api.batch.sensor.error.AnalysisError)1