예제 #1
0
        private void ArrangeDatabaseService(
            IEnumerable <CmsSettingsKey> cmsSettingsKeys,
            IEnumerable <CmsSettingsCategory> cmsSettingsCategories
            )
        {
            var cmsSettingsKeysNames = new SettingsKeyAnalyzers(null !)
                                       .Analyzers
                                       .Select(
                analyzer => analyzer.Parameters[0]
                .Name
                );

            mockDatabaseService.SetupExecuteSqlFromFile(
                Scripts.GetSecurityCmsSettings,
                nameof(cmsSettingsKeysNames),
                cmsSettingsKeysNames,
                cmsSettingsKeys
                );

            var cmsSettingsCategoryIdsOnPaths = cmsSettingsCategories
                                                .Select(category => category.CategoryID.ToString());

            mockDatabaseService.SetupExecuteSqlFromFile(
                Scripts.GetCmsSettingsCategories,
                nameof(cmsSettingsCategoryIdsOnPaths),
                cmsSettingsCategoryIdsOnPaths,
                cmsSettingsCategories
                );
        }
        public override void Initialize(AnalysisContext context)
        {
            base.Initialize(context);

            var supportedKinds = Analyzers.Select(p => ((ISyntaxNodeAnalyzer)p).Kind).Distinct();

            foreach (var syntaxKind in supportedKinds)
            {
                context.RegisterSyntaxNodeAction(p => { AnalyzeNode(p, syntaxKind); }
                                                 , syntaxKind);
            }
        }
예제 #3
0
        private void VerifyDiagnostics(
            IEnumerable <Diagnostic> actualDiagnostics,
            IEnumerable <Diagnostic> expectedDiagnostics,
            bool checkAdditionalLocations,
            CancellationToken cancellationToken = default)
        {
            int expectedCount = 0;
            int actualCount   = 0;

            using (IEnumerator <Diagnostic> expectedEnumerator = expectedDiagnostics.OrderBy(f => f, DiagnosticComparer.SpanStart).GetEnumerator())
                using (IEnumerator <Diagnostic> actualEnumerator = actualDiagnostics.OrderBy(f => f, DiagnosticComparer.SpanStart).GetEnumerator())
                {
                    if (!expectedEnumerator.MoveNext())
                    {
                        throw new InvalidOperationException($"'{nameof(expectedDiagnostics)}' contains no elements.");
                    }

                    do
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        expectedCount++;

                        Diagnostic expectedDiagnostic = expectedEnumerator.Current;

                        if (SupportedDiagnostics.IndexOf(expectedDiagnostic.Descriptor, DiagnosticDescriptorComparer.Id) == -1)
                        {
                            Assert.True(false, $"Diagnostic \"{expectedDiagnostic.Id}\" is not supported by analyzer(s) {string.Join(", ", Analyzers.Select(f => f.GetType().Name))}.");
                        }

                        if (actualEnumerator.MoveNext())
                        {
                            actualCount++;

                            VerifyDiagnostic(actualEnumerator.Current, expectedDiagnostic, checkAdditionalLocations: checkAdditionalLocations);
                        }
                        else
                        {
                            while (expectedEnumerator.MoveNext())
                            {
                                expectedCount++;
                            }

                            Assert.True(false, $"Mismatch between number of diagnostics returned, expected: {expectedCount} actual: {actualCount}{actualDiagnostics.ToDebugString()}");
                        }
                    } while (expectedEnumerator.MoveNext());

                    if (actualEnumerator.MoveNext())
                    {
                        actualCount++;

                        while (actualEnumerator.MoveNext())
                        {
                            actualCount++;
                        }

                        Assert.True(false, $"Mismatch between number of diagnostics returned, expected: {expectedCount} actual: {actualCount}{actualDiagnostics.ToDebugString()}");
                    }
                }
        }
