Search in sources :

Example 1 with ClientInputFile

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

the class InputFileBuilderTest method testCreateError.

@Test
public void testCreateError() throws IOException {
    when(langDetection.language(any(InputFile.class))).thenReturn("java");
    ClientInputFile file = new TestClientInputFile(Paths.get("INVALID"), "INVALID", true, StandardCharsets.ISO_8859_1);
    InputFileBuilder builder = new InputFileBuilder(langDetection, metadata);
    exception.expect(IllegalStateException.class);
    exception.expectMessage("Failed to open a stream on file");
    builder.create(file);
}
Also used : ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) TestClientInputFile(org.sonarsource.sonarlint.core.TestClientInputFile) TestClientInputFile(org.sonarsource.sonarlint.core.TestClientInputFile) InputFile(org.sonar.api.batch.fs.InputFile) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) TestClientInputFile(org.sonarsource.sonarlint.core.TestClientInputFile) Test(org.junit.Test)

Example 2 with ClientInputFile

use of org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile 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 3 with ClientInputFile

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

the class StandaloneTest method prepareInputFile.

private ClientInputFile prepareInputFile(String relativePath, String content, final boolean isTest, Charset encoding, @Nullable String language) throws IOException {
    final File file = new File(baseDir, relativePath);
    FileUtils.write(file, content, encoding);
    ClientInputFile inputFile = new TestClientInputFile(baseDir.toPath(), file.toPath(), isTest, encoding);
    return inputFile;
}
Also used : ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) File(java.io.File)

Example 4 with ClientInputFile

use of org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile 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 5 with ClientInputFile

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

the class StandaloneAnalysisConfigurationTest method testToString.

@Test
public void testToString() throws Exception {
    Map<String, String> props = new HashMap<>();
    props.put("sonar.java.libraries", "foo bar");
    final Path srcFile1 = temp.newFile().toPath();
    final Path srcFile2 = temp.newFile().toPath();
    final Path srcFile3 = temp.newFile().toPath();
    ClientInputFile inputFile = new TestClientInputFile(temp.getRoot().toPath(), srcFile1, false, StandardCharsets.UTF_8, null);
    ClientInputFile inputFileWithLanguage = new TestClientInputFile(temp.getRoot().toPath(), srcFile2, false, StandardCharsets.UTF_8, "java");
    ClientInputFile testInputFile = new TestClientInputFile(temp.getRoot().toPath(), srcFile3, true, StandardCharsets.UTF_8, "php");
    Path baseDir = temp.newFolder().toPath();
    Path workDir = temp.newFolder().toPath();
    StandaloneAnalysisConfiguration config = new StandaloneAnalysisConfiguration(baseDir, workDir, Arrays.asList(inputFile, inputFileWithLanguage, testInputFile), props);
    assertThat(config.toString()).isEqualTo("[\n" + "  baseDir: " + baseDir.toString() + "\n" + "  workDir: " + workDir.toString() + "\n" + "  extraProperties: {sonar.java.libraries=foo bar}\n" + "  inputFiles: [\n" + "    " + srcFile1.toString() + " (UTF-8)\n" + "    " + srcFile2.toString() + " (UTF-8) [java]\n" + "    " + srcFile3.toString() + " (UTF-8) [test] [php]\n" + "  ]\n" + "]\n");
    assertThat(config.baseDir()).isEqualTo(baseDir);
    assertThat(config.workDir()).isEqualTo(workDir);
    assertThat(config.inputFiles()).containsExactly(inputFile, inputFileWithLanguage, testInputFile);
    assertThat(config.extraProperties()).containsExactly(entry("sonar.java.libraries", "foo bar"));
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) TestClientInputFile(org.sonarsource.sonarlint.core.client.api.TestClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) TestClientInputFile(org.sonarsource.sonarlint.core.client.api.TestClientInputFile) Test(org.junit.Test)

Aggregations

ClientInputFile (org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)72 Test (org.junit.Test)45 Issue (org.sonarsource.sonarlint.core.client.api.common.analysis.Issue)36 ArrayList (java.util.ArrayList)30 StandaloneAnalysisConfiguration (org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration)25 TestClientInputFile (org.sonarsource.sonarlint.core.TestClientInputFile)19 File (java.io.File)14 ConnectedAnalysisConfiguration (org.sonarsource.sonarlint.core.client.api.connected.ConnectedAnalysisConfiguration)9 Path (java.nio.file.Path)8 HashMap (java.util.HashMap)7 AnalysisResults (org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults)7 LinkedList (java.util.LinkedList)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Collection (java.util.Collection)4 AccessToken (com.intellij.openapi.application.AccessToken)3 Logger (com.intellij.openapi.diagnostic.Logger)3 RangeMarker (com.intellij.openapi.editor.RangeMarker)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Project (com.intellij.openapi.project.Project)3 PsiFile (com.intellij.psi.PsiFile)3