Search in sources :

Example 6 with AnalysisResults

use of org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults in project sonarlint-intellij by SonarSource.

the class SonarLintAnalyzer method analyzeModule.

public AnalysisResults analyzeModule(Module module, Collection<VirtualFile> filesToAnalyze, IssueListener listener, ProgressMonitor progressMonitor) {
    // Configure plugin properties. Nothing might be done if there is no configurator available for the extensions loaded in runtime.
    Map<String, String> pluginProps = new HashMap<>();
    AnalysisConfigurator[] analysisConfigurators = AnalysisConfigurator.EP_NAME.getExtensions();
    if (analysisConfigurators.length > 0) {
        for (AnalysisConfigurator config : analysisConfigurators) {
            console.debug("Configuring analysis with " + config.getClass().getName());
            pluginProps.putAll(config.configure(module));
        }
    } else {
        console.info("No analysis configurator found");
    }
    // configure files
    VirtualFileTestPredicate testPredicate = SonarLintUtils.get(module, VirtualFileTestPredicate.class);
    List<ClientInputFile> inputFiles = getInputFiles(module, testPredicate, filesToAnalyze);
    // Analyze
    long start = System.currentTimeMillis();
    try {
        SonarLintFacade facade = projectBindingManager.getFacade(true);
        String what;
        if (filesToAnalyze.size() == 1) {
            what = "'" + filesToAnalyze.iterator().next().getName() + "'";
        } else {
            what = Integer.toString(filesToAnalyze.size()) + " files";
        }
        console.info("Analysing " + what + "...");
        if (facade.requiresSavingFiles()) {
            console.debug("Saving files");
            LOG.assertTrue(!ApplicationManager.getApplication().isReadAccessAllowed(), "Should not be in a read action (risk of dead lock)");
            ApplicationManager.getApplication().invokeAndWait(() -> SonarLintUtils.saveFiles(filesToAnalyze), ModalityState.defaultModalityState());
        }
        AnalysisResults result = facade.startAnalysis(inputFiles, listener, pluginProps, progressMonitor);
        console.debug("Done in " + (System.currentTimeMillis() - start) + "ms\n");
        if (filesToAnalyze.size() == 1) {
            telemetry.analysisDoneOnSingleFile(filesToAnalyze.iterator().next().getExtension(), (int) (System.currentTimeMillis() - start));
        } else {
            telemetry.analysisDoneOnMultipleFiles();
        }
        return result;
    } catch (InvalidBindingException e) {
        // should not happen, as analysis should not have been submitted in this case.
        throw new IllegalStateException(e);
    }
}
Also used : InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) HashMap(java.util.HashMap) SonarLintFacade(org.sonarlint.intellij.core.SonarLintFacade) AnalysisResults(org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)

Example 7 with AnalysisResults

use of org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults in project sonarlint-intellij by SonarSource.

the class SonarLintTask method analyze.

private List<AnalysisResults> analyze(Project project, ProgressIndicator indicator, AccumulatorIssueListener listener) {
    SonarLintAnalyzer analyzer = SonarLintUtils.get(project, SonarLintAnalyzer.class);
    indicator.setIndeterminate(true);
    int numModules = job.filesPerModule().keySet().size();
    String suffix = "";
    if (numModules > 1) {
        suffix = String.format(" in %d modules", numModules);
    }
    int numFiles = job.allFiles().size();
    if (numFiles > 1) {
        indicator.setText("Running SonarLint Analysis for " + numFiles + " files" + suffix);
    } else {
        indicator.setText("Running SonarLint Analysis for '" + getFileName(job.allFiles().iterator().next()) + "'");
    }
    LOGGER.info(indicator.getText());
    ProgressMonitor progressMonitor = new TaskProgressMonitor(indicator);
    List<AnalysisResults> results = new LinkedList<>();
    for (Map.Entry<Module, Collection<VirtualFile>> e : job.filesPerModule().entrySet()) {
        results.add(analyzer.analyzeModule(e.getKey(), e.getValue(), listener, progressMonitor));
        checkCanceled(indicator, myProject);
    }
    indicator.startNonCancelableSection();
    return results;
}
Also used : ProgressMonitor(org.sonarsource.sonarlint.core.client.api.common.ProgressMonitor) TaskProgressMonitor(org.sonarlint.intellij.util.TaskProgressMonitor) AnalysisResults(org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults) Collection(java.util.Collection) TaskProgressMonitor(org.sonarlint.intellij.util.TaskProgressMonitor) Module(com.intellij.openapi.module.Module) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 8 with AnalysisResults

use of org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults in project sonarlint-intellij by SonarSource.

the class StandaloneSonarLintFacadeTest method should_start_analysis.

