Ejemplo n.º 1
0
        public static SolutionBuilder CreateSolutionFromPaths(IEnumerable <string> paths,
                                                              OutputKind outputKind = OutputKind.DynamicallyLinkedLibrary,
                                                              IEnumerable <MetadataReference> additionalReferences = null,
                                                              bool isSupportForCSharp9InitNeeded = false)
        {
            if (paths == null || !paths.Any())
            {
                throw new ArgumentException("Please specify at least one file path to analyze.", nameof(paths));
            }

            var extensions = paths.Select(path => Path.GetExtension(path)).Distinct().ToList();

            if (extensions.Count != 1)
            {
                throw new ArgumentException("Please use a collection of paths with the same extension", nameof(paths));
            }

            var project = Create()
                          .AddProject(AnalyzerLanguage.FromPath(paths.First()), outputKind: outputKind)
                          .AddDocuments(paths)
                          .AddReferences(additionalReferences);

            if (isSupportForCSharp9InitNeeded)
            {
                project = project.AddSnippet(InitSnippet);
            }

            if (additionalReferences != null &&
                additionalReferences.Any(r => r.Display.Contains("\\netstandard")))
            {
                project = project.AddReferences(NetStandardMetadataReference.Netstandard);
            }

            return(project.GetSolution());
        }
Ejemplo n.º 2
0
        public static SolutionBuilder CreateSolutionFromPaths(IEnumerable <string> paths,
                                                              IEnumerable <MetadataReference> additionalReferences = null)
        {
            if (paths == null ||
                !paths.Any())
            {
                throw new ArgumentException("Please specify at least one file path to analyze.", nameof(paths));
            }

            var extensions = paths.Select(path => Path.GetExtension(path)).Distinct().ToList();

            if (extensions.Count != 1)
            {
                throw new ArgumentException("Please use a collection of paths with the same extension", nameof(paths));
            }

            var project = Create()
                          .AddProject(AnalyzerLanguage.FromPath(paths.First()))
                          .AddDocuments(paths)
                          .AddReferences(additionalReferences);

            if (additionalReferences != null &&
                additionalReferences.Any(r => r.Display.Contains("\\netstandard")))
            {
                project = project.AddReferences(NetStandardMetadataReference.Netstandard);
            }

            return(project.GetSolution());
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public static void VerifyNoExceptionThrown(string path, IEnumerable <DiagnosticAnalyzer> diagnosticAnalyzers)
        {
            var compilation = SolutionBuilder
                              .Create()
                              .AddProject(AnalyzerLanguage.FromPath(path))
                              .AddDocument(path)
                              .GetCompilation();

            var diagnostics = DiagnosticVerifier.GetAllDiagnostics(compilation, diagnosticAnalyzers);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 6
0
        private void RunAnalysisWithoutVerification(string path, SonarDiagnosticAnalyzer diagnosticAnalyzer,
                                                    IEnumerable <MetadataReference> additionalReferences = null)
        {
            var compilation = SolutionBuilder.Create()
                              .AddTestProject(AnalyzerLanguage.FromPath(path))
                              .AddReferences(additionalReferences)
                              .AddDocument(path)
                              .GetCompilation();

            DiagnosticVerifier.GetDiagnostics(compilation, diagnosticAnalyzer,
                                              CompilationErrorBehavior.FailTest,
                                              verifyNoExceptionIsThrown: false);
        }
Ejemplo n.º 7
0
        public static SolutionBuilder CreateSolutionFromPaths(IEnumerable <string> paths, params MetadataReference[] additionalReferences)
        {
            if (paths == null || !paths.Any())
            {
                throw new ArgumentException("Please specify at least one file path to analyze.", nameof(paths));
            }

            var extensions = paths.Select(path => Path.GetExtension(path)).Distinct().ToList();

            if (extensions.Count != 1)
            {
                throw new ArgumentException("Please use a collection of paths with the same extension", nameof(paths));
            }

            return(Create()
                   .AddProject(AnalyzerLanguage.FromPath(paths.First()))
                   .AddDocuments(paths)
                   .AddReferences(additionalReferences)
                   .GetSolution());
        }
Ejemplo n.º 8
0
        public static void VerifyNoIssueReported(string path, SonarDiagnosticAnalyzer diagnosticAnalyzer,
                                                 IEnumerable <ParseOptions> options = null, params MetadataReference[] additionalReferences)
        {
            var projectBuilder = SolutionBuilder.Create()
                                 .AddProject(AnalyzerLanguage.FromPath(path))
                                 .AddReferences(additionalReferences)
                                 .AddDocument(path);


            if (options == null)
            {
                var compilation = projectBuilder.GetCompilation(null);
                DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer);
            }
            else
            {
                foreach (var option in options)
                {
                    var compilation = projectBuilder.GetCompilation(option);
                    DiagnosticVerifier.VerifyNoIssueReported(compilation, diagnosticAnalyzer);
                }
            }
        }
Ejemplo n.º 9
0
 private static Document CreateDocument(string path, IEnumerable <MetadataReference> additionalReferences) =>
 SolutionBuilder.Create()
 .AddProject(AnalyzerLanguage.FromPath(path))
 .AddReferences(additionalReferences)
 .AddDocument(path, true)
 .FindDocument(Path.GetFileName(path));