Skip to content

Commit

Permalink
Only update each project once
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed Feb 27, 2020
1 parent 27a7311 commit 3102096
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -315,27 +316,33 @@ public void updateAllBindings() {
entry.getValue().ifPresent(engine -> updateGlobalStorageAndLogResults(serverConfiguration, engine));
}
});
updateBindingIfNecessary(null);
Map<String, Set<String>> updatedProjectsByServer = new HashMap<>();
updateBindingIfNecessary(null, updatedProjectsByServer);

foldersManager.getAll().forEach(this::updateBindingIfNecessary);
foldersManager.getAll().forEach(f -> updateBindingIfNecessary(f, updatedProjectsByServer));

client.showMessage(new MessageParams(MessageType.Info, "All SonarLint bindings succesfully updated"));
}

private void updateBindingIfNecessary(@Nullable WorkspaceFolderWrapper folder) {
private void updateBindingIfNecessary(@Nullable WorkspaceFolderWrapper folder, Map<String, Set<String>> updatedProjectsByServer) {
WorkspaceFolderSettings folderSettings = folder != null ? folder.getSettings() : settingsManager.getCurrentDefaultFolderSettings();
if (folderSettings.hasBinding()) {
String serverId = requireNonNull(folderSettings.getConnectionId());
ServerConfiguration serverConfiguration = createServerConfiguration(serverId);
if (serverConfiguration == null) {
LOG.error("Invalid binding for '{}'", folder != null ? folder.getRootPath() : "default folder");
return;
}
Optional<ConnectedSonarLintEngine> engineOpt = getOrCreateConnectedEngine(serverId, serverConfiguration, true);
if (!engineOpt.isPresent()) {
return;
String projectKey = requireNonNull(folderSettings.getProjectKey());
Set<String> alreadyUpdatedProjects = updatedProjectsByServer.get(serverId);
if (alreadyUpdatedProjects == null || !alreadyUpdatedProjects.contains(projectKey)) {
ServerConfiguration serverConfiguration = createServerConfiguration(serverId);
if (serverConfiguration == null) {
LOG.error("Invalid binding for '{}'", folder != null ? folder.getRootPath() : "default folder");
return;
}
Optional<ConnectedSonarLintEngine> engineOpt = getOrCreateConnectedEngine(serverId, serverConfiguration, true);
if (!engineOpt.isPresent()) {
return;
}
engineOpt.get().updateProject(serverConfiguration, projectKey, null);
updatedProjectsByServer.computeIfAbsent(serverId, s -> new HashSet<>()).add(projectKey);
}
engineOpt.get().updateProject(serverConfiguration, requireNonNull(folderSettings.getProjectKey()), null);
analysisManager.analyzeAllOpenFilesInFolder(folder);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,75 @@ public void update_all_project_bindings_update_not_started_servers() {
verifyNoMoreInteractions(analysisManager);
}

@Test
public void update_all_project_bindings_update_once_each_project_same_server() {
WorkspaceFolderWrapper folder1 = mockFileInABoundWorkspaceFolder();
WorkspaceFolderWrapper folder2 = mockFileInABoundWorkspaceFolder2();
// Folder 2 is bound to the same server, different project
folder2.setSettings(BOUND_SETTINGS_DIFFERENT_PROJECT_KEY);

when(foldersManager.getAll()).thenReturn(asList(folder1, folder2));

when(enginesFactory.createConnectedEngine(anyString()))
.thenReturn(fakeEngine);

underTest.updateAllBindings();

verify(fakeEngine).update(any(), any());
verify(fakeEngine).updateProject(any(), eq(PROJECT_KEY), any());
verify(fakeEngine).updateProject(any(), eq(PROJECT_KEY2), any());

verify(analysisManager).analyzeAllOpenFilesInFolder(folder1);
verify(analysisManager).analyzeAllOpenFilesInFolder(folder2);
verifyNoMoreInteractions(analysisManager);
}

@Test
public void update_all_project_bindings_update_only_once_each_project_same_server() {
WorkspaceFolderWrapper folder1 = mockFileInABoundWorkspaceFolder();
WorkspaceFolderWrapper folder2 = mockFileInABoundWorkspaceFolder2();
// Folder 2 is bound to the same server, same project
folder2.setSettings(BOUND_SETTINGS);

when(foldersManager.getAll()).thenReturn(asList(folder1, folder2));

when(enginesFactory.createConnectedEngine(anyString()))
.thenReturn(fakeEngine);

underTest.updateAllBindings();

verify(fakeEngine).update(any(), any());
verify(fakeEngine).updateProject(any(), eq(PROJECT_KEY), any());

verify(analysisManager).analyzeAllOpenFilesInFolder(folder1);
verify(analysisManager).analyzeAllOpenFilesInFolder(folder2);
verifyNoMoreInteractions(analysisManager);
}

@Test
public void update_all_project_bindings_ignore_wrong_binding() {
WorkspaceFolderWrapper folder = mockFileInAFolder();
folder.setSettings(BOUND_SETTINGS);

when(foldersManager.getAll()).thenReturn(asList(folder));

underTest.updateAllBindings();

verifyNoMoreInteractions(analysisManager);
assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Invalid binding for '" + folder.getRootPath() + "'");
}

@Test
public void update_all_project_bindings_ignore_wrong_binding_default_folder() {
mockFileOutsideFolder();
when(settingsManager.getCurrentDefaultFolderSettings()).thenReturn(BOUND_SETTINGS);

underTest.updateAllBindings();

verifyNoMoreInteractions(analysisManager);
assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Invalid binding for 'default folder'");
}

private WorkspaceFolderWrapper mockFileInABoundWorkspaceFolder() {
WorkspaceFolderWrapper folder = mockFileInAFolder();
folder.setSettings(BOUND_SETTINGS);
Expand Down

0 comments on commit 3102096

Please sign in to comment.