@Test
public void should_start_analysis() {
    AnalysisResults results = mock(AnalysisResults.class);
    when(engine.analyze(any(StandaloneAnalysisConfiguration.class), any(IssueListener.class), any(LogOutput.class), any(ProgressMonitor.class))).thenReturn(results);
    assertThat(facade.startAnalysis(Collections.emptyList(), mock(IssueListener.class), Collections.emptyMap(), mock(ProgressMonitor.class))).isEqualTo(results);
}
Also used : ProgressMonitor(org.sonarsource.sonarlint.core.client.api.common.ProgressMonitor) IssueListener(org.sonarsource.sonarlint.core.client.api.common.analysis.IssueListener) AnalysisResults(org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) LogOutput(org.sonarsource.sonarlint.core.client.api.common.LogOutput) Test(org.junit.Test)

Example 9 with AnalysisResults

use of org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults in project sonarlint-core by SonarSource.

the class ConnectedModeTest method semanticErrorJava.

@Test
public void semanticErrorJava() throws IOException {
    String fileContent = "package its;public class MyTest {int a;int a;}";
    Path testFile = temp.newFile("MyTestSemanticError.java").toPath();
    Files.write(testFile, fileContent.getBytes(StandardCharsets.UTF_8));
    updateGlobal();
    updateModule(PROJECT_KEY_JAVA);
    SaveIssueListener issueListener = new SaveIssueListener();
    AnalysisResults results = engine.analyze(createAnalysisConfiguration(PROJECT_KEY_JAVA, testFile.toString()), issueListener, null, null);
    assertThat(results.failedAnalysisFiles()).hasSize(1);
}
Also used : Path(java.nio.file.Path) AnalysisResults(org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults) Test(org.junit.Test)

Example 10 with AnalysisResults

use of org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTest method testJavaSurefireDontCrashAnalysis.

@Test
public void testJavaSurefireDontCrashAnalysis() throws Exception {
    File surefireReport = new File(baseDir, "reports/TEST-FooTest.xml");
    FileUtils.write(surefireReport, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<testsuite name=\"FooTest\" time=\"0.121\" tests=\"1\" errors=\"0\" skipped=\"0\" failures=\"0\">\n" + "<testcase name=\"errorAnalysis\" classname=\"FooTest\" time=\"0.031\"/>\n" + "</testsuite>");
    ClientInputFile inputFile = prepareInputFile("Foo.java", "public class Foo {\n" + "  public void foo() {\n" + "    int x;\n" + "    System.out.println(\"Foo\");\n" + "    System.out.println(\"Foo\"); //NOSONAR\n" + "  }\n" + "}", false);
    ClientInputFile inputFileTest = prepareInputFile("FooTest.java", "public class FooTest {\n" + "  public void testFoo() {\n" + "  }\n" + "}", true);
    final List<Issue> issues = new ArrayList<>();
    AnalysisResults results = sonarlint.analyze(new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile, inputFileTest), ImmutableMap.of("sonar.junit.reportsPath", "reports/")), issue -> issues.add(issue), null, null);
    assertThat(results.fileCount()).isEqualTo(2);
    assertThat(issues).extracting("ruleKey", "startLine", "inputFile.path").containsOnly(tuple("squid:S106", 4, inputFile.getPath()), tuple("squid:S1220", null, inputFile.getPath()), tuple("squid:S1481", 3, inputFile.getPath()), tuple("squid:S2187", 1, inputFileTest.getPath()));
}
Also used : Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) AnalysisResults(org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults) ArrayList(java.util.ArrayList) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) TestClientInputFile(org.sonarsource.sonarlint.core.TestClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) TestClientInputFile(org.sonarsource.sonarlint.core.TestClientInputFile) File(java.io.File) Test(org.junit.Test)

Aggregations

AnalysisResults (org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults)15 Test (org.junit.Test)9 ClientInputFile (org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)7 StandaloneAnalysisConfiguration (org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration)6 Path (java.nio.file.Path)4 ProgressMonitor (org.sonarsource.sonarlint.core.client.api.common.ProgressMonitor)4 Issue (org.sonarsource.sonarlint.core.client.api.common.analysis.Issue)4 ArrayList (java.util.ArrayList)3 Module (com.intellij.openapi.module.Module)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 TaskProgressMonitor (org.sonarlint.intellij.util.TaskProgressMonitor)2 TestClientInputFile (org.sonarsource.sonarlint.core.TestClientInputFile)2 LogOutput (org.sonarsource.sonarlint.core.client.api.common.LogOutput)2 IssueListener (org.sonarsource.sonarlint.core.client.api.common.analysis.IssueListener)2 LoggedErrorHandler (org.sonarsource.sonarlint.core.util.LoggedErrorHandler)2 ProgressWrapper (org.sonarsource.sonarlint.core.util.ProgressWrapper)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1