Skip to content

Commit

Permalink
BackgroundAnalysisScope enhancements (Part 1)
Browse files Browse the repository at this point in the history
Addresses first two parts of dotnet#57146
  • Loading branch information
mavasani committed Oct 15, 2021
1 parent 241ed70 commit f6d80fd
Show file tree
Hide file tree
Showing 46 changed files with 439 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ internal async Task TestAdditionalFileAnalyzer(bool registerFromInitialize, bool

switch (analysisScope)
{
case BackgroundAnalysisScope.None:
case BackgroundAnalysisScope.ActiveFile:
case BackgroundAnalysisScope.OpenFilesAndProjects:
workspace.OpenAdditionalDocument(firstAdditionalDocument.Id);
Expand All @@ -670,7 +671,7 @@ internal async Task TestAdditionalFileAnalyzer(bool registerFromInitialize, bool

var expectedCount = (analysisScope, testMultiple) switch
{
(BackgroundAnalysisScope.ActiveFile, _) => 0,
(BackgroundAnalysisScope.ActiveFile or BackgroundAnalysisScope.None, _) => 0,
(BackgroundAnalysisScope.OpenFilesAndProjects or BackgroundAnalysisScope.FullSolution, false) => 1,
(BackgroundAnalysisScope.OpenFilesAndProjects, true) => 2,
(BackgroundAnalysisScope.FullSolution, true) => 4,
Expand All @@ -687,7 +688,7 @@ internal async Task TestAdditionalFileAnalyzer(bool registerFromInitialize, bool
var applicableDiagnostics = diagnostics.Where(
d => d.Id == analyzer.Descriptor.Id && d.DataLocation.OriginalFilePath == additionalDoc.FilePath);

if (analysisScope == BackgroundAnalysisScope.ActiveFile)
if (analysisScope is BackgroundAnalysisScope.ActiveFile or BackgroundAnalysisScope.None)
{
Assert.Empty(applicableDiagnostics);
}
Expand Down Expand Up @@ -761,6 +762,7 @@ internal async Task TestDiagnosticSuppressor(bool includeAnalyzer, bool includeS

switch (analysisScope)
{
case BackgroundAnalysisScope.None:
case BackgroundAnalysisScope.ActiveFile:
workspace.OpenDocument(document.Id);
var documentTrackingService = (TestDocumentTrackingService)workspace.Services.GetService<IDocumentTrackingService>();
Expand All @@ -776,11 +778,14 @@ internal async Task TestDiagnosticSuppressor(bool includeAnalyzer, bool includeS
case BackgroundAnalysisScope.FullSolution:
await incrementalAnalyzer.AnalyzeProjectAsync(project, semanticsChanged: true, InvocationReasons.Reanalyze, CancellationToken.None);
break;

default:
throw ExceptionUtilities.UnexpectedValue(analysisScope);
}

await ((AsynchronousOperationListener)service.Listener).ExpeditedWaitAsync();

if (includeAnalyzer)
if (includeAnalyzer && analysisScope != BackgroundAnalysisScope.None)
{
Assert.True(diagnostic != null);
Assert.Equal(NamedTypeAnalyzer.DiagnosticId, diagnostic.Id);
Expand Down Expand Up @@ -863,6 +868,7 @@ void M()

switch (analysisScope)
{
case BackgroundAnalysisScope.None:
case BackgroundAnalysisScope.ActiveFile:
workspace.OpenDocument(document.Id);
var documentTrackingService = (TestDocumentTrackingService)workspace.Services.GetRequiredService<IDocumentTrackingService>();
Expand All @@ -882,21 +888,29 @@ void M()

await ((AsynchronousOperationListener)service.Listener).ExpeditedWaitAsync();

Assert.Equal(2, diagnostics.Count);
var root = await document.GetSyntaxRootAsync();
if (testPragma)
if (analysisScope == BackgroundAnalysisScope.None)
{
var pragma1 = root.FindTrivia(diagnostics[0].GetTextSpan().Start).ToString();
Assert.Equal($"#pragma warning disable {NamedTypeAnalyzer.DiagnosticId} // Unnecessary", pragma1);
var pragma2 = root.FindTrivia(diagnostics[1].GetTextSpan().Start).ToString();
Assert.Equal($"#pragma warning disable CS0168 // Variable is declared but never used - Unnecessary", pragma2);
// Anayzers are disabled for BackgroundAnalysisScope.None.
Assert.Empty(diagnostics);
}
else
{
var attribute1 = root.FindNode(diagnostics[0].GetTextSpan()).ToString();
Assert.Equal($@"System.Diagnostics.CodeAnalysis.SuppressMessage(""Category2"", ""{NamedTypeAnalyzer.DiagnosticId}"")", attribute1);
var attribute2 = root.FindNode(diagnostics[1].GetTextSpan()).ToString();
Assert.Equal($@"System.Diagnostics.CodeAnalysis.SuppressMessage(""Category3"", ""CS0168"")", attribute2);
Assert.Equal(2, diagnostics.Count);
if (testPragma)
{
var pragma1 = root.FindTrivia(diagnostics[0].GetTextSpan().Start).ToString();
Assert.Equal($"#pragma warning disable {NamedTypeAnalyzer.DiagnosticId} // Unnecessary", pragma1);
var pragma2 = root.FindTrivia(diagnostics[1].GetTextSpan().Start).ToString();
Assert.Equal($"#pragma warning disable CS0168 // Variable is declared but never used - Unnecessary", pragma2);
}
else
{
var attribute1 = root.FindNode(diagnostics[0].GetTextSpan()).ToString();
Assert.Equal($@"System.Diagnostics.CodeAnalysis.SuppressMessage(""Category2"", ""{NamedTypeAnalyzer.DiagnosticId}"")", attribute1);
var attribute2 = root.FindNode(diagnostics[1].GetTextSpan()).ToString();
Assert.Equal($@"System.Diagnostics.CodeAnalysis.SuppressMessage(""Category3"", ""CS0168"")", attribute2);
}
}
}

Expand Down
31 changes: 29 additions & 2 deletions src/EditorFeatures/Test/SolutionCrawler/WorkCoordinatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public async Task DynamicallyAddAnalyzer()
Assert.Equal(10, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None)]
[InlineData(BackgroundAnalysisScope.ActiveFile)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects)]
[InlineData(BackgroundAnalysisScope.FullSolution)]
Expand Down Expand Up @@ -105,6 +106,7 @@ internal async Task SolutionAdded_Simple(BackgroundAnalysisScope analysisScope)
Assert.Equal(expectedDocumentEvents, worker.SyntaxDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None)]
[InlineData(BackgroundAnalysisScope.ActiveFile)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects)]
[InlineData(BackgroundAnalysisScope.FullSolution)]
Expand All @@ -120,6 +122,7 @@ internal async Task SolutionAdded_Complex(BackgroundAnalysisScope analysisScope)
Assert.Equal(expectedDocumentEvents, worker.SyntaxDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None)]
[InlineData(BackgroundAnalysisScope.ActiveFile)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects)]
[InlineData(BackgroundAnalysisScope.FullSolution)]
Expand All @@ -135,6 +138,7 @@ internal async Task Solution_Remove(BackgroundAnalysisScope analysisScope)
Assert.Equal(10, worker.InvalidateDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None)]
[InlineData(BackgroundAnalysisScope.ActiveFile)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects)]
[InlineData(BackgroundAnalysisScope.FullSolution)]
Expand All @@ -150,6 +154,7 @@ internal async Task Solution_Clear(BackgroundAnalysisScope analysisScope)
Assert.Equal(10, worker.InvalidateDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None)]
[InlineData(BackgroundAnalysisScope.ActiveFile)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects)]
[InlineData(BackgroundAnalysisScope.FullSolution)]
Expand All @@ -169,6 +174,7 @@ internal async Task Solution_Reload(BackgroundAnalysisScope analysisScope)
Assert.Equal(expectedProjectEvents, worker.ProjectIds.Count);
}