예제 #4
0
        public async Task VerifyNoDiagnosticAsync(
            string source,
            IEnumerable <string> additionalSources = null,
            CodeVerificationOptions options        = null,
            CancellationToken cancellationToken    = default)
        {
            cancellationToken.ThrowIfCancellationRequested();

            options ??= Options;

            if (SupportedDiagnostics.IndexOf(Descriptor, DiagnosticDescriptorComparer.Id) == -1)
            {
                Assert.True(false, $"Diagnostic \"{Descriptor.Id}\" is not supported by analyzer(s) {string.Join(", ", Analyzers.Select(f => f.GetType().Name))}.");
            }

            using (Workspace workspace = new AdhocWorkspace())
            {
                Project project = WorkspaceFactory.AddProject(workspace.CurrentSolution, options);

                Document document = WorkspaceFactory.AddDocument(project, source, additionalSources);

                Compilation compilation = await document.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);

                ImmutableArray <Diagnostic> compilerDiagnostics = compilation.GetDiagnostics(cancellationToken);

                VerifyCompilerDiagnostics(compilerDiagnostics, options);

                compilation = UpdateCompilation(compilation);

                ImmutableArray <Diagnostic> analyzerDiagnostics = await compilation.GetAnalyzerDiagnosticsAsync(Analyzers, DiagnosticComparer.SpanStart, cancellationToken).ConfigureAwait(false);

                foreach (Diagnostic diagnostic in analyzerDiagnostics)
                {
                    if (string.Equals(diagnostic.Id, Descriptor.Id, StringComparison.Ordinal))
                    {
                        Assert.True(false, $"No diagnostic expected{analyzerDiagnostics.Where(f => string.Equals(f.Id, Descriptor.Id, StringComparison.Ordinal)).ToDebugString()}");
                    }
                }
            }
        }
예제 #5
0
        public override ReportResults GetResults()
        {
            var cmsSettingsKeysNames = new SettingsKeyAnalyzers(null !)
                                       .Analyzers
                                       .Select(
                analyzer => analyzer.Parameters[0]
                .Name
                );

            var cmsSettingsKeys = databaseService.ExecuteSqlFromFile <CmsSettingsKey>(
                Scripts.GetSecurityCmsSettings,
                new
            {
                cmsSettingsKeysNames
            }
                );

            var cmsSettingsKeyResults = GetCmsSettingsKeyResults(cmsSettingsKeys);

            var cmsSettingsCategoryIdsOnPaths = cmsSettingsKeyResults
                                                .SelectMany(cmsSettingsKeyResult => cmsSettingsKeyResult.GetCategoryIdsOnPath())
                                                .Distinct();

            var cmsSettingsCategories = databaseService.ExecuteSqlFromFile <CmsSettingsCategory>(
                Scripts.GetCmsSettingsCategories,
                new
            {
                cmsSettingsCategoryIdsOnPaths
            }
                );

            var sites = instanceService
                        .GetInstanceDetails()
                        .Sites
                        .Append(
                new CmsSite
            {
                SiteId   = 0,
                SiteName = Metadata.Terms.GlobalSiteName
            }
                );

            var instancePath = instanceService.CurrentInstance.Path;

            var resxValues =
                cmsFileService.GetResourceStringsFromResx(
                    instancePath,
                    DefaultKenticoPaths.PrimaryResxFile
                    );

            var localizedCmsSettingsKeyResults = cmsSettingsKeyResults
                                                 .Select(
                cmsSettingsKeyResult => new CmsSettingsKeyResult(
                    cmsSettingsKeyResult,
                    cmsSettingsCategories,
                    sites,
                    resxValues
                    )
                );

            var webConfigXml = cmsFileService.GetXDocument(
                instancePath,
                DefaultKenticoPaths.WebConfigFile
                );

            var webConfigSettingsResults = GetWebConfigSettingsResults(webConfigXml);

            return(CompileResults(
                       localizedCmsSettingsKeyResults,
                       webConfigSettingsResults
                       ));
        }
