Search in sources :

Example 76 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.

the class TestPlanBuilder method getTestPlan.

public DefaultTestPlan getTestPlan(InputFile component) {
    DefaultInputFile inputFile = (DefaultInputFile) component;
    inputFile.setPublished(true);
    if (!testPlanByFile.containsKey(inputFile)) {
        testPlanByFile.put(inputFile, new DefaultTestPlan());
    }
    return testPlanByFile.get(inputFile);
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 77 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.

the class DefaultSensorStorage method store.

@Override
public void store(NewSymbolTable newSymbolTable) {
    DefaultSymbolTable symbolTable = (DefaultSymbolTable) newSymbolTable;
    ScannerReportWriter writer = reportPublisher.getWriter();
    DefaultInputFile inputFile = (DefaultInputFile) symbolTable.inputFile();
    if (shouldSkipStorage(inputFile)) {
        return;
    }
    inputFile.setPublished(true);
    int componentRef = inputFile.scannerId();
    if (writer.hasComponentData(FileStructure.Domain.SYMBOLS, componentRef)) {
        throw new UnsupportedOperationException("Trying to save symbol table twice for the same file is not supported: " + symbolTable.inputFile());
    }
    final ScannerReport.Symbol.Builder builder = ScannerReport.Symbol.newBuilder();
    final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder();
    writer.writeComponentSymbols(componentRef, symbolTable.getReferencesBySymbol().entrySet().stream().map(input -> {
        builder.clear();
        rangeBuilder.clear();
        TextRange declaration = input.getKey();
        builder.setDeclaration(rangeBuilder.setStartLine(declaration.start().line()).setStartOffset(declaration.start().lineOffset()).setEndLine(declaration.end().line()).setEndOffset(declaration.end().lineOffset()).build());
        for (TextRange reference : input.getValue()) {
            builder.addReference(rangeBuilder.setStartLine(reference.start().line()).setStartOffset(reference.start().lineOffset()).setEndLine(reference.end().line()).setEndOffset(reference.end().lineOffset()).build());
        }
        return builder.build();
    }).collect(Collectors.toList()));
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TextRange(org.sonar.api.batch.fs.TextRange) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable)

Example 78 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.

the class DefaultSensorStorage method saveMeasure.

private void saveMeasure(InputComponent component, DefaultMeasure<?> measure) {
    if (component.isFile()) {
        DefaultInputFile defaultInputFile = (DefaultInputFile) component;
        defaultInputFile.setPublished(true);
    }
    if (component instanceof InputDir || (component instanceof DefaultInputModule && ((DefaultInputModule) component).definition().getParent() != null)) {
        logOnce(measure.metric().key(), "Storing measures on folders or modules is deprecated. Provided value of metric '{}' is ignored.", measure.metric().key());
        return;
    }
    if (DEPRECATED_METRICS_KEYS.contains(measure.metric().key())) {
        logOnce(measure.metric().key(), "Metric '{}' is deprecated. Provided value is ignored.", measure.metric().key());
        return;
    }
    Metric metric = metricFinder.findByKey(measure.metric().key());
    if (metric == null) {
        throw new UnsupportedOperationException("Unknown metric: " + measure.metric().key());
    }
    if (!measure.isFromCore() && NEWLY_CORE_METRICS_KEYS.contains(measure.metric().key())) {
        logOnce(measure.metric().key(), "Metric '{}' is an internal metric computed by SonarQube/SonarCloud. Provided value is ignored.", measure.metric().key());
        return;
    }
    if (!scannerMetrics.getMetrics().contains(metric)) {
        throw new UnsupportedOperationException("Metric '" + metric.key() + "' should not be computed by a Sensor");
    }
    if (((DefaultInputComponent) component).hasMeasureFor(metric)) {
        throw new UnsupportedOperationException("Can not add the same measure twice on " + component + ": " + measure);
    }
    ((DefaultInputComponent) component).setHasMeasureFor(metric);
    if (metric.key().equals(CoreMetrics.EXECUTABLE_LINES_DATA_KEY)) {
        if (component.isFile()) {
            ((DefaultInputFile) component).setExecutableLines(KeyValueFormat.parseIntInt((String) measure.value()).entrySet().stream().filter(e -> e.getValue() > 0).map(Map.Entry::getKey).collect(Collectors.toSet()));
        } else {
            throw new IllegalArgumentException("Executable lines can only be saved on files");
        }
    }
    reportPublisher.getWriter().appendComponentMeasure(((DefaultInputComponent) component).scannerId(), toReportMeasure(measure));
}
Also used : Metric(org.sonar.api.batch.measure.Metric) ScannerMetrics(org.sonar.core.metric.ScannerMetrics) Block(org.sonar.duplications.block.Block) Measure(org.sonar.api.batch.sensor.measure.Measure) COMMENT_LINES_DATA_KEY(org.sonar.api.measures.CoreMetrics.COMMENT_LINES_DATA_KEY) PmdBlockChunker(org.sonar.duplications.internal.pmd.PmdBlockChunker) NewSignificantCode(org.sonar.api.batch.sensor.code.NewSignificantCode) Loggers(org.sonar.api.utils.log.Loggers) NewCpdTokens(org.sonar.api.batch.sensor.cpd.NewCpdTokens) Map(java.util.Map) Configuration(org.sonar.api.config.Configuration) MetricFinder(org.sonar.api.batch.measure.MetricFinder) InputComponent(org.sonar.api.batch.fs.InputComponent) CloseableIterator(org.sonar.core.util.CloseableIterator) Set(java.util.Set) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) Collectors(java.util.stream.Collectors) CoreMetrics(org.sonar.api.measures.CoreMetrics) Serializable(java.io.Serializable) List(java.util.List) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) ReportPublisher(org.sonar.scanner.report.ReportPublisher) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) PUBLIC_DOCUMENTED_API_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY_KEY) Math.max(java.lang.Math.max) DefaultHighlighting(org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) ContextPropertiesCache(org.sonar.scanner.repository.ContextPropertiesCache) SortedMap(java.util.SortedMap) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) TEST_SUCCESS_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.TEST_SUCCESS_DENSITY_KEY) InputFile(org.sonar.api.batch.fs.InputFile) IssuePublisher(org.sonar.scanner.issue.IssuePublisher) BranchConfiguration(org.sonar.scanner.scan.branch.BranchConfiguration) SonarCpdBlockIndex(org.sonar.scanner.cpd.index.SonarCpdBlockIndex) Constants(org.sonar.scanner.protocol.Constants) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) ExternalIssue(org.sonar.api.batch.sensor.issue.ExternalIssue) HashSet(java.util.HashSet) ScannerReportUtils(org.sonar.scanner.report.ScannerReportUtils) AdHocRule(org.sonar.api.batch.sensor.rule.AdHocRule) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) TextRange(org.sonar.api.batch.fs.TextRange) NewSymbolTable(org.sonar.api.batch.sensor.symbol.NewSymbolTable) LINES_KEY(org.sonar.api.measures.CoreMetrics.LINES_KEY) DefaultSignificantCode(org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode) Issue(org.sonar.api.batch.sensor.issue.Issue) FileStructure(org.sonar.scanner.protocol.output.FileStructure) AnalysisError(org.sonar.api.batch.sensor.error.AnalysisError) DefaultCoverage(org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) Collectors.toList(java.util.stream.Collectors.toList) TreeMap(java.util.TreeMap) InputDir(org.sonar.api.batch.fs.InputDir) NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) KeyValueFormat(org.sonar.api.utils.KeyValueFormat) DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) Metric(org.sonar.api.batch.measure.Metric) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputDir(org.sonar.api.batch.fs.InputDir)

