Skip to content

Commit dbd6b41

Browse files
Raise CurrentConfigurationScopeChanged when configuration scope changed.
Part of SLVS-1497
1 parent 6e0dfcb commit dbd6b41

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/SLCore.UnitTests/State/ActiveConfigScopeTrackerTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
2020

21+
using System.ComponentModel;
2122
using SonarLint.VisualStudio.Core;
2223
using SonarLint.VisualStudio.Core.Synchronization;
2324
using SonarLint.VisualStudio.SLCore.Core;
@@ -38,6 +39,7 @@ public class ActiveConfigScopeTrackerTests
3839
private ISLCoreServiceProvider serviceProvider;
3940
private IAsyncLockFactory asyncLockFactory;
4041
private IThreadHandling threadHandling;
42+
private EventHandler currentConfigScopeChangedEventHandler;
4143

4244
[TestInitialize]
4345
public void TestInitialize()
@@ -50,8 +52,10 @@ public void TestInitialize()
5052
threadHandling = Substitute.For<IThreadHandling>();
5153
ConfigureServiceProvider(isServiceAvailable:true);
5254
ConfigureAsyncLockFactory();
55+
currentConfigScopeChangedEventHandler = Substitute.For<EventHandler>();
5356

5457
testSubject = new ActiveConfigScopeTracker(serviceProvider, asyncLockFactory, threadHandling);
58+
testSubject.CurrentConfigurationScopeChanged += currentConfigScopeChangedEventHandler;
5559
}
5660

5761
[TestMethod]
@@ -80,6 +84,7 @@ public void SetCurrentConfigScope_SetsUnboundScope()
8084
VerifyThreadHandling();
8185
VerifyServiceAddCall();
8286
VerifyLockTakenSynchronouslyAndReleased();
87+
VerifyCurrentConfigurationScopeChangedRaised();
8388
}
8489

8590
[TestMethod]
@@ -95,6 +100,7 @@ public void TryUpdateRootOnCurrentConfigScope_ConfigScopeSame_Updates()
95100

96101
result.Should().BeTrue();
97102
testSubject.currentConfigScope.Should().BeEquivalentTo(new ConfigurationScope(configScopeId, connectionId, sonarProjectKey, "some root", isReady));
103+
VerifyCurrentConfigurationScopeChangedRaised();
98104
}
99105

100106
[TestMethod]
@@ -110,6 +116,7 @@ public void TryUpdateRootOnCurrentConfigScope_ConfigScopeDifferent_DoesNotUpdate
110116

111117
result.Should().BeFalse();
112118
testSubject.currentConfigScope.Should().BeEquivalentTo(new ConfigurationScope(configScopeId, connectionId, sonarProjectKey, isReadyForAnalysis: isReady));
119+
VerifyCurrentConfigurationScopeChangedNotRaised();
113120
}
114121

115122
[TestMethod]
@@ -125,6 +132,7 @@ public void TryUpdateAnalysisReadinessOnCurrentConfigScope_ConfigScopeSame_Updat
125132

126133
result.Should().BeTrue();
127134
testSubject.currentConfigScope.Should().BeEquivalentTo(new ConfigurationScope(configScopeId, connectionId, sonarProjectKey, root, true));
135+
VerifyCurrentConfigurationScopeChangedRaised();
128136
}
129137

130138
[TestMethod]
@@ -140,6 +148,7 @@ public void TryUpdateAnalysisReadinessOnCurrentConfigScope_ConfigScopeDifferent_
140148

141149
result.Should().BeFalse();
142150
testSubject.currentConfigScope.Should().BeEquivalentTo(new ConfigurationScope(configScopeId, connectionId, sonarProjectKey, root));
151+
VerifyCurrentConfigurationScopeChangedNotRaised();
143152
}
144153

145154
[TestMethod]
@@ -155,6 +164,7 @@ public void SetCurrentConfigScope_SetsBoundScope()
155164
VerifyThreadHandling();
156165
VerifyServiceAddCall();
157166
VerifyLockTakenSynchronouslyAndReleased();
167+
VerifyCurrentConfigurationScopeChangedRaised();
158168
}
159169

160170
[TestMethod]
@@ -174,6 +184,7 @@ public void SetCurrentConfigScope_CurrentScopeExists_UpdatesBoundScope()
174184
VerifyThreadHandling();
175185
VerifyServiceUpdateCall();
176186
VerifyLockTakenSynchronouslyAndReleased();
187+
VerifyCurrentConfigurationScopeChangedRaised();
177188
}
178189

179190
[TestMethod]
@@ -185,6 +196,7 @@ public void SetCurrentConfigScope_ServiceUnavailable_Throws()
185196

186197
act.Should().ThrowExactly<InvalidOperationException>().WithMessage(SLCoreStrings.ServiceProviderNotInitialized);
187198
VerifyThreadHandling();
199+
VerifyCurrentConfigurationScopeChangedNotRaised();
188200
}
189201

190202
[TestMethod]
@@ -199,6 +211,7 @@ public void SetCurrentConfigScope_UpdateConfigScopeWithDifferentId_Throws()
199211

200212
act.Should().ThrowExactly<InvalidOperationException>().WithMessage(SLCoreStrings.ConfigScopeConflict);
201213
VerifyThreadHandling();
214+
VerifyCurrentConfigurationScopeChangedNotRaised();
202215
}
203216

