Beispiel #1
0
        private static void GetDocumentAndExtensionManager(TestDiagnosticAnalyzerService diagnosticService, TestWorkspace workspace, out Document document, out EditorLayerExtensionManager.ExtensionManager extensionManager)
        {
            var incrementalAnalyzer = (IIncrementalAnalyzerProvider)diagnosticService;

            // register diagnostic engine to solution crawler
            var analyzer = incrementalAnalyzer.CreateIncrementalAnalyzer(workspace);

            var reference = new MockAnalyzerReference();
            var project   = workspace.CurrentSolution.Projects.Single().AddAnalyzerReference(reference);

            document         = project.Documents.Single();
            extensionManager = document.Project.Solution.Workspace.Services.GetService <IExtensionManager>() as EditorLayerExtensionManager.ExtensionManager;
        }
        public TestDiagnosticAnalyzerDriver(
            Project project,
            DiagnosticAnalyzer workspaceAnalyzerOpt = null,
            Action <Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = null,
            bool includeSuppressedDiagnostics = false)
        {
            _exceptionDiagnosticsSource = new TestHostDiagnosticUpdateSource(project.Solution.Workspace);

            _diagnosticAnalyzerService = CreateDiagnosticAnalyzerService(project, workspaceAnalyzerOpt, onAnalyzerException);
            _diagnosticAnalyzerService.CreateIncrementalAnalyzer(project.Solution.Workspace);

            _includeSuppressedDiagnostics = includeSuppressedDiagnostics;
        }
Beispiel #3
0
        public TestDiagnosticAnalyzerDriver(Project project, ImmutableArray <DiagnosticAnalyzer> workspaceAnalyzers, Action <Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException = null, bool logAnalyzerExceptionAsDiagnostics = false)
        {
            _workspaceAnalyzers         = workspaceAnalyzers;
            _exceptionDiagnosticsSource = new TestHostDiagnosticUpdateSource(project.Solution.Workspace);
            _diagnosticAnalyzerService  = new TestDiagnosticAnalyzerService(project.Language, workspaceAnalyzers, _exceptionDiagnosticsSource, onAnalyzerException);
            _incrementalAnalyzer        = _diagnosticAnalyzerService.CreateIncrementalAnalyzer(project.Solution.Workspace);

            // If the test is not configured with a custom onAnalyzerException handler AND has not requested exceptions to be handled and logged as diagnostics, then FailFast on exceptions.
            if (onAnalyzerException == null && !logAnalyzerExceptionAsDiagnostics)
            {
                onAnalyzerException = DiagnosticExtensions.FailFastOnAnalyzerException;
            }

            _onAnalyzerException = onAnalyzerException;
        }
