diff --git a/src/main/java/org/sonarsource/sonarlint/ls/folders/ModuleEventsProcessor.java b/src/main/java/org/sonarsource/sonarlint/ls/folders/ModuleEventsProcessor.java index 884f788f..1134467b 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/folders/ModuleEventsProcessor.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/folders/ModuleEventsProcessor.java @@ -79,20 +79,12 @@ private void notifyBackend(List 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); @@ -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())); } diff --git a/src/test/java/org/sonarsource/sonarlint/ls/folders/ModuleEventsProcessorTest.java b/src/test/java/org/sonarsource/sonarlint/ls/folders/ModuleEventsProcessorTest.java index 4918fbc1..de9f8193 100644 --- a/src/test/java/org/sonarsource/sonarlint/ls/folders/ModuleEventsProcessorTest.java +++ b/src/test/java/org/sonarsource/sonarlint/ls/folders/ModuleEventsProcessorTest.java @@ -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; @@ -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 { @@ -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"); + } } \ No newline at end of file