Search in sources :

Example 1 with StandaloneAnalysisConfiguration

use of org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration in project sonarlint-core by SonarSource.

the class StandaloneTest method globalExtension.

@Test
public void globalExtension() throws Exception {
    ClientInputFile inputFile = prepareInputFile("foo.glob", "foo", false);
    final List<Issue> issues = new ArrayList<>();
    sonarlint.analyze(new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile), ImmutableMap.of()), issue -> issues.add(issue), null, null);
    assertThat(issues).extracting("ruleKey", "inputFile.path", "message").containsOnly(tuple("global:inc", inputFile.getPath(), "Issue number 0"));
    issues.clear();
    sonarlint.analyze(new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile), ImmutableMap.of()), issue -> issues.add(issue), null, null);
    assertThat(issues).extracting("ruleKey", "inputFile.path", "message").containsOnly(tuple("global:inc", inputFile.getPath(), "Issue number 1"));
}
Also used : Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) ArrayList(java.util.ArrayList) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) Test(org.junit.Test)

Example 2 with StandaloneAnalysisConfiguration

use of org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration in project sonarlint-core by SonarSource.

the class SonarLintLanguageServer method analyze.

private void analyze(URI uri, String content) {
    Map<URI, PublishDiagnosticsParams> files = new HashMap<>();
    files.put(uri, newPublishDiagnostics(uri));
    Path baseDir = workspaceRoot != null ? Paths.get(workspaceRoot) : Paths.get(uri).getParent();
    Objects.requireNonNull(baseDir);
    Objects.requireNonNull(engine);
    StandaloneAnalysisConfiguration configuration = new StandaloneAnalysisConfiguration(baseDir, baseDir.resolve(".sonarlint"), Arrays.asList(new DefaultClientInputFile(baseDir, uri, content, userSettings.testFilePattern, languageIdPerFileURI.get(uri))), userSettings.analyzerProperties);
    debug("Analysis triggered on " + uri + " with configuration: \n" + configuration.toString());
    long start = System.currentTimeMillis();
    AnalysisResults analysisResults = engine.analyze(configuration, issue -> {
        ClientInputFile inputFile = issue.getInputFile();
        if (inputFile != null) {
            URI uri1 = inputFile.getClientObject();
            PublishDiagnosticsParams publish = files.computeIfAbsent(uri1, SonarLintLanguageServer::newPublishDiagnostics);
            convert(issue).ifPresent(publish.getDiagnostics()::add);
        }
    }, logOutput, null);
    telemetry.analysisDoneOnSingleFile(StringUtils.substringAfterLast(uri.toString(), "."), (int) (System.currentTimeMillis() - start));
    // Ignore files with parsing error
    analysisResults.failedAnalysisFiles().stream().map(ClientInputFile::getClientObject).forEach(files::remove);
    files.values().forEach(client::publishDiagnostics);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) URI(java.net.URI) AnalysisResults(org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults) PublishDiagnosticsParams(org.eclipse.lsp4j.PublishDiagnosticsParams) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)

Example 3 with StandaloneAnalysisConfiguration

use of org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration in project sonarlint-core by SonarSource.

the class StandaloneSonarLintImpl method analyze.

@Override
public void analyze(AnalysisReq requestConfig, StreamObserver<Issue> response) {
    if (engine == null) {
        response.onError(new IllegalStateException("Not registered"));
        return;
    }
    try {
        List<ClientInputFile> files = new LinkedList<>();
        List<InputFile> requestFiles = requestConfig.getFileList();
        Path baseDir = Paths.get(requestConfig.getBaseDir());
        for (InputFile f : requestFiles) {
            files.add(new DefaultClientInputFile(baseDir, Paths.get(f.getPath()), f.getIsTest(), Charset.forName(f.getCharset()), f.getUserObject(), trimToNull(f.getLanguage())));
        }
        StandaloneAnalysisConfiguration config = new StandaloneAnalysisConfiguration(baseDir, Paths.get(requestConfig.getWorkDir()), files, requestConfig.getPropertiesMap());
        logOutput.log("Analysis configuration:\n" + config.toString(), Level.DEBUG);
        engine.analyze(config, new ProxyIssueListener(response), logOutput, null);
        response.onCompleted();
    } catch (Exception e) {
        LOGGER.error("Error analyzing", e);
        response.onError(e);
    }
}
Also used : Path(java.nio.file.Path) DefaultClientInputFile(org.sonarlint.daemon.model.DefaultClientInputFile) ProxyIssueListener(org.sonarlint.daemon.model.ProxyIssueListener) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) DefaultClientInputFile(org.sonarlint.daemon.model.DefaultClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) LinkedList(java.util.LinkedList) DefaultClientInputFile(org.sonarlint.daemon.model.DefaultClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) InputFile(org.sonarsource.sonarlint.daemon.proto.SonarlintDaemon.InputFile)

Example 4 with StandaloneAnalysisConfiguration

use of org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTest method simplePython.

@Test
public void simplePython() throws Exception {
    ClientInputFile inputFile = prepareInputFile("foo.py", "def my_function(name):\n" + "    print \"Hello\"\n" + "    print \"world!\" # NOSONAR\n" + "\n", false);
    final List<Issue> issues = new ArrayList<>();
    sonarlint.analyze(new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile), ImmutableMap.of()), issue -> issues.add(issue), null, null);
    assertThat(issues).extracting("ruleKey", "startLine", "inputFile.path").containsOnly(tuple("python:PrintStatementUsage", 2, inputFile.getPath()));
}
Also used : Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) 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) Test(org.junit.Test)

Example 5 with StandaloneAnalysisConfiguration

use of org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTest method simpleJavaWithBytecode.

@Test
public void simpleJavaWithBytecode() throws Exception {
    ClientInputFile inputFile = TestUtils.createInputFile(new File("src/test/projects/java-with-bytecode/src/Foo.java").getAbsoluteFile().toPath(), "src/Foo.java", false);
    final List<Issue> issues = new ArrayList<>();
    sonarlint.analyze(new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Arrays.asList(inputFile), ImmutableMap.of("sonar.java.binaries", new File("src/test/projects/java-with-bytecode/bin").getAbsolutePath())), issue -> issues.add(issue), null, null);
    assertThat(issues).extracting("ruleKey", "startLine", "inputFile.path").containsOnly(tuple("squid:S106", 5, inputFile.getPath()), tuple("squid:S1220", null, inputFile.getPath()), tuple("squid:UnusedPrivateMethod", 8, inputFile.getPath()), tuple("squid:S1186", 8, inputFile.getPath()));
}
Also used : Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) 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

StandaloneAnalysisConfiguration (org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration)29 ClientInputFile (org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)26 ArrayList (java.util.ArrayList)25 Test (org.junit.Test)24 Issue (org.sonarsource.sonarlint.core.client.api.common.analysis.Issue)23 TestClientInputFile (org.sonarsource.sonarlint.core.TestClientInputFile)15 AnalysisResults (org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults)5 File (java.io.File)4 Path (java.nio.file.Path)3 HashMap (java.util.HashMap)3 RuleDetails (org.sonarsource.sonarlint.core.client.api.common.RuleDetails)2 URI (java.net.URI)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1