예제 #6
0
        public async Task GenerateAsync()
        {
            WriteAllText(
                @"Analyzers\README.md",
                MarkdownGenerator.CreateAnalyzersReadMe(Analyzers.Where(f => !f.IsObsolete), Comparer));

            WriteAllText(
                @"Analyzers\AnalyzersByCategory.md",
                MarkdownGenerator.CreateAnalyzersByCategoryMarkdown(Analyzers.Where(f => !f.IsObsolete), Comparer));

            VisualStudioInstance instance = MSBuildLocator.QueryVisualStudioInstances().Single();

            MSBuildLocator.RegisterInstance(instance);

            using (MSBuildWorkspace workspace = MSBuildWorkspace.Create())
            {
                workspace.WorkspaceFailed += (o, e) => Console.WriteLine(e.Diagnostic.Message);

                string solutionPath = Path.Combine(RootPath, "Roslynator.sln");

                Console.WriteLine($"Loading solution '{solutionPath}'");

                Solution solution = await workspace.OpenSolutionAsync(solutionPath).ConfigureAwait(false);

                Console.WriteLine($"Finished loading solution '{solutionPath}'");

                RoslynatorInfo roslynatorInfo = await RoslynatorInfo.Create(solution).ConfigureAwait(false);

                IOrderedEnumerable <SourceFile> sourceFiles = Analyzers
                                                              .Select(f => new SourceFile(f.Id, roslynatorInfo.GetAnalyzerFilesAsync(f.Identifier).Result))
                                                              .Concat(Refactorings
                                                                      .Select(f => new SourceFile(f.Id, roslynatorInfo.GetRefactoringFilesAsync(f.Identifier).Result)))
                                                              .OrderBy(f => f.Id);

                MetadataFile.SaveSourceFiles(sourceFiles, @"..\SourceFiles.xml");

                foreach (AnalyzerDescriptor analyzer in Analyzers)
                {
                    //IEnumerable<string> filePaths = await roslynatorInfo.GetAnalyzerFilesAsync(analyzer.Identifier).ConfigureAwait(false);

                    WriteAllText(
                        $@"..\docs\analyzers\{analyzer.Id}.md",
                        MarkdownGenerator.CreateAnalyzerMarkdown(analyzer, Array.Empty <string>()),
                        fileMustExists: false);
                }

                foreach (RefactoringDescriptor refactoring in Refactorings)
                {
                    //IEnumerable<string> filePaths = await roslynatorInfo.GetRefactoringFilesAsync(refactoring.Identifier).ConfigureAwait(false);

                    WriteAllText(
                        $@"..\docs\refactorings\{refactoring.Id}.md",
                        MarkdownGenerator.CreateRefactoringMarkdown(refactoring, Array.Empty <string>()),
                        fileMustExists: false);
                }

                foreach (CompilerDiagnosticDescriptor diagnostic in CompilerDiagnostics)
                {
                    //IEnumerable<string> filePaths = await roslynatorInfo.GetCompilerDiagnosticFilesAsync(diagnostic.Identifier).ConfigureAwait(false);

                    WriteAllText(
                        $@"..\docs\cs\{diagnostic.Id}.md",
                        MarkdownGenerator.CreateCompilerDiagnosticMarkdown(diagnostic, CodeFixes, Comparer, Array.Empty <string>()),
                        fileMustExists: false);
                }
            }

            WriteAllText(
                @"..\docs\refactorings\Refactorings.md",
                MarkdownGenerator.CreateRefactoringsMarkdown(Refactorings, Comparer));

            WriteAllText(
                @"Refactorings\README.md",
                MarkdownGenerator.CreateRefactoringsReadMe(Refactorings.Where(f => !f.IsObsolete), Comparer));

            WriteAllText(
                @"CodeFixes\README.md",
                MarkdownGenerator.CreateCodeFixesReadMe(CompilerDiagnostics, Comparer));

            WriteAllText(
                "DefaultConfigFile.xml",
                XmlGenerator.CreateDefaultConfigFile(Refactorings, CodeFixes));

            WriteAllText(
                "DefaultRuleSet.ruleset",
                XmlGenerator.CreateDefaultRuleSet(Analyzers));
        }