Beispiel #4
0
        private static TestWorkspace ServiceSetup(CodeFixProvider codefix, out TestDiagnosticAnalyzerService diagnosticService, out CodeFixService fixService, out IErrorLoggerService errorLogger)
        {
            diagnosticService = new TestDiagnosticAnalyzerService(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap());
            var fixers = SpecializedCollections.SingletonEnumerable(
                new Lazy <CodeFixProvider, CodeChangeProviderMetadata>(
                    () => codefix,
                    new CodeChangeProviderMetadata("Test", languages: LanguageNames.CSharp)));
            var code      = @"class Program { }";
            var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(code);
            var logger    = SpecializedCollections.SingletonEnumerable(new Lazy <IErrorLoggerService>(() => new TestErrorLogger()));

            errorLogger = logger.First().Value;
            fixService  = new CodeFixService(
                diagnosticService, logger, fixers, SpecializedCollections.EmptyEnumerable <Lazy <ISuppressionFixProvider, CodeChangeProviderMetadata> >());
            return(workspace);
        }
        private static Tuple <TestWorkspace, TestDiagnosticAnalyzerService, CodeFixService, IErrorLoggerService> ServiceSetup(CodeFixProvider codefix)
        {
            var diagnosticService = new TestDiagnosticAnalyzerService(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap());
            var fixers            = SpecializedCollections.SingletonEnumerable(
                new Lazy <CodeFixProvider, CodeChangeProviderMetadata>(
                    () => codefix,
                    new CodeChangeProviderMetadata("Test", languages: LanguageNames.CSharp)));
            var code        = @"class Program { }";
            var workspace   = TestWorkspace.CreateCSharp(code);
            var logger      = SpecializedCollections.SingletonEnumerable(new Lazy <IErrorLoggerService>(() => new TestErrorLogger()));
            var errorLogger = logger.First().Value;
            var fixService  = new CodeFixService(
                workspace.ExportProvider.GetExportedValue <IThreadingContext>(),
                diagnosticService, logger, fixers, SpecializedCollections.EmptyEnumerable <Lazy <IConfigurationFixProvider, CodeChangeProviderMetadata> >());

            return(Tuple.Create(workspace, diagnosticService, fixService, errorLogger));
        }
        private RemoteHostClientServiceFactory.RemoteHostClientService CreateRemoteHostClientService(
            Workspace workspace = null,
            IEnumerable <AnalyzerReference> hostAnalyzerReferences  = null,
            IAsynchronousOperationListenerProvider listenerProvider = null)
        {
            workspace ??= new AdhocWorkspace(TestHostServices.CreateHostServices());
            workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options
                                                                            .WithChangedOption(RemoteHostOptions.RemoteHostTest, true)
                                                                            .WithChangedOption(SolutionCrawlerOptions.BackgroundAnalysisScopeOption, LanguageNames.CSharp, BackgroundAnalysisScope.FullSolution)
                                                                            .WithChangedOption(SolutionCrawlerOptions.BackgroundAnalysisScopeOption, LanguageNames.VisualBasic, BackgroundAnalysisScope.FullSolution)));

            var analyzerService  = new TestDiagnosticAnalyzerService(hostAnalyzerReferences.AsImmutableOrEmpty());
            var threadingContext = ((IMefHostExportProvider)workspace.Services.HostServices).GetExports <IThreadingContext>().Single().Value;
            var factory          = new RemoteHostClientServiceFactory(threadingContext, listenerProvider ?? AsynchronousOperationListenerProvider.NullProvider, analyzerService);

            return(factory.CreateService(workspace.Services) as RemoteHostClientServiceFactory.RemoteHostClientService);
        }
Beispiel #7
0
        private static (TestWorkspace workspace, TestDiagnosticAnalyzerService analyzerService, CodeFixService codeFixService, IErrorLoggerService errorLogger) ServiceSetup(CodeFixProvider codefix)
        {
            var fixers = SpecializedCollections.SingletonEnumerable(
                new Lazy <CodeFixProvider, CodeChangeProviderMetadata>(
                    () => codefix,
                    new CodeChangeProviderMetadata("Test", languages: LanguageNames.CSharp)));

            var code = @"class Program { }";

            var workspace         = TestWorkspace.CreateCSharp(code, openDocuments: true);
            var analyzerReference = new TestAnalyzerReferenceByLanguage(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap());

            workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference }));

            var diagnosticService = new TestDiagnosticAnalyzerService();
            var logger            = SpecializedCollections.SingletonEnumerable(new Lazy <IErrorLoggerService>(() => new TestErrorLogger()));
            var errorLogger       = logger.First().Value;
            var fixService        = new CodeFixService(
                workspace.ExportProvider.GetExportedValue <IThreadingContext>(),
                diagnosticService, logger, fixers, SpecializedCollections.EmptyEnumerable <Lazy <IConfigurationFixProvider, CodeChangeProviderMetadata> >());

            return(workspace, diagnosticService, fixService, errorLogger);
        }