[InlineData(BackgroundAnalysisScope.None)]
[InlineData(BackgroundAnalysisScope.ActiveFile)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects)]
[InlineData(BackgroundAnalysisScope.FullSolution)]
Expand All @@ -192,6 +198,7 @@ internal async Task Solution_Change(BackgroundAnalysisScope analysisScope)
Assert.Equal(expectedDocumentEvents, worker.SyntaxDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None)]
[InlineData(BackgroundAnalysisScope.ActiveFile)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects)]
[InlineData(BackgroundAnalysisScope.FullSolution)]
Expand All @@ -218,6 +225,7 @@ internal async Task Project_Add(BackgroundAnalysisScope analysisScope)
Assert.Equal(expectedDocumentEvents, worker.SyntaxDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None)]
[InlineData(BackgroundAnalysisScope.ActiveFile)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects)]
[InlineData(BackgroundAnalysisScope.FullSolution)]
Expand All @@ -236,6 +244,7 @@ internal async Task Project_Remove(BackgroundAnalysisScope analysisScope)
Assert.Equal(5, worker.InvalidateDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None)]
[InlineData(BackgroundAnalysisScope.ActiveFile)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects)]
[InlineData(BackgroundAnalysisScope.FullSolution)]
Expand All @@ -256,6 +265,7 @@ internal async Task Project_Change(BackgroundAnalysisScope analysisScope)
Assert.Equal(1, worker.InvalidateDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -283,6 +293,7 @@ internal async Task Project_AssemblyName_Change(BackgroundAnalysisScope analysis
Assert.Equal(expectedDocumentEvents, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -310,6 +321,7 @@ internal async Task Project_DefaultNamespace_Change(BackgroundAnalysisScope anal
Assert.Equal(expectedDocumentEvents, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -337,6 +349,7 @@ internal async Task Project_AnalyzerOptions_Change(BackgroundAnalysisScope analy
Assert.Equal(expectedDocumentEvents, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -364,6 +377,7 @@ internal async Task Project_OutputFilePath_Change(BackgroundAnalysisScope analys
Assert.Equal(expectedDocumentEvents, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -391,6 +405,7 @@ internal async Task Project_OutputRefFilePath_Change(BackgroundAnalysisScope ana
Assert.Equal(expectedDocumentEvents, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -418,6 +433,7 @@ internal async Task Project_CompilationOutputInfo_Change(BackgroundAnalysisScope
Assert.Equal(expectedDocumentEvents, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -504,6 +520,7 @@ public async Task Test_BackgroundAnalysisScopeOptionChanged_FullSolution()
Assert.Equal(2, worker.ProjectIds.Count);
}

[InlineData(BackgroundAnalysisScope.None)]
[InlineData(BackgroundAnalysisScope.ActiveFile)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects)]
[InlineData(BackgroundAnalysisScope.FullSolution)]
Expand All @@ -524,6 +541,7 @@ internal async Task Project_Reload(BackgroundAnalysisScope analysisScope)
Assert.Equal(expectedProjectEvents, worker.ProjectIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -557,6 +575,7 @@ internal async Task Document_Add(BackgroundAnalysisScope analysisScope, bool act
Assert.Equal(expectedDocumentSemanticEvents, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -586,6 +605,7 @@ internal async Task Document_Remove(BackgroundAnalysisScope analysisScope, bool
Assert.Equal(expectedDocumentInvalidatedEvents, worker.InvalidateDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand All @@ -611,6 +631,7 @@ internal async Task Document_Reload(BackgroundAnalysisScope analysisScope, bool
Assert.Equal(0, worker.InvalidateDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -656,6 +677,7 @@ internal async Task Document_Reanalyze(BackgroundAnalysisScope analysisScope, bo
Assert.Equal(expectedReanalyzeDocumentCount, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand All @@ -681,6 +703,7 @@ internal async Task Document_Change(BackgroundAnalysisScope analysisScope, bool
Assert.Equal(expectedDocumentEvents, worker.SyntaxDocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -719,6 +742,7 @@ internal async Task Document_AdditionalFileChange(BackgroundAnalysisScope analys
Assert.Equal(expectedDocumentSemanticEvents, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -758,6 +782,7 @@ internal async Task Document_AnalyzerConfigFileChange(BackgroundAnalysisScope an
Assert.Equal(expectedDocumentSemanticEvents, worker.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -812,6 +837,7 @@ internal async Task Document_Cancellation(BackgroundAnalysisScope analysisScope,
Assert.Equal(expectedDocumentSemanticEvents, analyzer.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand Down Expand Up @@ -919,6 +945,7 @@ public async Task Document_InvocationReasons()
Assert.Equal(5, analyzer.DocumentIds.Count);
}

[InlineData(BackgroundAnalysisScope.None, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, false)]
[InlineData(BackgroundAnalysisScope.ActiveFile, true)]
[InlineData(BackgroundAnalysisScope.OpenFilesAndProjects, false)]
Expand All @@ -943,14 +970,14 @@ internal async Task Document_ActiveDocumentChanged(BackgroundAnalysisScope analy
var expectedSyntaxDocumentEvents = (analysisScope, hasActiveDocumentBefore) switch
{
(BackgroundAnalysisScope.ActiveFile, _) => 1,
(BackgroundAnalysisScope.OpenFilesAndProjects or BackgroundAnalysisScope.FullSolution, _) => 0,
(BackgroundAnalysisScope.OpenFilesAndProjects or BackgroundAnalysisScope.FullSolution or BackgroundAnalysisScope.None, _) => 0,
_ => throw ExceptionUtilities.Unreachable,
};

var expectedDocumentEvents = (analysisScope, hasActiveDocumentBefore) switch
{
(BackgroundAnalysisScope.ActiveFile, _) => 5,
(BackgroundAnalysisScope.OpenFilesAndProjects or BackgroundAnalysisScope.FullSolution, _) => 0,
(BackgroundAnalysisScope.OpenFilesAndProjects or BackgroundAnalysisScope.FullSolution or BackgroundAnalysisScope.None, _) => 0,
_ => throw ExceptionUtilities.Unreachable,
};

Expand Down
Loading

0 comments on commit f6d80fd

Please sign in to comment.