protected void VerifyDiagnostics(string source, ICollection <MSBuildAnalyzer> analyzers, bool includeCoreDiagnostics, params MSBuildDiagnostic[] expectedDiagnostics) { var token = CancellationToken.None; var(env, _) = MSBuildTestEnvironment.EnsureInitialized(); var host = env.GetEditorHost(); var doc = MSBuildRootDocument.Parse( new StringTextSource(source), "FakeProject.csproj", null, host.GetService <MSBuildSchemaProvider> (), host.GetService <IRuntimeInformation> (), host.GetService <ITaskMetadataBuilder> (), token); var analyzerDriver = new MSBuildAnalyzerDriver(); if (analyzers != null && analyzers.Count > 0) { analyzerDriver.AddAnalyzers(analyzers); } else if (!includeCoreDiagnostics) { throw new ArgumentException("Analyzers can only be null or empty if core diagnostics are included", nameof(analyzers)); } var actualDiagnostics = analyzerDriver.Analyze(doc, includeCoreDiagnostics, token); foreach (var expectedDiag in expectedDiagnostics) { bool found = false; for (int i = 0; i < actualDiagnostics.Count; i++) { var actualDiag = actualDiagnostics[i]; // todo: compare properties if (actualDiag.Descriptor == expectedDiag.Descriptor && actualDiag.Span.Equals(expectedDiag.Span)) { found = true; actualDiagnostics.RemoveAt(i); break; } } if (!found) { Assert.Fail($"Did not find expected diagnostic {expectedDiag.Descriptor.Id}@{expectedDiag.Span.Start}-{expectedDiag.Span.End}"); } } if (actualDiagnostics.Count > 0) { var diag = actualDiagnostics[0]; Assert.Fail($"Found unexpected diagnostic {diag.Descriptor.Id}@{diag.Span.Start}-{diag.Span.End}"); } }
public MSBuildBackgroundParser( ITextBuffer buffer, IRuntimeInformation runtimeInformation, MSBuildSchemaProvider schemaProvider, ITaskMetadataBuilder taskMetadataBuilder) { RuntimeInformation = runtimeInformation ?? throw new ArgumentNullException(nameof(runtimeInformation)); SchemaProvider = schemaProvider ?? throw new ArgumentNullException(nameof(schemaProvider)); TaskMetadataBuilder = taskMetadataBuilder ?? throw new ArgumentNullException(nameof(taskMetadataBuilder)); XmlParser = XmlBackgroundParser.GetParser(buffer); XmlParser.ParseCompleted += XmlParseCompleted; if (buffer.Properties.TryGetProperty <ITextDocument> (typeof(ITextDocument), out var doc)) { filepath = doc.FilePath; doc.FileActionOccurred += OnFileAction; } analyzerDriver = new MSBuildAnalyzerDriver(); analyzerDriver.AddBuiltInAnalyzers(); }