Search in sources :

Example 36 with ClientInputFile

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

the class IssueTreeModelBuilderTest method mockFile.

private static ClientInputFile mockFile(String path) {
    ClientInputFile file = mock(ClientInputFile.class);
    when(file.getPath()).thenReturn(path);
    when(file.getCharset()).thenReturn(Charset.defaultCharset());
    when(file.isTest()).thenReturn(false);
    return file;
}
Also used : ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)

Example 37 with ClientInputFile

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

the class ProxyIssueListener method handle.

@Override
public void handle(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue issue) {
    Severity severity;
    ClientInputFile inputFile = issue.getInputFile();
    switch(issue.getSeverity()) {
        case "MINOR":
            severity = Severity.MINOR;
            break;
        case "BLOCKER":
            severity = Severity.BLOCKER;
            break;
        case "INFO":
            severity = Severity.INFO;
            break;
        case "CRITICAL":
            severity = Severity.CRITICAL;
            break;
        case "MAJOR":
        default:
            severity = Severity.MAJOR;
            break;
    }
    Type type = Type.CODE_SMELL;
    if (issue.getType() != null) {
        switch(StringUtils.lowerCase(issue.getType())) {
            case "bug":
                type = Type.BUG;
                break;
            case "vulnerability":
                type = Type.VULNERABILITY;
                break;
            case "code_smell":
            default:
                type = Type.CODE_SMELL;
                break;
        }
    }
    Issue.Builder builder = Issue.newBuilder();
    builder.setRuleKey(issue.getRuleKey()).setRuleName(issue.getRuleName()).setMessage(issue.getMessage()).setSeverity(severity).setType(type).setStartLine(issue.getStartLine() != null ? issue.getStartLine() : 0).setStartLineOffset(issue.getStartLineOffset() != null ? issue.getStartLineOffset() : 0).setEndLine(issue.getEndLine() != null ? issue.getEndLine() : 0).setEndLineOffset(issue.getEndLineOffset() != null ? issue.getEndLineOffset() : 0);
    if (inputFile != null) {
        builder.setFilePath(inputFile.getPath());
        if (inputFile.getClientObject() != null) {
            builder.setUserObject((String) inputFile.getClientObject());
        }
    }
    observer.onNext(builder.build());
}
Also used : Type(org.sonarsource.sonarlint.daemon.proto.SonarlintDaemon.Issue.Type) Issue(org.sonarsource.sonarlint.daemon.proto.SonarlintDaemon.Issue) Severity(org.sonarsource.sonarlint.daemon.proto.SonarlintDaemon.Issue.Severity) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)

Example 38 with ClientInputFile

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

the class ConnectedSonarLintImpl method analyze.

@Override
public void analyze(ConnectedAnalysisReq requestConfig, StreamObserver<org.sonarsource.sonarlint.daemon.proto.SonarlintDaemon.Issue> response) {
    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())));
        }
        ConnectedAnalysisConfiguration config = new ConnectedAnalysisConfiguration(requestConfig.getModuleKey(), baseDir, Paths.get(requestConfig.getWorkDir()), files, requestConfig.getPropertiesMap());
        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) ConnectedAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.connected.ConnectedAnalysisConfiguration) ProxyIssueListener(org.sonarlint.daemon.model.ProxyIssueListener) 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 39 with ClientInputFile

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

the class ProxyIssueListenerTest method testWithSeverity.

private void testWithSeverity(Severity severity) {
    StreamObserver<Issue> observer = mock(StreamObserver.class);
    ClientInputFile inputFile = mock(ClientInputFile.class);
    when(inputFile.getPath()).thenReturn("filename");
    when(inputFile.getClientObject()).thenReturn("obj");
    ProxyIssueListener listener = new ProxyIssueListener(observer);
    org.sonarsource.sonarlint.core.client.api.common.analysis.Issue i = mock(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue.class);
    when(i.getEndLine()).thenReturn(10);
    when(i.getStartLine()).thenReturn(11);
    when(i.getStartLineOffset()).thenReturn(12);
    when(i.getEndLineOffset()).thenReturn(13);
    when(i.getMessage()).thenReturn("msg");
    when(i.getRuleKey()).thenReturn("key");
    when(i.getRuleName()).thenReturn("name");
    when(i.getSeverity()).thenReturn(severity.toString().toUpperCase());
    when(i.getInputFile()).thenReturn(inputFile);
    listener.handle(i);
    ArgumentCaptor<Issue> argument = ArgumentCaptor.forClass(Issue.class);
    verify(observer).onNext(argument.capture());
    Issue captured = argument.getValue();
    assertThat(captured.getEndLine()).isEqualTo(10);
    assertThat(captured.getStartLine()).isEqualTo(11);
    assertThat(captured.getStartLineOffset()).isEqualTo(12);
    assertThat(captured.getEndLineOffset()).isEqualTo(13);
    assertThat(captured.getMessage()).isEqualTo("msg");
    assertThat(captured.getRuleKey()).isEqualTo("key");
    assertThat(captured.getRuleName()).isEqualTo("name");
    assertThat(captured.getSeverity()).isEqualTo(severity);
    assertThat(captured.getFilePath()).isEqualTo("filename");
    assertThat(captured.getUserObject()).isEqualTo("obj");
}
Also used : Issue(org.sonarsource.sonarlint.daemon.proto.SonarlintDaemon.Issue) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)

Example 40 with ClientInputFile

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

the class ProxyIssueListenerTest method dont_fail_if_no_user_obj.

@Test
public void dont_fail_if_no_user_obj() {
    StreamObserver<Issue> observer = mock(StreamObserver.class);
    ClientInputFile inputFile = mock(ClientInputFile.class);
    when(inputFile.getPath()).thenReturn("filename");
    ProxyIssueListener listener = new ProxyIssueListener(observer);
    org.sonarsource.sonarlint.core.client.api.common.analysis.Issue i = mock(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue.class);
    when(i.getEndLine()).thenReturn(10);
    when(i.getStartLine()).thenReturn(11);
    when(i.getStartLineOffset()).thenReturn(12);
    when(i.getEndLineOffset()).thenReturn(13);
    when(i.getMessage()).thenReturn("msg");
    when(i.getRuleKey()).thenReturn("key");
    when(i.getRuleName()).thenReturn("name");
    when(i.getSeverity()).thenReturn("MINOR");
    when(i.getInputFile()).thenReturn(inputFile);
    listener.handle(i);
    ArgumentCaptor<Issue> argument = ArgumentCaptor.forClass(Issue.class);
    verify(observer).onNext(argument.capture());
    Issue captured = argument.getValue();
    assertThat(captured.getEndLine()).isEqualTo(10);
    assertThat(captured.getStartLine()).isEqualTo(11);
    assertThat(captured.getStartLineOffset()).isEqualTo(12);
    assertThat(captured.getEndLineOffset()).isEqualTo(13);
    assertThat(captured.getMessage()).isEqualTo("msg");
    assertThat(captured.getRuleKey()).isEqualTo("key");
    assertThat(captured.getRuleName()).isEqualTo("name");
    assertThat(captured.getSeverity()).isEqualTo(Severity.MINOR);
    assertThat(captured.getFilePath()).isEqualTo("filename");
    assertThat(captured.getUserObject()).isEqualTo("");
}
Also used : Issue(org.sonarsource.sonarlint.daemon.proto.SonarlintDaemon.Issue) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) 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