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);
}
}
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;
}
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);
}
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);
}
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()));
}
Aggregations