diff --git a/src/ConnectedMode.UnitTests/ServerSentEvents/SSESessionManagerTests.cs b/src/ConnectedMode.UnitTests/ServerSentEvents/SSESessionManagerTests.cs index f37065b1b3..dbda0b0cbd 100644 --- a/src/ConnectedMode.UnitTests/ServerSentEvents/SSESessionManagerTests.cs +++ b/src/ConnectedMode.UnitTests/ServerSentEvents/SSESessionManagerTests.cs @@ -18,8 +18,6 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -using System; -using System.Threading.Tasks; using SonarLint.VisualStudio.ConnectedMode.ServerSentEvents; using SonarLint.VisualStudio.Core; using SonarLint.VisualStudio.Core.Binding; @@ -89,10 +87,25 @@ public void CreateSessionIfInConnectedMode_WhenInStandaloneModeOnCreation_DoesNo testScope.SSESessionFactoryMock.Verify(factory => factory.Create(DefaultProjectKey, It.IsAny()), Times.Never); } + [TestMethod] + public void CreateSessionIfInConnectedMode_WhenConnectedToSonarCloud_DoesNotCreateSession() + { + var bindingConfig = TestScope.CreateConnectedModeSonarCloudBindingConfiguration(DefaultProjectKey); + + var testScope = new TestScope(bindingConfig); + + var _ = testScope.CreateTestSubject(); + var testSubject = testScope.CreateTestSubject(); + + testSubject.CreateSessionIfInConnectedMode(bindingConfig); + + testScope.SSESessionFactoryMock.Verify(factory => factory.Create(DefaultProjectKey, It.IsAny()), Times.Never); + } + [TestMethod] public void CreateSessionIfInConnectedMode_WhenInConnectedModeOnCreation_CreatesSession() { - var bindingConfig = TestScope.CreateConnectedModeBindingConfiguration(DefaultProjectKey); + var bindingConfig = TestScope.CreateConnectedModeSonarQubeBindingConfiguration(DefaultProjectKey); var testScope = new TestScope(bindingConfig); var sessionMock = testScope.SetUpSSEFactoryToReturnNoOpSSESession(DefaultProjectKey); @@ -158,7 +171,7 @@ public void OnSolutionChanged_WhenChangesFromConnectedToConnected_CancelsSession [DataTestMethod] public async Task OnSessionFailed_CancelsSessionAndStartsNewOne() { - var bindingConfig = TestScope.CreateConnectedModeBindingConfiguration(DefaultProjectKey); + var bindingConfig = TestScope.CreateConnectedModeSonarQubeBindingConfiguration(DefaultProjectKey); var testScope = new TestScope(bindingConfig); @@ -256,11 +269,23 @@ public SSESessionManager CreateTestSubject() Mock.Of()); } - public static BindingConfiguration CreateConnectedModeBindingConfiguration(string projectKey) + public static BindingConfiguration CreateConnectedModeSonarQubeBindingConfiguration(string projectKey) + { + var sonarQube = new ServerConnection.SonarQube(new Uri("http://localhost")); + return CreateConnectedModeBindingConfiguration(projectKey, sonarQube); + } + + public static BindingConfiguration CreateConnectedModeSonarCloudBindingConfiguration(string projectKey) + { + var sonarCloud = new ServerConnection.SonarCloud(projectKey); + return CreateConnectedModeBindingConfiguration(projectKey, sonarCloud); + } + + private static BindingConfiguration CreateConnectedModeBindingConfiguration(string projectKey, ServerConnection serverConnection) { var randomString = Guid.NewGuid().ToString(); var bindingConfiguration = new BindingConfiguration( - new BoundServerProject(randomString, projectKey, new ServerConnection.SonarQube(new Uri("http://localhost"))), + new BoundServerProject(randomString, projectKey, serverConnection), SonarLintMode.Connected, randomString); return bindingConfiguration; @@ -298,7 +323,7 @@ public Mock SetUpSSEFactoryToReturnNoOpSSESession(string projectKey public void RaiseInConnectedModeEvent(string projectKey) { - var openProjectEvent = CreateConnectedModeBindingConfiguration(projectKey); + var openProjectEvent = CreateConnectedModeSonarQubeBindingConfiguration(projectKey); RaiseSolutionBindingEvent(openProjectEvent); } diff --git a/src/ConnectedMode/ServerSentEvents/SSESessionManager.cs b/src/ConnectedMode/ServerSentEvents/SSESessionManager.cs index 2d0c8bc020..0b08d1cfca 100644 --- a/src/ConnectedMode/ServerSentEvents/SSESessionManager.cs +++ b/src/ConnectedMode/ServerSentEvents/SSESessionManager.cs @@ -18,9 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -using System; using System.ComponentModel.Composition; -using System.Threading.Tasks; using Microsoft.VisualStudio.Threading; using SonarLint.VisualStudio.Core; using SonarLint.VisualStudio.Core.Binding; @@ -43,7 +41,7 @@ internal sealed class SSESessionManager : IDisposable private bool disposed; [ImportingConstructor] - public SSESessionManager(IActiveSolutionBoundTracker activeSolutionBoundTracker, + public SSESessionManager(IActiveSolutionBoundTracker activeSolutionBoundTracker, ISSESessionFactory sseSessionFactory, ILogger logger) { @@ -83,7 +81,7 @@ public void CreateSessionIfInConnectedMode(BindingConfiguration bindingConfigura lock (syncRoot) { EndCurrentSession(); - + var isInConnectedMode = !bindingConfiguration.Equals(BindingConfiguration.Standalone); if (!isInConnectedMode) @@ -92,6 +90,12 @@ public void CreateSessionIfInConnectedMode(BindingConfiguration bindingConfigura return; } + if (bindingConfiguration.Project.ServerConnection is ServerConnection.SonarCloud) + { + logger.LogVerbose("[SSESessionManager] Not available for the current server connection"); + return; + } + logger.LogVerbose("[SSESessionManager] In connected mode, creating session..."); currentSession = sseSessionFactory.Create(bindingConfiguration.Project.ServerProjectKey, OnSessionFailedAsync);