Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLLS-281 distinguish added and modified files #415

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<properties>
<jdk.min.version>17</jdk.min.version>
<sonarlint.core.version>10.11.0.79608</sonarlint.core.version>
<sonarlint.core.version>10.11.0.79653</sonarlint.core.version>
<!-- Version used by Xodus -->
<kotlin.version>1.6.10</kotlin.version>
<!-- analyzers used for tests -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public void didChange(DidChangeTextDocumentParams params) {
} else {
// VSCode sends us full file content in the change event
CompletableFutures.computeAsync(cancelChecker -> {
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(file.get());
moduleEventsProcessor.notifyBackendWithUpdatedContent(file.get());
return null;
});
}
Expand Down Expand Up @@ -587,7 +587,7 @@ public void didChange(DidChangeNotebookDocumentParams params) {
} else {
var file = openNotebook.get().asVersionedOpenFile();
CompletableFutures.computeAsync(cancelChecker -> {
moduleEventsProcessor.notifyBackendWithFileLanguageAndContent(file);
moduleEventsProcessor.notifyBackendWithUpdatedContent(file);
return null;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ public CompletableFuture<GetAllProjectsResponse> getAllProjects(Either<Transient
return initializedBackend().getConnectionService().getAllProjects(new GetAllProjectsParams(transientConnection));
}

public void updateFileSystem(List<URI> deletedFileUris, List<ClientFileDto> addedOrChangedFiles) {
initializedBackend().getFileService().didUpdateFileSystem(new DidUpdateFileSystemParams(deletedFileUris, addedOrChangedFiles));
public void updateFileSystem(List<ClientFileDto> addedFiles, List<ClientFileDto> changedFiles, List<URI> deletedFileUris) {
initializedBackend().getFileService().didUpdateFileSystem(new DidUpdateFileSystemParams(addedFiles, changedFiles, deletedFileUris));
}

public CompletableFuture<ListAllResponse> getAllTaints(String folderUri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.lsp4j.FileChangeType;
import org.eclipse.lsp4j.FileEvent;
import org.jetbrains.annotations.NotNull;
import org.sonarsource.sonarlint.core.commons.api.SonarLanguage;
import org.sonarsource.sonarlint.core.rpc.protocol.common.ClientFileDto;
import org.sonarsource.sonarlint.core.rpc.protocol.common.Language;
Expand Down Expand Up @@ -70,29 +72,39 @@ public void didChangeWatchedFiles(List<FileEvent> changes) {

private void notifyBackend(List<FileEvent> changes) {
List<URI> deletedFileUris = new ArrayList<>();
List<ClientFileDto> addedOrChangedFiles = new ArrayList<>();
List<ClientFileDto> addedFiles = new ArrayList<>();
List<ClientFileDto> changedFiles = new ArrayList<>();
changes.forEach(event -> {
var fileUri = URI.create(event.getUri());
if (event.getType() == FileChangeType.Deleted) {
deletedFileUris.add(fileUri);
} else {
workspaceFoldersManager.findFolderForFile(fileUri)
.ifPresent(folder -> {
var settings = folder.getSettings();
var baseDir = folder.getRootPath();
var fsPath = Paths.get(fileUri);
var relativePath = baseDir.relativize(fsPath);
var folderUri = folder.getUri().toString();
var isTest = isTestFile(fileUri, settings);
addedOrChangedFiles.add(new ClientFileDto(fileUri, relativePath, folderUri, isTest, StandardCharsets.UTF_8.name(), fsPath, null, null, true));
});
var clientFileDto = getClientFileDto(new VersionedOpenFile(fileUri, "", 0, ""));
if (event.getType() == FileChangeType.Created) {
addedFiles.add(clientFileDto);
} else {
changedFiles.add(clientFileDto);
}
}
});
backendServiceFacade.getBackendService().updateFileSystem(deletedFileUris, addedOrChangedFiles);
backendServiceFacade.getBackendService().updateFileSystem(addedFiles, changedFiles, deletedFileUris);
}

public void notifyBackendWithFileLanguageAndContent(VersionedOpenFile file) {
List<ClientFileDto> filesToNotify = new ArrayList<>();
var openedFileDto = getClientFileDto(file);
// We are simply enriching already added files with language and content information; The files were not actually modified
// i.e. didOpen
backendServiceFacade.getBackendService().updateFileSystem(List.of(openedFileDto), List.of(), List.of());
}

public void notifyBackendWithUpdatedContent(VersionedOpenFile file) {
var changedFileDto = getClientFileDto(file);
backendServiceFacade.getBackendService().updateFileSystem(List.of(), List.of(changedFileDto), List.of());
}

@NotNull
ClientFileDto getClientFileDto(VersionedOpenFile file) {
AtomicReference<ClientFileDto> clientFileDto = new AtomicReference<>();
var fileUri = file.getUri();
var fsPath = Paths.get(fileUri);
SonarLanguage sqLanguage = AnalysisClientInputFile.toSqLanguage(file.getLanguageId().toLowerCase(Locale.ROOT));
Expand All @@ -103,18 +115,14 @@ public void notifyBackendWithFileLanguageAndContent(VersionedOpenFile file) {
var relativePath = baseDir.relativize(fsPath);
var folderUri = folder.getUri().toString();
var isTest = isTestFile(file, settings);
filesToNotify.add(new ClientFileDto(fileUri, relativePath, folderUri, isTest, StandardCharsets.UTF_8.name(),
clientFileDto.set(new ClientFileDto(fileUri, relativePath, folderUri, isTest, StandardCharsets.UTF_8.name(),
fsPath, file.getContent(), sqLanguage != null ? Language.valueOf(sqLanguage.name()) : null, true));
}, () -> {
var isTest = isTestFile(file, settingsManager.getCurrentDefaultFolderSettings());
filesToNotify.add(new ClientFileDto(fileUri, fsPath, ROOT_CONFIGURATION_SCOPE, isTest, StandardCharsets.UTF_8.name(),
clientFileDto.set(new ClientFileDto(fileUri, fsPath, ROOT_CONFIGURATION_SCOPE, isTest, StandardCharsets.UTF_8.name(),
fsPath, file.getContent(), sqLanguage != null ? Language.valueOf(sqLanguage.name()) : null, true));
});
backendServiceFacade.getBackendService().updateFileSystem(List.of(), filesToNotify);
}

private boolean isTestFile(URI fileUri, WorkspaceFolderSettings settings) {
return fileTypeClassifier.isTest(settings, fileUri, false, () -> javaConfigCache.getOrFetch(fileUri));
return clientFileDto.get();
}

private boolean isTestFile(VersionedOpenFile file, WorkspaceFolderSettings settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ void shouldForwardOpenIssueRequest() {
var fileUri = fileInAWorkspaceFolderPath.toUri();
var textRangeDto = new TextRangeDto(1, 2, 3, 4);
var issueDetailsDto = new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", FILE_PYTHON, "branch", "PR", "this is wrong",
"issueKey", FILE_PYTHON, "this is wrong",
"29.09.2023", "print('ddd')", false, List.of());
var showIssueParams = new ShowIssueParams(fileUri.toString(), issueDetailsDto);

Expand All @@ -625,7 +625,7 @@ void shouldForwardOpenIssueRequestWithoutRuleDescriptionWhenBindingDoesNotExist(
var fileUri = fileInAWorkspaceFolderPath.toUri();
var textRangeDto = new TextRangeDto(1, 2, 3, 4);
var issueDetailsDto = new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", FILE_PYTHON, "bb", null, "this is wrong", "29.09.2023", "print('ddd')",
"issueKey", FILE_PYTHON, "this is wrong", "29.09.2023", "print('ddd')",
false, List.of());
when(bindingManager.getBindingIfExists(fileUri))
.thenReturn(Optional.empty());
Expand All @@ -643,7 +643,7 @@ void shouldForwardOpenIssueRequestWithRuleDescriptionWhenBindingDoesExist() {
var fileUri = fileInAWorkspaceFolderPath.toUri();
var textRangeDto = new TextRangeDto(1, 2, 3, 4);
var issueDetailsDto = new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", FILE_PYTHON, "bb", null, "this is wrong", "29.09.2023", "print('ddd')",
"issueKey", FILE_PYTHON, "this is wrong", "29.09.2023", "print('ddd')",
false, List.of());
when(bindingManager.getBindingIfExists(fileUri))
.thenReturn(Optional.of(new ProjectBinding("connectionId", "projectKey")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void shouldBuildCommandParamsFromShowIssueParams() {

var textRangeDto = new TextRangeDto(1, 0, 1, 13);
var showIssueParams = new ShowIssueParams(workspaceFolderPath.toUri().toString(), new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", Path.of("myFile.py"), "branch", "pr", "this is wrong",
"issueKey", Path.of("myFile.py"), "this is wrong",
"29.09.2023", "print('1234')", false, flows));

var result = new ShowAllLocationsCommand.Param(showIssueParams, "connectionId", true);
Expand All @@ -152,7 +152,7 @@ void shouldBuildCommandParamsFromShowIssueParams() {
void shouldBuildCommandParamsFromShowIssueParamsForFileLevelIssue() {
var textRangeDto = new TextRangeDto(0, 0, 0, 0);
var showIssueParams = new ShowIssueParams(workspaceFolderPath.toUri().toString(), new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", Path.of("myFile.py"), "branch", null, "this is wrong",
"issueKey", Path.of("myFile.py"), "this is wrong",
"29.09.2023", """
print('1234')
print('aa')
Expand All @@ -168,7 +168,7 @@ void shouldBuildCommandParamsFromShowIssueParamsForFileLevelIssue() {
void shouldBuildCommandParamsFromShowIssueParamsForInvalidTextRange() {
var textRangeDto = new TextRangeDto(-1, 0, -2, 0);
var showIssueParams = new ShowIssueParams(workspaceFolderPath.toUri().toString(), new IssueDetailsDto(textRangeDto, "rule:S1234",
"issueKey", Path.of("myFile.py"), "bb", "1234", "this is wrong",
"issueKey", Path.of("myFile.py"), "this is wrong",
"29.09.2023", "print('1234')", false, List.of()));

var result = new ShowAllLocationsCommand.Param(showIssueParams, "connectionId", true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* SonarLint Language Server
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.sonarlint.ls.folders;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import org.eclipse.lsp4j.FileChangeType;
import org.eclipse.lsp4j.FileEvent;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.sonarsource.sonarlint.core.rpc.protocol.common.ClientFileDto;
import org.sonarsource.sonarlint.core.rpc.protocol.common.Language;
import org.sonarsource.sonarlint.ls.backend.BackendService;
import org.sonarsource.sonarlint.ls.backend.BackendServiceFacade;
import org.sonarsource.sonarlint.ls.file.FileTypeClassifier;
import org.sonarsource.sonarlint.ls.file.VersionedOpenFile;
import org.sonarsource.sonarlint.ls.java.JavaConfigCache;
import org.sonarsource.sonarlint.ls.settings.SettingsManager;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class ModuleEventsProcessorTest {
ModuleEventsProcessor moduleEventsProcessor;
WorkspaceFoldersManager workspaceFoldersManager = mock(WorkspaceFoldersManager.class);
FileTypeClassifier fileTypeClassifier = mock(FileTypeClassifier.class);
JavaConfigCache javaConfigCache = mock(JavaConfigCache.class);
BackendServiceFacade backendServiceFacade = mock(BackendServiceFacade.class);
SettingsManager settingsManager = mock(SettingsManager.class);

@BeforeEach
void setUp() {
moduleEventsProcessor = new ModuleEventsProcessor(workspaceFoldersManager, fileTypeClassifier, javaConfigCache, backendServiceFacade, settingsManager);
}

@Test
void should_get_client_file_dto_outside_workspace_folder() {
var content = "print('Hello, World!')";
var testFile1 = new VersionedOpenFile(URI.create("file:///tmp/test.py"), "python", 1, content);

var clientFileDto = moduleEventsProcessor.getClientFileDto(testFile1);

assertThat(clientFileDto).isNotNull();
assertThat(clientFileDto.getDetectedLanguage()).isEqualTo(Language.PYTHON);
assertThat(clientFileDto.getContent()).isEqualTo(content);
assertThat(clientFileDto.getConfigScopeId()).isEqualTo(BackendServiceFacade.ROOT_CONFIGURATION_SCOPE);
assertThat(clientFileDto.getUri()).hasToString(testFile1.getUri().toString());
assertThat(clientFileDto.getCharset()).isEqualTo(StandardCharsets.UTF_8.name());
}

@Test
void should_get_client_file_dto_inside_workspace_folder() {
var content = "print('Hello, World!')";
var fileUri = URI.create("file:///tmp/test.py");
var mockFolder = mock(WorkspaceFolderWrapper.class);
when(workspaceFoldersManager.findFolderForFile(fileUri)).thenReturn(Optional.of(mockFolder));
when(mockFolder.getRootPath()).thenReturn(Path.of(URI.create("file:///tmp/").getPath()));
when(mockFolder.getUri()).thenReturn(URI.create("file:///tmp/"));
when(mockFolder.getSettings()).thenReturn(null);
var testFile1 = new VersionedOpenFile(fileUri, "python", 1, content);

var clientFileDto = moduleEventsProcessor.getClientFileDto(testFile1);

assertThat(clientFileDto).isNotNull();
assertThat(clientFileDto.getDetectedLanguage()).isEqualTo(Language.PYTHON);
assertThat(clientFileDto.getContent()).isEqualTo(content);
assertThat(clientFileDto.getConfigScopeId()).isEqualTo("file:///tmp/");
assertThat(clientFileDto.getUri()).hasToString(testFile1.getUri().toString());
assertThat(clientFileDto.getCharset()).isEqualTo(StandardCharsets.UTF_8.name());
}

@Test
void should_notify_backend_on_watched_files_change() {
var backend = mock(BackendService.class);
var events = List.of(
new FileEvent("file:///tmp/test1.py", FileChangeType.Created),
new FileEvent("file:///tmp/test2.py", FileChangeType.Changed),
new FileEvent("file:///tmp/test3.py", FileChangeType.Deleted)
);
when(backendServiceFacade.getBackendService()).thenReturn(backend);

moduleEventsProcessor.didChangeWatchedFiles(events);

var deletedFilesCaptor = ArgumentCaptor.forClass(List.class);
var addedFilesCaptor = ArgumentCaptor.forClass(List.class);
var changedFilesCaptor = ArgumentCaptor.forClass(List.class);

verify(backend).updateFileSystem(addedFilesCaptor.capture(), changedFilesCaptor.capture(), deletedFilesCaptor.capture());
assertThat(addedFilesCaptor.getValue()).hasSize(1);
assertThat(((ClientFileDto) addedFilesCaptor.getValue().get(0)).getUri()).hasToString("file:///tmp/test1.py");
assertThat(changedFilesCaptor.getValue()).hasSize(1);
assertThat(((ClientFileDto) changedFilesCaptor.getValue().get(0)).getUri()).hasToString("file:///tmp/test2.py");
assertThat(deletedFilesCaptor.getValue()).hasSize(1);
assertThat(deletedFilesCaptor.getValue().get(0)).hasToString("file:///tmp/test3.py");
}
}