Search in sources :

Example 11 with FileSystem

use of org.sonar.api.batch.fs.FileSystem in project magik-tools by StevenLooman.

the class MagikSquidSensor method execute.

@Override
public void execute(final SensorContext context) {
    final FileSystem fileSystem = context.fileSystem();
    final FilePredicates predicates = fileSystem.predicates();
    final FilePredicate filePredicate = predicates.and(predicates.hasType(InputFile.Type.MAIN), predicates.hasLanguage(Magik.KEY));
    final List<InputFile> inputFiles = new ArrayList<>();
    fileSystem.inputFiles(filePredicate).forEach(inputFiles::add);
    final ProgressReport progressReport = new ProgressReport("Report about progress of Sonar Magik analyzer", SLEEP_PERIOD);
    final List<String> filenames = inputFiles.stream().map(InputFile::toString).collect(Collectors.toList());
    progressReport.start(filenames);
    for (final InputFile inputFile : inputFiles) {
        this.scanMagikFile(context, inputFile);
        progressReport.nextFile();
    }
    progressReport.stop();
}
Also used : FilePredicate(org.sonar.api.batch.fs.FilePredicate) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) ArrayList(java.util.ArrayList) ProgressReport(org.sonar.squidbridge.ProgressReport) InputFile(org.sonar.api.batch.fs.InputFile)

Example 12 with FileSystem

use of org.sonar.api.batch.fs.FileSystem in project sonar-go by SonarSource.

the class GoCoverageReport method saveFileCoverage.

private static void saveFileCoverage(SensorContext sensorContext, String filePath, List<CoverageStat> coverageStats) throws IOException {
    FileSystem fileSystem = sensorContext.fileSystem();
    InputFile inputFile = findInputFile(filePath, fileSystem);
    if (inputFile != null) {
        LOG.debug("Saving coverage measures for file '{}'", filePath);
        List<String> lines = Arrays.asList(inputFile.contents().split("\\r?\\n"));
        NewCoverage newCoverage = sensorContext.newCoverage().onFile(inputFile);
        FileCoverage fileCoverage = new FileCoverage(coverageStats, lines);
        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", filePath);
    }
}
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 13 with FileSystem

use of org.sonar.api.batch.fs.FileSystem in project sonar-iac by SonarSource.

the class IacSensor method execute.

@Override
public void execute(SensorContext sensorContext) {
    if (!isActive(sensorContext)) {
        return;
    }
    importExternalReports(sensorContext);
    DurationStatistics statistics = new DurationStatistics(sensorContext.config());
    FileSystem fileSystem = sensorContext.fileSystem();
    Iterable<InputFile> inputFiles = fileSystem.inputFiles(mainFilePredicate(sensorContext));
    List<String> filenames = StreamSupport.stream(inputFiles.spliterator(), false).map(InputFile::toString).collect(Collectors.toList());
    ProgressReport progressReport = new ProgressReport("Progress of the " + language.getName() + " analysis", TimeUnit.SECONDS.toMillis(10));
    progressReport.start(filenames);
    boolean success = false;
    Analyzer analyzer = new Analyzer(treeParser(), visitors(sensorContext, statistics), statistics);
    try {
        success = analyzer.analyseFiles(sensorContext, inputFiles, progressReport);
    } finally {
        if (success) {
            progressReport.stop();
        } else {
            progressReport.cancel();
        }
    }
    statistics.log();
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) ProgressReport(org.sonarsource.analyzer.commons.ProgressReport) InputFile(org.sonar.api.batch.fs.InputFile)

Example 14 with FileSystem

use of org.sonar.api.batch.fs.FileSystem in project sonar-php by SonarSource.

the class CoverageResultImporter method saveCoverageMeasure.

/**
 * Saves the required metrics found on the fileNode
 *
 * @param fileNode        the file
 */
private void saveCoverageMeasure(FileNode fileNode, SensorContext context) {
    FileSystem fileSystem = context.fileSystem();
    // PHP supports only absolute paths
    String path = fileNode.getName();
    InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().hasPath(path));
    // targeted file for coverage is not null.
    if (inputFile != null) {
        saveCoverageLineHitsData(fileNode, inputFile, context);
    // Saving the uncovered statements (lines) is no longer needed because coverage metrics are internally derived by the NewCoverage
    } else {
        unresolvedInputFiles.add(path);
    }
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) InputFile(org.sonar.api.batch.fs.InputFile)

Example 15 with FileSystem

use of org.sonar.api.batch.fs.FileSystem in project sonar-findbugs by spotbugs.

the class FindbugsExecutorTest method shoulFailIfNoCompiledClasses.

public void shoulFailIfNoCompiledClasses() throws Exception {
    FileSystem fs = mock(FileSystem.class);
    when(fs.baseDir()).thenReturn(new File("."));
    SimpleConfiguration configuration = new SimpleConfiguration();
    // settings.setProperty(CoreProperties.CORE_VIOLATION_LOCALE_PROPERTY, Locale.getDefault().getDisplayName());
    FindbugsConfiguration conf = new FindbugsConfiguration(fs, configuration, null, null);
    FindbugsExecutor executor = new FindbugsExecutor(conf, fsEmpty, configEmpty);
    assertThrows(IllegalStateException.class, () -> {
        executor.execute();
    });
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) SimpleConfiguration(org.sonar.plugins.findbugs.configuration.SimpleConfiguration) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File)

Aggregations

FileSystem (org.sonar.api.batch.fs.FileSystem)32 InputFile (org.sonar.api.batch.fs.InputFile)29 FilePredicates (org.sonar.api.batch.fs.FilePredicates)19 File (java.io.File)7 FilePredicate (org.sonar.api.batch.fs.FilePredicate)3 SensorContext (org.sonar.api.batch.sensor.SensorContext)3 RecognitionException (com.sonar.sslr.api.RecognitionException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 CheckFactory (org.sonar.api.batch.rule.CheckFactory)2 NewCoverage (org.sonar.api.batch.sensor.coverage.NewCoverage)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)2 Configuration (org.sonar.api.config.Configuration)2 FileLinesContext (org.sonar.api.measures.FileLinesContext)2 FileLinesContextFactory (org.sonar.api.measures.FileLinesContextFactory)2 RuleKey (org.sonar.api.rule.RuleKey)2