Search in sources :

Example 16 with InputFile

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

the class TestExecutionSensor method processTestFile.

private void processTestFile(InputFile inputFile, SensorContext context) {
    File testExecutionFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + TEST_EXTENSION);
    if (testExecutionFile.exists()) {
        LOG.debug("Processing " + testExecutionFile.getAbsolutePath());
        try {
            List<String> lines = FileUtils.readLines(testExecutionFile, fs.encoding().name());
            int lineNumber = 0;
            MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, inputFile);
            for (String line : lines) {
                lineNumber++;
                if (StringUtils.isBlank(line)) {
                    continue;
                }
                if (line.startsWith("#")) {
                    continue;
                }
                try {
                    Iterator<String> split = Splitter.on(":").split(line).iterator();
                    String name = split.next();
                    String durationStr = split.next();
                    Long duration = StringUtils.isNotBlank(durationStr) ? Long.parseLong(durationStr) : null;
                    String msg = split.next();
                    String stack = split.next();
                    String status = split.next();
                    String type = split.next();
                    MutableTestCase testCase = testPlan.addTestCase(name);
                    testCase.setDurationInMs(duration);
                    testCase.setMessage(msg);
                    testCase.setStackTrace(stack);
                    testCase.setStatus(Status.valueOf(status));
                    testCase.setType(type);
                } catch (Exception e) {
                    throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testExecutionFile.getAbsolutePath(), e);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : MutableTestPlan(org.sonar.api.test.MutableTestPlan) MutableTestCase(org.sonar.api.test.MutableTestCase) IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) IOException(java.io.IOException)

Example 17 with InputFile

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

the class AbstractDeprecatedXooRuleSensor method doAnalyse.

private void doAnalyse(SensorContext context, String languageKey) {
    RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, getRuleKey());
    if (activeRules.find(ruleKey) == null) {
        return;
    }
    for (InputFile inputFile : fs.inputFiles(fs.predicates().hasLanguage(languageKey))) {
        File sonarFile = File.create(inputFile.relativePath());
        sonarFile = context.getResource(sonarFile);
        processFile(inputFile, sonarFile, context, ruleKey, languageKey);
    }
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) InputFile(org.sonar.api.batch.fs.InputFile) File(org.sonar.api.resources.File) InputFile(org.sonar.api.batch.fs.InputFile)

Example 18 with InputFile

use of org.sonar.api.batch.fs.InputFile in project sonarlint-core by SonarSource.

the class InputPathCacheTest method testFiles.

@Test
public void testFiles() {
    InputFile file1 = mock(InputFile.class);
    when(file1.filename()).thenReturn("file1.java");
    when(file1.language()).thenReturn("lang1");
    InputFile file2 = mock(InputFile.class);
    when(file2.filename()).thenReturn("file2");
    when(file2.language()).thenReturn("lang2");
    cache.doAdd(file1);
    cache.doAdd(file2);
    assertThat(cache.inputFiles()).containsOnly(file1, file2);
    // always null
    assertThat(cache.inputFile("file1.java")).isNull();
    assertThat(cache.getFilesByExtension("java")).containsOnly(file1);
    assertThat(cache.getFilesByExtension("")).containsOnly(file2);
    assertThat(cache.getFilesByName("file1.java")).containsOnly(file1);
    assertThat(cache.languages()).containsExactly("lang1", "lang2");
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 19 with InputFile

use of org.sonar.api.batch.fs.InputFile in project sonar-web by SonarSource.

the class WebSensor method saveMetrics.

private static void saveMetrics(SensorContext context, WebSourceCode sourceCode) {
    InputFile inputFile = sourceCode.inputFile();
    saveComplexityDistribution(context, sourceCode);
    for (Map.Entry<Metric<Integer>, Integer> entry : sourceCode.getMeasures().entrySet()) {
        context.<Integer>newMeasure().on(inputFile).forMetric(entry.getKey()).withValue(entry.getValue()).save();
    }
    for (WebIssue issue : sourceCode.getIssues()) {
        NewIssue newIssue = context.newIssue().forRule(issue.ruleKey()).gap(issue.cost());
        Integer line = issue.line();
        NewIssueLocation location = newIssue.newLocation().on(inputFile).message(issue.message());
        if (line != null) {
            location.at(inputFile.selectLine(line));
        }
        newIssue.at(location);
        newIssue.save();
    }
}
Also used : NewIssueLocation(org.sonar.api.batch.sensor.issue.NewIssueLocation) WebIssue(org.sonar.plugins.web.checks.WebIssue) NewIssue(org.sonar.api.batch.sensor.issue.NewIssue) Metric(org.sonar.api.measures.Metric) Map(java.util.Map) InputFile(org.sonar.api.batch.fs.InputFile)

Example 20 with InputFile

use of org.sonar.api.batch.fs.InputFile 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)

Aggregations

InputFile (org.sonar.api.batch.fs.InputFile)221 Test (org.junit.Test)120 File (java.io.File)100 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)58 AnalysisResult (org.sonar.scanner.mediumtest.AnalysisResult)32 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)24 FileSystem (org.sonar.api.batch.fs.FileSystem)21 FilePredicates (org.sonar.api.batch.fs.FilePredicates)19 IOException (java.io.IOException)18 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)15 List (java.util.List)14 Test (org.junit.jupiter.api.Test)12 BlameLine (org.sonar.api.batch.scm.BlameLine)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)9 Before (org.junit.Before)9 Path (java.nio.file.Path)8 HashMap (java.util.HashMap)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8