204217
[TestMethod]
@@ -212,6 +225,7 @@ public void RemoveCurrentConfigScope_RemovesScope()
212225
configScopeService.Received().DidRemoveConfigurationScope(Arg.Is<DidRemoveConfigurationScopeParams>(p => p.removedId == configScopeId));
213226
VerifyThreadHandling();
214227
VerifyLockTakenSynchronouslyAndReleased();
228+
VerifyCurrentConfigurationScopeChangedRaised();
215229
}
216230

217231
[TestMethod]
@@ -222,6 +236,7 @@ public void RemoveCurrentConfigScope_NoCurrentScope_DoesNothing()
222236
configScopeService.ReceivedCalls().Count().Should().Be(0);
223237
VerifyThreadHandling();
224238
VerifyLockTakenSynchronouslyAndReleased();
239+
VerifyCurrentConfigurationScopeChangedNotRaised();
225240
}
226241

227242
[TestMethod]
@@ -234,6 +249,7 @@ public void RemoveCurrentConfigScope_ServiceUnavailable_Throws()
234249

235250
act.Should().ThrowExactly<InvalidOperationException>().WithMessage(SLCoreStrings.ServiceProviderNotInitialized);
236251
VerifyThreadHandling();
252+
VerifyCurrentConfigurationScopeChangedNotRaised();
237253
}
238254

239255
[TestMethod]
@@ -279,6 +295,7 @@ public void Reset_SetsCurrentScopeToNull()
279295
serviceProvider.ReceivedCalls().Count().Should().Be(0);
280296
VerifyThreadHandling();
281297
VerifyLockTakenSynchronouslyAndReleased();
298+
VerifyCurrentConfigurationScopeChangedRaised();
282299
}
283300

284301
[TestMethod]
@@ -335,6 +352,16 @@ private void ConfigureServiceProvider(bool isServiceAvailable)
335352
});
336353
}
337354

355+
private void VerifyCurrentConfigurationScopeChangedRaised()
356+
{
357+
currentConfigScopeChangedEventHandler.Received(1).Invoke(testSubject, Arg.Any<EventArgs>());
358+
}
359+
360+
private void VerifyCurrentConfigurationScopeChangedNotRaised()
361+
{
362+
currentConfigScopeChangedEventHandler.DidNotReceive().Invoke(testSubject, Arg.Any<EventArgs>());
363+
}
364+
338365
private class ConfigurationScopeDtoComparer : IEqualityComparer<ConfigurationScopeDto>
339366
{
340367
public bool Equals(ConfigurationScopeDto x, ConfigurationScopeDto y)

src/SLCore/State/ActiveConfigScopeTracker.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public interface IActiveConfigScopeTracker : IDisposable
4343
bool TryUpdateRootOnCurrentConfigScope(string id, string root);
4444

4545
bool TryUpdateAnalysisReadinessOnCurrentConfigScope(string id, bool isReady);
46+
47+
event EventHandler CurrentConfigurationScopeChanged;
4648
}
4749

4850
public record ConfigurationScope(
@@ -107,12 +109,14 @@ public void SetCurrentConfigScope(string id, string connectionId = null, string
107109
{
108110
configurationScopeService.DidUpdateBinding(new DidUpdateBindingParams(id, GetBinding(connectionId, sonarProjectKey)));
109111
currentConfigScope = currentConfigScope with { ConnectionId = connectionId, SonarProjectId = sonarProjectKey };
112+
OnCurrentConfigurationScopeChanged();
110113
return;
111114
}
112115

113116
configurationScopeService.DidAddConfigurationScopes(new DidAddConfigurationScopesParams([
114117
new ConfigurationScopeDto(id, id, true, GetBinding(connectionId, sonarProjectKey))]));
115118
currentConfigScope = new ConfigurationScope(id, connectionId, sonarProjectKey);
119+
OnCurrentConfigurationScopeChanged();
116120
}
117121
}
118122

@@ -123,6 +127,7 @@ public void Reset()
123127
using (asyncLock.Acquire())
124128
{
125129
currentConfigScope = null;
130+
OnCurrentConfigurationScopeChanged();
126131
}
127132
}
128133

@@ -145,6 +150,7 @@ public void RemoveCurrentConfigScope()
145150
configurationScopeService.DidRemoveConfigurationScope(
146151
new DidRemoveConfigurationScopeParams(currentConfigScope.Id));
147152
currentConfigScope = null;
153+
OnCurrentConfigurationScopeChanged();
148154
}
149155
}
150156

@@ -158,6 +164,7 @@ public bool TryUpdateRootOnCurrentConfigScope(string id, string root)
158164
}
159165

160166
currentConfigScope = currentConfigScope with { RootPath = root };
167+
OnCurrentConfigurationScopeChanged();
161168
return true;
162169
}
163170
}
@@ -172,10 +179,13 @@ public bool TryUpdateAnalysisReadinessOnCurrentConfigScope(string id, bool isRea
172179
}
173180

174181
currentConfigScope = currentConfigScope with { isReadyForAnalysis = isReady};
182+
OnCurrentConfigurationScopeChanged();
175183
return true;
176184
}
177185
}
178186

187+
public event EventHandler CurrentConfigurationScopeChanged;
188+
179189
public void Dispose()
180190
{
181191
asyncLock?.Dispose();
@@ -184,4 +194,9 @@ public void Dispose()
184194
private BindingConfigurationDto GetBinding(string connectionId, string sonarProjectKey) => connectionId is not null
185195
? new BindingConfigurationDto(connectionId, sonarProjectKey)
186196
: null;
197+
198+
private void OnCurrentConfigurationScopeChanged()
199+
{
200+
CurrentConfigurationScopeChanged?.Invoke(this, EventArgs.Empty);
201+
}
187202
}

0 commit comments

Comments
 (0)