Exemple #1
0
        private ProjectBuilder(Project project)
        {
            Project       = project;
            FileExtension = project.Language == LanguageNames.CSharp ? ".cs" : ".vb";

            this.solutionWrapper = new Lazy <SolutionBuilder>(() => SolutionBuilder.FromSolution(project.Solution));
        }
Exemple #2
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));
                    }
                }
            }
        }
Exemple #3
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);
 }
Exemple #4
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);
        }
Exemple #5
0
        public static void VerifyNoIssueReportedInTest(string path, SonarDiagnosticAnalyzer diagnosticAnalyzer,
                                                       IEnumerable <MetadataReference> additionalReferences = null)
        {
            var compilation = SolutionBuilder.Create()
                              .AddTestProject(AnalyzerLanguage.FromPath(path))
                              .AddReferences(additionalReferences)
                              .AddDocument(path)
                              .GetCompilation();

            DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer);
        }
Exemple #6
0
        private static void VerifyAnalyzer(IEnumerable <string> paths, SonarDiagnosticAnalyzer[] diagnosticAnalyzers,
                                           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, diagnosticAnalyzers, checkMode);
            }
        }
Exemple #7
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);
            }
        }
Exemple #8
0
 public static void VerifyUtilityAnalyzer<TMessage>(IEnumerable<string> paths,
                                                    UtilityAnalyzerBase diagnosticAnalyzer,
                                                    string protobufPath,
                                                    Action<IReadOnlyList<TMessage>> verifyProtobuf,
                                                    IEnumerable<ParseOptions> options = null)
     where TMessage : IMessage<TMessage>, new()
 {
     var solutionBuilder = SolutionBuilder.CreateSolutionFromPaths(paths);
     foreach (var compilation in solutionBuilder.Compile(options?.ToArray()))
     {
         DiagnosticVerifier.Verify(compilation, diagnosticAnalyzer, CompilationErrorBehavior.Default);
         verifyProtobuf(ReadProtobuf(protobufPath).ToList());
     }
Exemple #9
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 SnippetCompiler(string code, bool ignoreErrors, AnalyzerLanguage language, IEnumerable <MetadataReference> additionalReferences = null)
        {
            CompilationOptions compilationOptions = language == AnalyzerLanguage.CSharp
                ? (CompilationOptions) new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                : (CompilationOptions) new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

            this.compilation = SolutionBuilder
                               .Create()
                               .AddProject(language, createExtraEmptyFile: false)
                               .AddSnippet(code)
                               .AddReferences(additionalReferences ?? Enumerable.Empty <MetadataReference>())
                               .GetCompilation(compilationOptions: compilationOptions);

            if (!ignoreErrors && HasCompilationErrors(compilation))
            {
                DumpCompilationErrors(compilation);
                throw new System.InvalidOperationException("Test setup error: test code snippet did not compile. See output window for details.");
            }

            this.SyntaxTree    = compilation.SyntaxTrees.First();
            this.SemanticModel = compilation.GetSemanticModel(this.SyntaxTree);
        }
Exemple #11
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);
                }
            }
        }
 private static Document CreateDocument(string path, IEnumerable <MetadataReference> additionalReferences) =>
 SolutionBuilder.Create()
 .AddProject(AnalyzerLanguage.FromPath(path))
 .AddReferences(additionalReferences)
 .AddDocument(path, true)
 .FindDocument(Path.GetFileName(path));