Example 79 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.

the class DefaultSensorStorage method store.

@Override
public void store(NewCoverage coverage) {
    DefaultCoverage defaultCoverage = (DefaultCoverage) coverage;
    DefaultInputFile inputFile = (DefaultInputFile) defaultCoverage.inputFile();
    inputFile.setPublished(true);
    SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine = reloadExistingCoverage(inputFile);
    int lineCount = inputFile.lines();
    mergeLineCoverageValues(lineCount, defaultCoverage.hitsByLine(), coveragePerLine, (value, builder) -> builder.setHits(builder.getHits() || value > 0));
    mergeLineCoverageValues(lineCount, defaultCoverage.conditionsByLine(), coveragePerLine, (value, builder) -> builder.setConditions(max(value, builder.getConditions())));
    mergeLineCoverageValues(lineCount, defaultCoverage.coveredConditionsByLine(), coveragePerLine, (value, builder) -> builder.setCoveredConditions(max(value, builder.getCoveredConditions())));
    reportPublisher.getWriter().writeComponentCoverage(inputFile.scannerId(), coveragePerLine.values().stream().map(ScannerReport.LineCoverage.Builder::build).collect(Collectors.toList()));
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) DefaultCoverage(org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage)

Example 80 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.

the class DefaultSensorStorage method store.

@Override
public void store(NewCpdTokens cpdTokens) {
    DefaultCpdTokens defaultCpdTokens = (DefaultCpdTokens) cpdTokens;
    DefaultInputFile inputFile = (DefaultInputFile) defaultCpdTokens.inputFile();
    inputFile.setPublished(true);
    PmdBlockChunker blockChunker = new PmdBlockChunker(getCpdBlockSize(inputFile.language()));
    List<Block> blocks = blockChunker.chunk(inputFile.key(), defaultCpdTokens.getTokenLines());
    index.insert(inputFile, blocks);
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) PmdBlockChunker(org.sonar.duplications.internal.pmd.PmdBlockChunker) DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) Block(org.sonar.duplications.block.Block)

Aggregations

DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)173 Test (org.junit.Test)117 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 File (java.io.File)63 InputFile (org.sonar.api.batch.fs.InputFile)46 Path (java.nio.file.Path)25 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)18 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)17 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)16 FileMetadata (org.sonar.api.batch.fs.internal.FileMetadata)14 IOException (java.io.IOException)13 DefaultIndexedFile (org.sonar.api.batch.fs.internal.DefaultIndexedFile)11 Metadata (org.sonar.api.batch.fs.internal.Metadata)11 Before (org.junit.Before)10 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)10 BlameLine (org.sonar.api.batch.scm.BlameLine)9 List (java.util.List)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)7 TextRange (org.sonar.api.batch.fs.TextRange)7