示例#1
0
 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);
        }
示例#3
0
 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);
示例#4
0
 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);
 }
示例#5
0
        // 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);
            }
        }
示例#6
0
        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);
示例#9
0
        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));
                    }
                }
            }
        }
示例#10
0
        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)));
        }
示例#11
0
 public static void VerifyNoIssueReported(Compilation compilation, DiagnosticAnalyzer diagnosticAnalyzer,
                                          CompilationErrorBehavior checkMode = CompilationErrorBehavior.Default)
 {
     GetDiagnostics(compilation, diagnosticAnalyzer, checkMode).Should().BeEmpty();
 }
示例#12
0
 public static void Verify(Compilation compilation, DiagnosticAnalyzer diagnosticAnalyzer, CompilationErrorBehavior checkMode) =>
 Verify(compilation, new [] { diagnosticAnalyzer }, checkMode);
示例#13
0
 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();
            }
        }
示例#16
0
        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);
                }
            }
        }
示例#17
0
 // 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);
示例#19
0
 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);
        }
示例#21
0
 public static void VerifyAnalyzer(string path,
                                   DiagnosticAnalyzer diagnosticAnalyzer,
                                   CompilationErrorBehavior checkMode,
                                   IEnumerable <MetadataReference> additionalReferences = null) =>
 VerifyAnalyzer(new[] { path }, new[] { diagnosticAnalyzer }, null, checkMode, OutputKind.DynamicallyLinkedLibrary, additionalReferences);
示例#22
0
        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)));
        }
示例#23
0
        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);
        }
示例#24
0
        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);
        }
示例#25
0
 public static void VerifyCSharpAnalyzer(string snippet,
                                         SonarDiagnosticAnalyzer diagnosticAnalyzer,
                                         CompilationErrorBehavior checkMode,
                                         IEnumerable <MetadataReference> additionalReferences = null) =>
 VerifyCSharpAnalyzer(snippet, diagnosticAnalyzer, null, checkMode, additionalReferences);
示例#26
0
        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();
            }
        }
示例#27
0
        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);
            }
        }