Skip to content

Commit

Permalink
Upd
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Nov 20, 2024
1 parent 9a585d8 commit f3ff531
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 deletions.
86 changes: 77 additions & 9 deletions src/ConnectedMode.UnitTests/StatefulServerBranchProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ namespace SonarLint.VisualStudio.ConnectedMode.UnitTests
[TestClass]
public class StatefulServerBranchProviderTests
{
private readonly ActiveSolutionBindingEventArgs ConnectedModeBinding = new(new BindingConfiguration(default, SonarLintMode.Connected, default));
private readonly ActiveSolutionBindingEventArgs StandaloneModeBinding = new(BindingConfiguration.Standalone);

[TestMethod]
public void MefCtor_CheckIsExported()
{
Expand Down Expand Up @@ -75,10 +78,13 @@ public async Task GetServerBranchNameAsync_WhenCalled_UsesCache()
serverBranchProvider.VerifyGetServerBranchNameCalled(Times.Once);
}

[TestMethod]
public async Task GetServerBranchNameAsync_PreSolutionBindingChanged_CacheIsCleared()
[DataTestMethod]
[DataRow(true)]
[DataRow(false)]
public async Task GetServerBranchNameAsync_PreSolutionBindingChanged_CacheIsCleared(bool isConnected)
{
await TestEffectOfRaisingEventOnCache(asbt => asbt.PreSolutionBindingChanged += null,
eventArg: isConnected ? ConnectedModeBinding : StandaloneModeBinding,
shouldClearCache: true);
}

Expand All @@ -104,7 +110,8 @@ await TestEffectOfRaisingEventOnCache(asbt => asbt.SolutionBindingUpdated += nul
}

private static async Task TestEffectOfRaisingEventOnCache(Action<IActiveSolutionBoundTracker> eventAction,
bool shouldClearCache)
bool shouldClearCache,
object eventArg = null)
{
var serverBranchProvider = CreateServerBranchProvider("OriginalBranch");
var activeSolutionBoundTracker = new Mock<IActiveSolutionBoundTracker>();
Expand All @@ -120,7 +127,7 @@ private static async Task TestEffectOfRaisingEventOnCache(Action<IActiveSolution
serverBranchProvider.SetBranchNameToReturn("NewBranch");

// Raise event - should *not* trigger clearing the cache
activeSolutionBoundTracker.Raise(eventAction, null, null);
activeSolutionBoundTracker.Raise(eventAction, null, eventArg);

//second call: may or may not use the cache
serverBranch = await testSubject.GetServerBranchNameAsync(CancellationToken.None);
Expand All @@ -138,7 +145,50 @@ private static async Task TestEffectOfRaisingEventOnCache(Action<IActiveSolution
}

[TestMethod]
public void NotifySlCoreBranchChange_CallsDidVcsRepositoryChangeWithCorrectId()
public void NotifySlCoreBranchChange_BindingChanged_Connected_CallsDidVcsRepositoryChangeWithCorrectId()
{
// Arrange
const string expectedConfigScopeId = "expected-id";
var activeConfigScopeTracker = new Mock<IActiveConfigScopeTracker>();
activeConfigScopeTracker.Setup(x => x.Current).Returns(new ConfigurationScope(expectedConfigScopeId));

var sonarProjectBranchSlCoreService = new Mock<ISonarProjectBranchSlCoreService>();
var serviceProvider = new Mock<ISLCoreServiceProvider>();
var service = sonarProjectBranchSlCoreService.Object;
serviceProvider.Setup(x => x.TryGetTransientService(out service)).Returns(true);

var serverBranchProvider = CreateServerBranchProvider("OriginalBranch");
var activeSolutionBoundTracker = new Mock<IActiveSolutionBoundTracker>();
CreateTestSubject(serverBranchProvider.Object, activeSolutionBoundTracker.Object, activeConfigScopeTracker.Object, serviceProvider.Object);

// Act
activeSolutionBoundTracker.Raise(x => x.PreSolutionBindingChanged += null, null, ConnectedModeBinding);

// Assert
sonarProjectBranchSlCoreService.Verify(x =>
x.DidVcsRepositoryChange(It.Is<DidVcsRepositoryChangeParams>(p => p.configurationScopeId == expectedConfigScopeId)));
}

[TestMethod]
public void NotifySlCoreBranchChange_BindingChanged_Standalone_Ignores()
{
// Arrange
var activeConfigScopeTracker = new Mock<IActiveConfigScopeTracker>();
var serviceProvider = new Mock<ISLCoreServiceProvider>();
var serverBranchProvider = CreateServerBranchProvider("OriginalBranch");
var activeSolutionBoundTracker = new Mock<IActiveSolutionBoundTracker>();
CreateTestSubject(serverBranchProvider.Object, activeSolutionBoundTracker.Object, activeConfigScopeTracker.Object, serviceProvider.Object);

// Act
activeSolutionBoundTracker.Raise(x => x.PreSolutionBindingChanged += null, null, StandaloneModeBinding);

// Assert
activeConfigScopeTracker.VerifyNoOtherCalls();
serviceProvider.VerifyNoOtherCalls();
}

[TestMethod]
public void NotifySlCoreBranchChange_BindingUpdated_CallsDidVcsRepositoryChangeWithCorrectId()
{
// Arrange
const string expectedConfigScopeId = "expected-id";
Expand All @@ -155,16 +205,35 @@ public void NotifySlCoreBranchChange_CallsDidVcsRepositoryChangeWithCorrectId()
CreateTestSubject(serverBranchProvider.Object, activeSolutionBoundTracker.Object, activeConfigScopeTracker.Object, serviceProvider.Object);

// Act
activeSolutionBoundTracker.Raise(x => x.PreSolutionBindingChanged += null, null, null);
activeSolutionBoundTracker.Raise(x => x.PreSolutionBindingUpdated += null, null, null);

// Assert
sonarProjectBranchSlCoreService.Verify(x =>
x.DidVcsRepositoryChange(It.Is<DidVcsRepositoryChangeParams>(p => p.configurationScopeId == expectedConfigScopeId)), Times.Exactly(2));
x.DidVcsRepositoryChange(It.Is<DidVcsRepositoryChangeParams>(p => p.configurationScopeId == expectedConfigScopeId)));
}

[TestMethod]
public void NotifySlCoreBranchChange_BindingChanged_WhenServiceProviderReturnsFalse_LogsError()
{
// Arrange
var sonarProjectBranchSlCoreService = new Mock<ISonarProjectBranchSlCoreService>();
var serviceProvider = new Mock<ISLCoreServiceProvider>();
var service = sonarProjectBranchSlCoreService.Object;
serviceProvider.Setup(x => x.TryGetTransientService(out service)).Returns(false);

var serverBranchProvider = CreateServerBranchProvider("OriginalBranch");
var activeSolutionBoundTracker = new Mock<IActiveSolutionBoundTracker>();
var logger = new TestLogger();
CreateTestSubject(serverBranchProvider.Object, activeSolutionBoundTracker.Object, slCoreServiceProvider: serviceProvider.Object, logger: logger);

activeSolutionBoundTracker.Raise(x => x.PreSolutionBindingChanged += null, null, ConnectedModeBinding);

logger.AssertPartialOutputStringExists(SLCoreStrings.ServiceProviderNotInitialized);
sonarProjectBranchSlCoreService.VerifyNoOtherCalls();
}

[TestMethod]
public void NotifySlCoreBranchChange_WhenServiceProviderReturnsFalse_LogsError()
public void NotifySlCoreBranchChange_BindingUpdated_WhenServiceProviderReturnsFalse_LogsError()
{
// Arrange
var sonarProjectBranchSlCoreService = new Mock<ISonarProjectBranchSlCoreService>();
Expand All @@ -177,7 +246,6 @@ public void NotifySlCoreBranchChange_WhenServiceProviderReturnsFalse_LogsError()
var logger = new TestLogger();
CreateTestSubject(serverBranchProvider.Object, activeSolutionBoundTracker.Object, slCoreServiceProvider: serviceProvider.Object, logger: logger);

activeSolutionBoundTracker.Raise(x => x.PreSolutionBindingChanged += null, null, null);
activeSolutionBoundTracker.Raise(x => x.PreSolutionBindingUpdated += null, null, null);

logger.AssertPartialOutputStringExists(SLCoreStrings.ServiceProviderNotInitialized);
Expand Down
7 changes: 5 additions & 2 deletions src/ConnectedMode/StatefulServerBranchProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public StatefulServerBranchProvider(
this.logger = logger;
this.threadHandling = threadHandling;

activeSolutionBoundTracker.PreSolutionBindingChanged += OnPreSolutionBindingChanged;
activeSolutionBoundTracker.PreSolutionBindingUpdated += OnPreSolutionBindingUpdated;
activeSolutionBoundTracker.PreSolutionBindingChanged += OnPreSolutionBindingChanged;
}

private void OnPreSolutionBindingUpdated(object sender, EventArgs e)
Expand All @@ -75,7 +75,10 @@ private void OnPreSolutionBindingChanged(object sender, ActiveSolutionBindingEve
logger.LogVerbose(Resources.StatefulBranchProvider_BindingChanged);
selectedBranch = null;

NotifySlCoreBranchChange();
if(e.Configuration.Mode.IsInAConnectedMode())
{
NotifySlCoreBranchChange();
}
}

private void NotifySlCoreBranchChange() =>
Expand Down

0 comments on commit f3ff531

Please sign in to comment.