Skip to content

Commit

Permalink
SLLS-281 try deduplicating
Browse files Browse the repository at this point in the history
  • Loading branch information
sophio-japharidze-sonarsource committed Nov 28, 2024
1 parent 3a6604a commit 72dc88b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,12 @@ private void notifyBackend(List<FileEvent> changes) {
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);
if (event.getType() == FileChangeType.Created) {
addedFiles.add(new ClientFileDto(fileUri, relativePath, folderUri, isTest, StandardCharsets.UTF_8.name(), fsPath, null, null, true));
} else {
changedFiles.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(addedFiles, changedFiles, deletedFileUris);
Expand Down Expand Up @@ -133,10 +125,6 @@ ClientFileDto getClientFileDto(VersionedOpenFile file) {
return clientFileDto.get();
}

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

private boolean isTestFile(VersionedOpenFile file, WorkspaceFolderSettings settings) {
return fileTypeClassifier.isTest(settings, file.getUri(), file.isJava(), () -> javaConfigCache.getOrFetch(file.getUri()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@
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;
Expand All @@ -34,6 +40,7 @@

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 {
Expand Down Expand Up @@ -84,4 +91,29 @@ void should_get_client_file_dto_inside_workspace_folder() {
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");
}
}

0 comments on commit 72dc88b

Please sign in to comment.