Beispiel #8
0
        public async Task TestGetFirstDiagnosticWithFixAsync()
        {
            var diagnosticService = new TestDiagnosticAnalyzerService();

            var fixers = CreateFixers();
            var code   = @"
    a
";

            using var workspace = TestWorkspace.CreateCSharp(code, openDocuments: true);

            var analyzerReference = new TestAnalyzerReferenceByLanguage(DiagnosticExtensions.GetCompilerDiagnosticAnalyzersMap());

            workspace.TryApplyChanges(workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference }));

            var logger     = SpecializedCollections.SingletonEnumerable(new Lazy <IErrorLoggerService>(() => workspace.Services.GetRequiredService <IErrorLoggerService>()));
            var fixService = new CodeFixService(
                workspace.ExportProvider.GetExportedValue <IThreadingContext>(),
                diagnosticService, logger, fixers, SpecializedCollections.EmptyEnumerable <Lazy <IConfigurationFixProvider, CodeChangeProviderMetadata> >());

            var incrementalAnalyzer = (IIncrementalAnalyzerProvider)diagnosticService;

            // register diagnostic engine to solution crawler
            var analyzer = incrementalAnalyzer.CreateIncrementalAnalyzer(workspace);

            var reference = new MockAnalyzerReference();
            var project   = workspace.CurrentSolution.Projects.Single().AddAnalyzerReference(reference);
            var document  = project.Documents.Single();
            var unused    = await fixService.GetMostSevereFixableDiagnosticAsync(document, TextSpan.FromBounds(0, 0), cancellationToken : CancellationToken.None);

            var fixer1 = (MockFixer)fixers.Single().Value;
            var fixer2 = (MockFixer)reference.Fixer !;

            // check to make sure both of them are called.
            Assert.True(fixer1.Called);
            Assert.True(fixer2.Called);
        }
        private static IEnumerable <Diagnostic> GetDiagnostics(DiagnosticAnalyzer workspaceAnalyzerOpt, Document document, TextSpan span, Project project, bool getDocumentDiagnostics, bool getProjectDiagnostics, Action <Exception, DiagnosticAnalyzer, Diagnostic> onAnalyzerException, bool logAnalyzerExceptionAsDiagnostics)
        {
            var documentDiagnostics = SpecializedCollections.EmptyEnumerable <Diagnostic>();
            var projectDiagnostics  = SpecializedCollections.EmptyEnumerable <Diagnostic>();

            // If no user diagnostic analyzer, then test compiler diagnostics.
            var workspaceAnalyzer = workspaceAnalyzerOpt ?? DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(project.Language);

            // If the test is not configured with a custom onAnalyzerException handler AND has not requested exceptions to be handled and logged as diagnostics, then FailFast on exceptions.
            if (onAnalyzerException == null && !logAnalyzerExceptionAsDiagnostics)
            {
                onAnalyzerException = DiagnosticExtensions.FailFastOnAnalyzerException;
            }

            var exceptionDiagnosticsSource = new TestHostDiagnosticUpdateSource(project.Solution.Workspace);
            var analyzerService            = new TestDiagnosticAnalyzerService(project.Language, workspaceAnalyzer, exceptionDiagnosticsSource, onAnalyzerException);
            var incrementalAnalyzer        = analyzerService.CreateIncrementalAnalyzer(project.Solution.Workspace);

            if (getDocumentDiagnostics)
            {
                var tree = document.GetSyntaxTreeAsync().Result;
                var root = tree.GetRoot();
                var dxs  = analyzerService.GetDiagnosticsAsync(project.Solution, project.Id, document.Id).WaitAndGetResult(CancellationToken.None);
                documentDiagnostics = dxs.Where(d => d.HasTextSpan && d.TextSpan.IntersectsWith(span)).Select(d => d.ToDiagnostic(tree));
            }

            if (getProjectDiagnostics)
            {
                var dxs = analyzerService.GetDiagnosticsAsync(project.Solution, project.Id).WaitAndGetResult(CancellationToken.None);
                projectDiagnostics = dxs.Where(d => !d.HasTextSpan).Select(d => d.ToDiagnostic(tree: null));
            }

            var exceptionDiagnostics = exceptionDiagnosticsSource.TestOnly_GetReportedDiagnostics(workspaceAnalyzer).Select(d => d.ToDiagnostic(tree: null));

            return(documentDiagnostics.Concat(projectDiagnostics).Concat(exceptionDiagnostics));
        }
Beispiel #10
0
        private void AccessSupportedDiagnostics(DiagnosticAnalyzer analyzer)
        {
            var diagnosticService = new TestDiagnosticAnalyzerService(LanguageNames.CSharp, analyzer);

            diagnosticService.GetDiagnosticDescriptors(projectOpt: null);
        }
Beispiel #11
0
        private void AccessSupportedDiagnostics(DiagnosticAnalyzer analyzer)
        {
            var diagnosticService = new TestDiagnosticAnalyzerService(LanguageNames.CSharp, analyzer);

            diagnosticService.GetDiagnosticDescriptorsPerReference();
        }
 public TestDiagnosticAnalyzerDriver(Project project, bool includeSuppressedDiagnostics = false)
 {
     _diagnosticAnalyzerService = new TestDiagnosticAnalyzerService();
     _diagnosticAnalyzerService.CreateIncrementalAnalyzer(project.Solution.Workspace);
     _includeSuppressedDiagnostics = includeSuppressedDiagnostics;
 }