25
25
using Moq ;
26
26
using SonarLint . VisualStudio . Core ;
27
27
using SonarLint . VisualStudio . Core . Binding ;
28
+ using SonarLint . VisualStudio . Infrastructure . VS . Roslyn ;
28
29
using SonarLint . VisualStudio . TestInfrastructure ;
29
30
using SonarQube . Client ;
30
31
using SonarQube . Client . Models ;
@@ -188,11 +189,73 @@ public void ActiveSolutionBoundTracker_UnBoundProject_NullPassedToConfigScopeUpd
188
189
configScopeUpdaterMock . Verify ( x => x . UpdateConfigScopeForCurrentSolution ( null ) ) ;
189
190
configScopeUpdaterMock . VerifyNoOtherCalls ( ) ;
190
191
}
192
+
193
+ [ TestMethod ]
194
+ public void Ctor_NoSolution_CallsAnalyzerManager ( )
195
+ {
196
+ var configScopeUpdaterMock = new Mock < IConfigScopeUpdater > ( ) ;
197
+ var analyzerManager = new Mock < ISolutionRoslynAnalyzerManager > ( ) ;
198
+
199
+ activeSolutionTracker . CurrentSolutionName = null ;
200
+ ConfigureService ( isConnected : false ) ;
201
+
202
+ var testSubject = CreateTestSubject (
203
+ activeSolutionTracker ,
204
+ configProvider ,
205
+ loggerMock . Object ,
206
+ configScopeUpdater : configScopeUpdaterMock . Object ,
207
+ sonarQubeService : sonarQubeServiceMock . Object ,
208
+ solutionRoslynAnalyzerManager : analyzerManager . Object ) ;
209
+
210
+ analyzerManager . Verify ( x => x . OnSolutionChanged ( null , BindingConfiguration . Standalone ) ) ;
211
+ }
212
+
213
+ [ TestMethod ]
214
+ public void Ctor_StandaloneSolution_CallsAnalyzerManager ( )
215
+ {
216
+ var configScopeUpdaterMock = new Mock < IConfigScopeUpdater > ( ) ;
217
+ var analyzerManager = new Mock < ISolutionRoslynAnalyzerManager > ( ) ;
218
+
219
+ activeSolutionTracker . CurrentSolutionName = "solution123" ;
220
+ ConfigureService ( isConnected : false ) ;
221
+
222
+ var testSubject = CreateTestSubject (
223
+ activeSolutionTracker ,
224
+ configProvider ,
225
+ loggerMock . Object ,
226
+ configScopeUpdater : configScopeUpdaterMock . Object ,
227
+ sonarQubeService : sonarQubeServiceMock . Object ,
228
+ solutionRoslynAnalyzerManager : analyzerManager . Object ) ;
229
+
230
+ analyzerManager . Verify ( x => x . OnSolutionChanged ( "solution123" , BindingConfiguration . Standalone ) ) ;
231
+ }
232
+
233
+ [ TestMethod ]
234
+ public void Ctor_BoundSolution_CallsAnalyzerManager ( )
235
+ {
236
+ var configScopeUpdaterMock = new Mock < IConfigScopeUpdater > ( ) ;
237
+ var analyzerManager = new Mock < ISolutionRoslynAnalyzerManager > ( ) ;
238
+
239
+ activeSolutionTracker . CurrentSolutionName = "solution123" ;
240
+ ConfigureService ( isConnected : false ) ;
241
+ ConfigureSolutionBinding ( boundSonarQubeProject ) ;
242
+
243
+ var testSubject = CreateTestSubject (
244
+ activeSolutionTracker ,
245
+ configProvider ,
246
+ loggerMock . Object ,
247
+ configScopeUpdater : configScopeUpdaterMock . Object ,
248
+ sonarQubeService : sonarQubeServiceMock . Object ,
249
+ solutionRoslynAnalyzerManager : analyzerManager . Object ) ;
250
+
251
+ analyzerManager . Verify ( x => x . OnSolutionChanged ( "solution123" , It . Is < BindingConfiguration > ( y => y . Mode == SonarLintMode . Connected && y . Project == boundSonarQubeProject ) ) ) ;
252
+ }
191
253
192
254
[ TestMethod ]
193
255
public void ActiveSolutionBoundTracker_Changes ( )
194
256
{
195
257
var configScopeUpdaterMock = new Mock < IConfigScopeUpdater > ( ) ;
258
+ var analyzerManager = new Mock < ISolutionRoslynAnalyzerManager > ( ) ;
196
259
197
260
ConfigureService ( isConnected : false ) ;
198
261
ConfigureSolutionBinding ( boundSonarQubeProject ) ;
@@ -202,7 +265,8 @@ public void ActiveSolutionBoundTracker_Changes()
202
265
configProvider ,
203
266
loggerMock . Object ,
204
267
configScopeUpdater : configScopeUpdaterMock . Object ,
205
- sonarQubeService : sonarQubeServiceMock . Object ) ;
268
+ sonarQubeService : sonarQubeServiceMock . Object ,
269
+ solutionRoslynAnalyzerManager : analyzerManager . Object ) ;
206
270
var eventCounter = new EventCounter ( testSubject ) ;
207
271
208
272
// Sanity
@@ -215,6 +279,7 @@ public void ActiveSolutionBoundTracker_Changes()
215
279
216
280
// Case 1: Clear bound project
217
281
ConfigureSolutionBinding ( null ) ;
282
+ activeSolutionTracker . CurrentSolutionName = "solution1" ;
218
283
219
284
// Act
220
285
testSubject . HandleBindingChange ( true ) ;
@@ -225,7 +290,8 @@ public void ActiveSolutionBoundTracker_Changes()
225
290
eventCounter . SolutionBindingChangedCount . Should ( ) . Be ( 1 , "Unbind should trigger reanalysis" ) ;
226
291
eventCounter . PreSolutionBindingUpdatedCount . Should ( ) . Be ( 0 , "Unbind should not trigger change" ) ;
227
292
eventCounter . SolutionBindingUpdatedCount . Should ( ) . Be ( 0 , "Unbind should not trigger change" ) ;
228
- configScopeUpdaterMock . Verify ( x => x . UpdateConfigScopeForCurrentSolution ( It . IsAny < BoundServerProject > ( ) ) , Times . Exactly ( 1 ) ) ;
293
+ configScopeUpdaterMock . Verify ( x => x . UpdateConfigScopeForCurrentSolution ( null ) , Times . Exactly ( 1 ) ) ;
294
+ VerifyAnalyzerUpdatedAndClearMock ( analyzerManager , "solution1" , false ) ;
229
295
VerifyAndResetBoundSolutionUiContextMock ( isActive : false ) ;
230
296
231
297
VerifyServiceDisconnect ( Times . Never ( ) ) ;
@@ -243,6 +309,7 @@ public void ActiveSolutionBoundTracker_Changes()
243
309
eventCounter . PreSolutionBindingUpdatedCount . Should ( ) . Be ( 0 , "Bind should not trigger update event" ) ;
244
310
eventCounter . SolutionBindingUpdatedCount . Should ( ) . Be ( 0 , "Bind should not trigger update event" ) ;
245
311
configScopeUpdaterMock . Verify ( x => x . UpdateConfigScopeForCurrentSolution ( It . IsAny < BoundServerProject > ( ) ) , Times . Exactly ( 2 ) ) ;
312
+ VerifyAnalyzerUpdatedAndClearMock ( analyzerManager , "solution1" , true ) ;
246
313
VerifyAndResetBoundSolutionUiContextMock ( isActive : true ) ;
247
314
248
315
// Notifications from the Team Explorer should not trigger connect/disconnect
@@ -261,6 +328,7 @@ public void ActiveSolutionBoundTracker_Changes()
261
328
eventCounter . PreSolutionBindingUpdatedCount . Should ( ) . Be ( 0 , "Solution change should not trigger update event" ) ;
262
329
eventCounter . SolutionBindingUpdatedCount . Should ( ) . Be ( 0 , "Solution change should not trigger update event" ) ;
263
330
configScopeUpdaterMock . Verify ( x => x . UpdateConfigScopeForCurrentSolution ( It . IsAny < BoundServerProject > ( ) ) , Times . Exactly ( 3 ) ) ;
331
+ VerifyAnalyzerUpdatedAndClearMock ( analyzerManager , null , false ) ;
264
332
VerifyAndResetBoundSolutionUiContextMock ( isActive : false ) ;
265
333
266
334
// Closing an unbound solution should not call disconnect/connect
@@ -270,7 +338,7 @@ public void ActiveSolutionBoundTracker_Changes()
270
338
// Case 4: Load a bound solution
271
339
ConfigureSolutionBinding ( boundSonarQubeProject ) ;
272
340
// Act
273
- activeSolutionTracker . SimulateActiveSolutionChanged ( isSolutionOpen : true , "solution " ) ;
341
+ activeSolutionTracker . SimulateActiveSolutionChanged ( isSolutionOpen : true , "solution2 " ) ;
274
342
275
343
// Assert
276
344
testSubject . CurrentConfiguration . Mode . Should ( ) . Be ( SonarLintMode . Connected , "Bound respond to solution change event and report bound" ) ;
@@ -279,6 +347,7 @@ public void ActiveSolutionBoundTracker_Changes()
279
347
eventCounter . PreSolutionBindingUpdatedCount . Should ( ) . Be ( 0 , "Bind should not trigger update event" ) ;
280
348
eventCounter . SolutionBindingUpdatedCount . Should ( ) . Be ( 0 , "Bind should not trigger update event" ) ;
281
349
configScopeUpdaterMock . Verify ( x => x . UpdateConfigScopeForCurrentSolution ( It . IsAny < BoundServerProject > ( ) ) , Times . Exactly ( 4 ) ) ;
350
+ VerifyAnalyzerUpdatedAndClearMock ( analyzerManager , "solution2" , true ) ;
282
351
VerifyAndResetBoundSolutionUiContextMock ( isActive : true ) ;
283
352
284
353
// Loading a bound solution should call connect
@@ -296,6 +365,7 @@ public void ActiveSolutionBoundTracker_Changes()
296
365
eventCounter . PreSolutionBindingUpdatedCount . Should ( ) . Be ( 0 , "Solution change should not trigger update event" ) ;
297
366
eventCounter . SolutionBindingUpdatedCount . Should ( ) . Be ( 0 , "Solution change should not trigger update event" ) ;
298
367
configScopeUpdaterMock . Verify ( x => x . UpdateConfigScopeForCurrentSolution ( It . IsAny < BoundServerProject > ( ) ) , Times . Exactly ( 5 ) ) ;
368
+ VerifyAnalyzerUpdatedAndClearMock ( analyzerManager , null , false ) ;
299
369
VerifyAndResetBoundSolutionUiContextMock ( isActive : false ) ;
300
370
301
371
// SonarQubeService.Disconnect should be called since the WPF DisconnectCommand is not available
@@ -312,11 +382,27 @@ public void ActiveSolutionBoundTracker_Changes()
312
382
eventCounter . PreSolutionBindingChangedCount . Should ( ) . Be ( 5 , "Once disposed should stop raising the event" ) ;
313
383
eventCounter . SolutionBindingChangedCount . Should ( ) . Be ( 5 , "Once disposed should stop raising the event" ) ;
314
384
configScopeUpdaterMock . Verify ( x => x . UpdateConfigScopeForCurrentSolution ( It . IsAny < BoundServerProject > ( ) ) , Times . Exactly ( 5 ) ) ;
385
+ analyzerManager . Invocations . Should ( ) . BeEmpty ( ) ;
315
386
// SonarQubeService.Disconnect should be called since the WPF DisconnectCommand is not available
316
387
VerifyServiceDisconnect ( Times . Once ( ) ) ;
317
388
VerifyServiceConnect ( Times . Once ( ) ) ;
318
389
}
319
390
391
+ private void VerifyAnalyzerUpdatedAndClearMock ( Mock < ISolutionRoslynAnalyzerManager > analyzerManager , string solutionName , bool bound )
392
+ {
393
+ if ( bound )
394
+ {
395
+ analyzerManager . Verify ( x => x . OnSolutionChanged ( solutionName ,
396
+ It . Is < BindingConfiguration > ( y => y . Mode == SonarLintMode . Connected && y . Project == boundSonarQubeProject ) ) ,
397
+ Times . Exactly ( 1 ) ) ;
398
+ }
399
+ else
400
+ {
401
+ analyzerManager . Verify ( x => x . OnSolutionChanged ( solutionName , BindingConfiguration . Standalone ) , Times . Exactly ( 1 ) ) ;
402
+ }
403
+ analyzerManager . Invocations . Clear ( ) ;
404
+ }
405
+
320
406
[ TestMethod ]
321
407
public void UpdateConnection_WasDisconnected_NewSolutionIsUnbound_NoConnectOrDisconnectCalls ( )
322
408
{
@@ -517,13 +603,15 @@ private ActiveSolutionBoundTracker CreateTestSubject(
517
603
ILogger logger = null ,
518
604
IBoundSolutionGitMonitor gitEvents = null ,
519
605
IConfigScopeUpdater configScopeUpdater = null ,
520
- ISonarQubeService sonarQubeService = null )
606
+ ISonarQubeService sonarQubeService = null ,
607
+ ISolutionRoslynAnalyzerManager solutionRoslynAnalyzerManager = null )
521
608
{
522
609
configScopeUpdater ??= Mock . Of < IConfigScopeUpdater > ( ) ;
523
610
logger ??= new TestLogger ( logToConsole : true ) ;
524
611
gitEvents ??= Mock . Of < IBoundSolutionGitMonitor > ( ) ;
525
612
sonarQubeService ??= Mock . Of < ISonarQubeService > ( ) ;
526
- return new ActiveSolutionBoundTracker ( serviceProvider , solutionTracker , configScopeUpdater , logger , gitEvents , configurationProvider , sonarQubeService ) ;
613
+ solutionRoslynAnalyzerManager ??= Mock . Of < ISolutionRoslynAnalyzerManager > ( ) ;
614
+ return new ActiveSolutionBoundTracker ( serviceProvider , solutionTracker , configScopeUpdater , solutionRoslynAnalyzerManager , logger , gitEvents , configurationProvider , sonarQubeService ) ;
527
615
}
528
616
529
617
private void ConfigureService ( bool isConnected )
0 commit comments