public async Task TestDisabledByDefaultAnalyzerEnabledWithEditorConfig(bool enabledWithEditorconfig) { using var workspace = new AdhocWorkspace(); var analyzerReference = new AnalyzerImageReference(ImmutableArray.Create <DiagnosticAnalyzer>(new DisabledByDefaultAnalyzer())); var options = workspace.CurrentSolution.Options .WithChangedOption(SolutionCrawlerOptions.BackgroundAnalysisScopeOption, LanguageNames.CSharp, BackgroundAnalysisScope.FullSolution); workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference }).WithOptions(options)); var project = workspace.AddProject( ProjectInfo.Create( ProjectId.CreateNewId(), VersionStamp.Create(), "CSharpProject", "CSharpProject", LanguageNames.CSharp, filePath: "z:\\CSharpProject.csproj")); if (enabledWithEditorconfig) { var editorconfigText = @$ "
public async Task DiagnosticAnalyzerDriverAllInOne() { var source = TestResource.AllInOneCSharpCode; // AllInOneCSharpCode has no properties with initializers or named types with primary constructors. var symbolKindsWithNoCodeBlocks = new HashSet <SymbolKind>(); symbolKindsWithNoCodeBlocks.Add(SymbolKind.Property); symbolKindsWithNoCodeBlocks.Add(SymbolKind.NamedType); var missingSyntaxNodes = new HashSet <SyntaxKind>(); // https://github.com/dotnet/roslyn/issues/44682 - Add to all in one missingSyntaxNodes.Add(SyntaxKind.WithExpression); missingSyntaxNodes.Add(SyntaxKind.RecordDeclaration); missingSyntaxNodes.Add(SyntaxKind.FunctionPointerType); var analyzer = new CSharpTrackingDiagnosticAnalyzer(); using var workspace = TestWorkspace.CreateCSharp(source, TestOptions.Regular); var analyzerReference = new AnalyzerImageReference(ImmutableArray.Create <DiagnosticAnalyzer>(analyzer)); var newSolution = workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference }) .Projects.Single().AddAdditionalDocument(name: "dummy.txt", text: "", filePath: "dummy.txt").Project.Solution; workspace.TryApplyChanges(newSolution); var document = workspace.CurrentSolution.Projects.Single().Documents.Single(); AccessSupportedDiagnostics(analyzer); await DiagnosticProviderTestUtilities.GetAllDiagnosticsAsync(document, new TextSpan(0, document.GetTextAsync().Result.Length)); analyzer.VerifyAllAnalyzerMembersWereCalled(); analyzer.VerifyAnalyzeSymbolCalledForAllSymbolKinds(); analyzer.VerifyAnalyzeNodeCalledForAllSyntaxKinds(missingSyntaxNodes); analyzer.VerifyOnCodeBlockCalledForAllSymbolAndMethodKinds(symbolKindsWithNoCodeBlocks, true); }
private void AddAnalyzersToWorkspace(TestWorkspace workspace) { var analyzerReference = new AnalyzerImageReference(OtherAnalyzers.Add(SuppressionAnalyzer)); workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference })); }
protected async Task TestPragmaOrAttributeAsync( string code, ParseOptions options, bool pragma, Func <SyntaxNode, bool> digInto, Func <string, bool> verifier, Func <CodeAction, bool> fixChecker ) { using (var workspace = CreateWorkspaceFromFile(code, options)) { var(analyzer, fixer) = CreateDiagnosticProviderAndFixer(workspace); var analyzerReference = new AnalyzerImageReference( ImmutableArray.Create <DiagnosticAnalyzer>(analyzer) ); workspace.TryApplyChanges( workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference }) ); var document = workspace.CurrentSolution.Projects.Single().Documents.Single(); var root = document.GetSyntaxRootAsync().GetAwaiter().GetResult(); var existingDiagnostics = root.GetDiagnostics().ToArray(); var descendants = root.DescendantNodesAndSelf(digInto).ToImmutableArray(); analyzer.AllNodes = descendants; var diagnostics = await DiagnosticProviderTestUtilities.GetAllDiagnosticsAsync( workspace, document, root.FullSpan ); foreach (var diagnostic in diagnostics) { if (!fixer.IsFixableDiagnostic(diagnostic)) { continue; } var fixes = fixer .GetFixesAsync( document, diagnostic.Location.SourceSpan, SpecializedCollections.SingletonEnumerable(diagnostic), CancellationToken.None ) .GetAwaiter() .GetResult(); if (fixes == null || fixes.Count() <= 0) { continue; } var fix = GetFix(fixes.Select(f => f.Action), pragma); if (fix == null) { continue; } // already same fix has been tested if (fixChecker(fix)) { continue; } var operations = fix.GetOperationsAsync(CancellationToken.None) .GetAwaiter() .GetResult(); var applyChangesOperation = operations.OfType <ApplyChangesOperation>().Single(); var newDocument = applyChangesOperation.ChangedSolution.Projects .Single() .Documents.Single(); var newTree = newDocument.GetSyntaxTreeAsync().GetAwaiter().GetResult(); var newText = newTree.GetText().ToString(); Assert.True(verifier(newText)); var newDiagnostics = newTree.GetDiagnostics(); Assert.Equal(0, existingDiagnostics.Except(newDiagnostics, this).Count()); } } }
public static async System.Threading.Tasks.Task <List <String> > CheckXMLCommentDocumentation(string path) { MSBuildLocator.RegisterDefaults(); List <string> issues = new List <string>(); using (var workspace = MSBuildWorkspace.Create()) { workspace.LoadMetadataForReferencedProjects = true; System.Diagnostics.Debug.WriteLine("Opening workspace"); var solution = await workspace.OpenSolutionAsync(path); System.Diagnostics.Debug.WriteLine("Creating Code Analyzer"); // Create a reference to the custom Code Analyzer to add to the projects var immutableArray = (new List <DiagnosticAnalyzer> { new InlineDocumentationAnalyzerAnalyzer() }).ToImmutableArray(); var analyzerImageReference = new AnalyzerImageReference(immutableArray); System.Diagnostics.Debug.WriteLine("Creating analyzer-laced solution"); // Add the custom code analzyer to the solution, so it will be used with all projects solution = solution.AddAnalyzerReference(analyzerImageReference); System.Diagnostics.Debug.WriteLine("Loaded analyzer-laced solution"); // Output the diagnostics foreach (var diagnostic in workspace.Diagnostics) { System.Diagnostics.Debug.WriteLine(diagnostic); Console.WriteLine(diagnostic.Message); } // Iterate over every project in the solution foreach (var project in solution.Projects) { // Get a compilation of the project asynchronously. // This provides us with syntax trees we can traverse var compilation = await project.GetCompilationAsync(); foreach (var tree in compilation.SyntaxTrees) { // Check the Class definitions in the syntax tree for correct commenting issues.AddRange(CheckForClassComments(tree)); // Check the Property definitions in the syntax tree for correct commenting issues.AddRange(CheckForPropertyComments(tree)); // Check the Method definitions in the syntax tree for correct commenting issues.AddRange(CheckForMethodComments(tree)); } } /* * foreach(var proj in solution.Projects) * { * var project = proj.AddAnalyzerReference(analyzerImageReference); * var compilation = await project.GetCompilationAsync(); * var diagnostics = compilation.GetDiagnostics(); * foreach(var diagnostic in diagnostics) * { * Console.WriteLine(diagnostic.GetMessage()); * } * } */ } return(issues); }