public static void VerifyAnalyzerInTest(IEnumerable <string> paths, DiagnosticAnalyzer diagnosticAnalyzer, IEnumerable <ParseOptions> options = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, IEnumerable <MetadataReference> additionalReferences = null) => VerifyAnalyzer(paths, new[] { diagnosticAnalyzer }, options, checkMode, outputKind, AddTestReference(additionalReferences));
private static void VerifyEmpty(string name, string content, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default) { AnalyzerLanguage language; if (name.EndsWith(".cs")) { language = AnalyzerLanguage.CSharp; } else if (name.EndsWith(".vb")) { language = AnalyzerLanguage.VisualBasic; } else { throw new ArgumentException($"Was expecting the file name to end with '.cs' or '.vb', got '{name}'.", nameof(name)); } var compilation = SolutionBuilder .Create() .AddProject(language, createExtraEmptyFile: false) .AddSnippet(content, name) .GetCompilation(); DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer, checkMode); }
public static void VerifyAnalyzer(string path, DiagnosticAnalyzer[] diagnosticAnalyzers, IEnumerable <ParseOptions> options = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, IEnumerable <MetadataReference> additionalReferences = null) => VerifyAnalyzer(new[] { path }, diagnosticAnalyzers, options, checkMode, outputKind, additionalReferences);
public static void VerifyVisualBasicAnalyzer(string snippet, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, IEnumerable<MetadataReference> additionalReferences = null) { var solution = SolutionBuilder.Create().AddProject(AnalyzerLanguage.VisualBasic).AddSnippet(snippet).AddReferences(additionalReferences).GetSolution(); VerifyAnalyzer(solution, new DiagnosticAnalyzer[] { diagnosticAnalyzer }, null, checkMode); }
// This method is checking only the expected issues from the first file path provided. The rest of the paths are added to the // project for enabling testing of different scenarios. public static void VerifyAnalyzer(IEnumerable <string> paths, SonarDiagnosticAnalyzer diagnosticAnalyzer, IEnumerable <ParseOptions> options = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, IEnumerable <MetadataReference> additionalReferences = null) { var solutionBuilder = SolutionBuilder.CreateSolutionFromPaths(paths, additionalReferences); foreach (var compilation in solutionBuilder.Compile(options?.ToArray())) { DiagnosticVerifier.Verify(compilation, diagnosticAnalyzer, checkMode); } }
public static void VerifyVisualBasicAnalyzer(string snippet, SonarDiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, IEnumerable <MetadataReference> additionalReferences = null) { var solution = SolutionBuilder .Create() .AddProject(AnalyzerLanguage.VisualBasic) .AddSnippet(snippet) .AddReferences(additionalReferences) .GetSolution(); // ToDo: add [CallerLineNumber]int lineNumber = 0 // then add ability to shift result reports with this line number foreach (var compilation in solution.Compile()) { DiagnosticVerifier.Verify(compilation, diagnosticAnalyzer, checkMode); } }
public static void Verify(Compilation compilation, DiagnosticAnalyzer[] diagnosticAnalyzers, CompilationErrorBehavior checkMode, string sonarProjectConfigPath = null) => Verify(compilation, diagnosticAnalyzers, checkMode, compilation.SyntaxTrees.Skip(1).First().GetText(), sonarProjectConfigPath);
public static void Verify(Compilation compilation, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode, SourceText source, string sonarProjectConfigPath = null) => Verify(compilation, new[] { diagnosticAnalyzer }, checkMode, source);
public static void VerifyUtilityAnalyzer <TMessage>(IEnumerable <string> paths, UtilityAnalyzerBase diagnosticAnalyzer, string protobufPath, Action <IList <TMessage> > verifyProtobuf, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default) where TMessage : IMessage <TMessage>, new() { var solutionBuilder = SolutionBuilder.CreateSolutionFromPaths(paths); foreach (var compilation in solutionBuilder.Compile()) { DiagnosticVerifier.Verify(compilation, diagnosticAnalyzer, checkMode); verifyProtobuf(ReadProtobuf(protobufPath).ToList()); } IEnumerable <TMessage> ReadProtobuf(string path) { using (var input = File.OpenRead(path)) { var parser = new MessageParser <TMessage>(() => new TMessage()); while (input.Position < input.Length) { yield return(parser.ParseDelimitedFrom(input)); } } } }
public static IEnumerable <Diagnostic> GetDiagnostics(Compilation compilation, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode) { var ids = diagnosticAnalyzer.SupportedDiagnostics .Select(diagnostic => diagnostic.Id) .ToHashSet(); return(GetAllDiagnostics(compilation, new[] { diagnosticAnalyzer }, checkMode) .Where(d => ids.Contains(d.Id))); }
public static void VerifyNoIssueReported(Compilation compilation, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default) { GetDiagnostics(compilation, diagnosticAnalyzer, checkMode).Should().BeEmpty(); }
public static void Verify(Compilation compilation, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode) => Verify(compilation, new [] { diagnosticAnalyzer }, checkMode);
public static IEnumerable <Diagnostic> GetDiagnostics(Compilation compilation, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode, bool verifyNoExceptionIsThrown = true) => GetDiagnostics(compilation, new[] { diagnosticAnalyzer }, checkMode, verifyNoExceptionIsThrown);
public static void VerifyNoIssueReported(Compilation compilation, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, string sonarProjectConfigPath = null) => GetDiagnostics(compilation, diagnosticAnalyzer, checkMode, sonarProjectConfigPath: sonarProjectConfigPath).Should().BeEmpty();
public static void Verify(Compilation compilation, DiagnosticAnalyzer[] diagnosticAnalyzers, CompilationErrorBehavior checkMode, SourceText source, string sonarProjectConfigPath = null) { SuppressionHandler.HookSuppression(); try { var diagnostics = GetDiagnostics(compilation, diagnosticAnalyzers, checkMode, sonarProjectConfigPath: sonarProjectConfigPath); var expectedIssues = IssueLocationCollector.GetExpectedIssueLocations(source.Lines).ToList(); CompareActualToExpected(compilation.LanguageVersionString(), diagnostics, expectedIssues, false); // When there are no diagnostics reported from the test (for example the FileLines analyzer // does not report in each call to Verifier.VerifyAnalyzer) we skip the check for the extension // method. if (diagnostics.Any()) { SuppressionHandler.ExtensionMethodsCalledForAllDiagnostics(diagnosticAnalyzers).Should().BeTrue("The ReportDiagnosticWhenActive should be used instead of ReportDiagnostic"); } } finally { SuppressionHandler.UnHookSuppression(); } }
public static void VerifyNoIssueReported(string path, SonarDiagnosticAnalyzer diagnosticAnalyzer, IEnumerable <ParseOptions> options = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, IEnumerable <MetadataReference> additionalReferences = null) { var projectBuilder = SolutionBuilder.Create() .AddProject(AnalyzerLanguage.FromPath(path)) .AddReferences(additionalReferences) .AddDocument(path); if (options == null) { var compilation = projectBuilder.GetCompilation(null); DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer, checkMode); } else { foreach (var option in options) { var compilation = projectBuilder.GetCompilation(option); DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer, checkMode); } } }
// This method is checking only the expected issues from the first file path provided. The rest of the paths are added to the // project for enabling testing of different scenarios. public static void VerifyAnalyzer(IEnumerable <string> paths, SonarDiagnosticAnalyzer diagnosticAnalyzer, IEnumerable <ParseOptions> options = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, IEnumerable <MetadataReference> additionalReferences = null) => VerifyAnalyzer(paths, new [] { diagnosticAnalyzer }, options, checkMode, additionalReferences);
public static IEnumerable <Diagnostic> GetDiagnostics(Compilation compilation, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode, bool verifyNoExceptionIsThrown = true, string sonarProjectConfigPath = null) => GetDiagnostics(compilation, new[] { diagnosticAnalyzer }, checkMode, verifyNoExceptionIsThrown, sonarProjectConfigPath);
public static void VerifyAnalyzer(string path, SonarDiagnosticAnalyzer[] diagnosticAnalyzers, IEnumerable <ParseOptions> options = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, IEnumerable <MetadataReference> additionalReferences = null) { VerifyAnalyzer(new[] { path }, diagnosticAnalyzers, options, checkMode, additionalReferences); }
public static ImmutableArray <Diagnostic> GetAllDiagnostics(Compilation compilation, IEnumerable <DiagnosticAnalyzer> diagnosticAnalyzers, CompilationErrorBehavior checkMode, bool verifyNoException = true, CancellationToken?cancellationToken = null, string sonarProjectConfigPath = null) { var supportedDiagnostics = diagnosticAnalyzers .SelectMany(analyzer => analyzer.SupportedDiagnostics) .Select(diagnostic => new KeyValuePair <string, ReportDiagnostic>(diagnostic.Id, ReportDiagnostic.Warn)) .Concat(new[] { new KeyValuePair <string, ReportDiagnostic>(AnalyzerFailedDiagnosticId, ReportDiagnostic.Error) }); var compilationOptions = compilation.Options.WithSpecificDiagnosticOptions(supportedDiagnostics); var actualToken = cancellationToken ?? CancellationToken.None; var analyzerOptions = string.IsNullOrWhiteSpace(sonarProjectConfigPath) ? null : TestHelper.CreateOptions(sonarProjectConfigPath); var diagnostics = compilation .WithOptions(compilationOptions) .WithAnalyzers(diagnosticAnalyzers.ToImmutableArray(), analyzerOptions) .GetAllDiagnosticsAsync(actualToken) .Result; if (!actualToken.IsCancellationRequested) { if (verifyNoException) { VerifyNoExceptionThrown(diagnostics); } if (checkMode == CompilationErrorBehavior.FailTest) { VerifyBuildErrors(diagnostics, compilation); } } return(diagnostics); }
public static void VerifyAnalyzer(string path, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode, IEnumerable <MetadataReference> additionalReferences = null) => VerifyAnalyzer(new[] { path }, new[] { diagnosticAnalyzer }, null, checkMode, OutputKind.DynamicallyLinkedLibrary, additionalReferences);
private static IEnumerable <Diagnostic> GetDiagnostics(Compilation compilation, DiagnosticAnalyzer[] diagnosticAnalyzers, CompilationErrorBehavior checkMode, bool verifyNoExceptionIsThrown = true) { var ids = diagnosticAnalyzers .SelectMany(analyzer => analyzer.SupportedDiagnostics) .Select(diagnostic => diagnostic.Id) .Distinct() .ToHashSet(); return(GetAllDiagnostics(compilation, diagnosticAnalyzers, checkMode, verifyNoExceptionIsThrown) .Where(d => ids.Contains(d.Id))); }
public static void VerifyNoExceptionThrown(string path, IEnumerable <DiagnosticAnalyzer> diagnosticAnalyzers, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default) { var compilation = SolutionBuilder .Create() .AddProject(AnalyzerLanguage.FromPath(path)) .AddDocument(path) .GetCompilation(); DiagnosticVerifier.GetAllDiagnostics(compilation, diagnosticAnalyzers, checkMode); }
public static ImmutableArray <Diagnostic> GetAllDiagnostics(Compilation compilation, IEnumerable <DiagnosticAnalyzer> diagnosticAnalyzers, CompilationErrorBehavior checkMode, bool verifyNoException = true, CancellationToken?cancellationToken = null) { var compilationOptions = compilation.Language == LanguageNames.CSharp ? (CompilationOptions) new CS.CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true) : new VB.VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary); var supportedDiagnostics = diagnosticAnalyzers .SelectMany(analyzer => analyzer.SupportedDiagnostics) .Select(diagnostic => new KeyValuePair <string, ReportDiagnostic>(diagnostic.Id, ReportDiagnostic.Warn)) .Concat(new[] { new KeyValuePair <string, ReportDiagnostic>(AnalyzerFailedDiagnosticId, ReportDiagnostic.Error) }); compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(supportedDiagnostics); var actualToken = cancellationToken.HasValue ? cancellationToken.Value : CancellationToken.None; var diagnostics = compilation .WithOptions(compilationOptions) .WithAnalyzers(diagnosticAnalyzers.ToImmutableArray()) .GetAllDiagnosticsAsync(actualToken) .Result; if (!actualToken.IsCancellationRequested) { if (verifyNoException) { VerifyNoExceptionThrown(diagnostics); } if (checkMode == CompilationErrorBehavior.FailTest) { VerifyBuildErrors(diagnostics, compilation); } } return(diagnostics); }
public static void VerifyCSharpAnalyzer(string snippet, SonarDiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode, IEnumerable <MetadataReference> additionalReferences = null) => VerifyCSharpAnalyzer(snippet, diagnosticAnalyzer, null, checkMode, additionalReferences);
public static void Verify(Compilation compilation, DiagnosticAnalyzer[] diagnosticAnalyzers, CompilationErrorBehavior checkMode) { try { SuppressionHandler.HookSuppression(); var diagnostics = GetDiagnostics(compilation, diagnosticAnalyzers, checkMode); var expectedIssues = new IssueLocationCollector() .GetExpectedIssueLocations(compilation.SyntaxTrees.Skip(1).First().GetText().Lines) .ToList(); CompareActualToExpected(diagnostics, expectedIssues, false); // When there are no diagnostics reported from the test (for example the FileLines analyzer // does not report in each call to Verifier.VerifyAnalyzer) we skip the check for the extension // method. if (diagnostics.Any()) { SuppressionHandler.ExtensionMethodsCalledForAllDiagnostics(diagnosticAnalyzers).Should() .BeTrue("The ReportDiagnosticWhenActive should be used instead of ReportDiagnostic"); } } finally { SuppressionHandler.UnHookSuppression(); } }
private static void VerifyAnalyzer(IEnumerable <string> paths, SonarDiagnosticAnalyzer[] diagnosticAnalyzers, IEnumerable <ParseOptions> options = null, CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default, OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary, IEnumerable <MetadataReference> additionalReferences = null) { var solutionBuilder = SolutionBuilder.CreateSolutionFromPaths(paths, outputKind, additionalReferences, IsSupportForCSharp9InitNeeded(options)); foreach (var compilation in solutionBuilder.Compile(options?.ToArray())) { DiagnosticVerifier.Verify(compilation, diagnosticAnalyzers, checkMode); } }