private void AddAnalyzerConfigDocumentWithOptions(TestWorkspace workspace, TestParametersOptions options)
        {
            Debug.Assert(options != null);
            var analyzerConfigText = GenerateAnalyzerConfigText(options);

            var newSolution = workspace.CurrentSolution;

            foreach (var project in workspace.Projects)
            {
                Assert.True(PathUtilities.IsAbsolute(project.FilePath));
                var projectRootFilePath = PathUtilities.GetPathRoot(project.FilePath);
                var documentId          = DocumentId.CreateNewId(project.Id);
                newSolution = newSolution.AddAnalyzerConfigDocument(
                    documentId,
                    ".editorconfig",
                    SourceText.From(analyzerConfigText),
                    filePath: Path.Combine(projectRootFilePath, ".editorconfig"));
            }

            var applied = workspace.TryApplyChanges(newSolution);

            Assert.True(applied);
            return;

            string GenerateAnalyzerConfigText(TestParametersOptions options)
            {
                var textBuilder = new StringBuilder();

                // Add an auto-generated header at the top so we can skip this file in expected baseline validation.
                textBuilder.AppendLine(AutoGeneratedAnalyzerConfigHeader);
                textBuilder.AppendLine();
                textBuilder.AppendLine(options.GetEditorConfigText());
                return(textBuilder.ToString());
            }
        }
Exemple #2
0
        public void GlobalSetup()
        {
            _useExportProviderAttribute.Before(null);

            if (_workspace != null)
            {
                throw new InvalidOperationException();
            }

            _workspace = TestWorkspace.Create(
                @"<Workspace>
    <Project Language=""NoCompilation"" CommonReferences=""false"">
        <Document>
            // a no-compilation document
        </Document>
    </Project>
</Workspace>"
                );

            // Explicitly choose the sqlite db to test.
            _workspace.TryApplyChanges(
                _workspace.CurrentSolution.WithOptions(
                    _workspace.Options.WithChangedOption(
                        StorageOptions.Database,
                        StorageDatabase.SQLite
                        )
                    )
                );

            var connectionPoolService =
                _workspace.ExportProvider.GetExportedValue <SQLiteConnectionPoolService>();

            _storageService = new SQLitePersistentStorageService(
                connectionPoolService,
                new LocationService()
                );

            var solution = _workspace.CurrentSolution;

            _storage = _storageService
                       .GetStorageWorkerAsync(
                _workspace,
                SolutionKey.ToSolutionKey(solution),
                solution,
                CancellationToken.None
                )
                       .AsTask()
                       .GetAwaiter()
                       .GetResult();
            if (_storage == NoOpPersistentStorage.Instance)
            {
                throw new InvalidOperationException(
                          "We didn't properly get the sqlite storage instance."
                          );
            }

            Console.WriteLine("Storage type: " + _storage.GetType());
            _document = _workspace.CurrentSolution.Projects.Single().Documents.Single();
            _random   = new Random(0);
        }
        internal Holder CreateSession(TestWorkspace workspace, char opening, char closing, Dictionary <OptionKey2, object> changedOptionSet = null)
        {
            var threadingContext = workspace.ExportProvider.GetExportedValue <IThreadingContext>();
            var undoManager      = workspace.ExportProvider.GetExportedValue <ITextBufferUndoManagerProvider>();
            var editorOperationsFactoryService = workspace.ExportProvider.GetExportedValue <IEditorOperationsFactoryService>();

            if (changedOptionSet != null)
            {
                var options = workspace.Options;
                foreach (var entry in changedOptionSet)
                {
                    options = options.WithChangedOption(entry.Key, entry.Value);
                }

                workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(options));
            }

            var document = workspace.Documents.First();

            var provider     = new BraceCompletionSessionProvider(threadingContext, undoManager, editorOperationsFactoryService);
            var openingPoint = new SnapshotPoint(document.GetTextBuffer().CurrentSnapshot, document.CursorPosition.Value);

            if (provider.TryCreateSession(document.GetTextView(), openingPoint, opening, closing, out var session))
            {
                return(new Holder(workspace, session));
            }

            workspace.Dispose();
            return(null);
        }
Exemple #4
0
        internal static Holder CreateSession(TestWorkspace workspace, char opening, char closing, Dictionary <OptionKey2, object> changedOptionSet = null)
        {
            if (changedOptionSet != null)
            {
                var options = workspace.Options;
                foreach (var entry in changedOptionSet)
                {
                    options = options.WithChangedOption(entry.Key, entry.Value);
                }

                workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(options));
            }

            var document = workspace.Documents.First();

            var provider     = Assert.IsType <BraceCompletionSessionProvider>(workspace.ExportProvider.GetExportedValue <IBraceCompletionSessionProvider>());
            var openingPoint = new SnapshotPoint(document.GetTextBuffer().CurrentSnapshot, document.CursorPosition.Value);

            if (provider.TryCreateSession(document.GetTextView(), openingPoint, opening, closing, out var session))
            {
                return(new Holder(workspace, session));
            }

            workspace.Dispose();
            return(null);
        }
Exemple #5
0
        private void AddAnalyzersToWorkspace(TestWorkspace workspace)
        {
            var analyzerReference = new AnalyzerImageReference(
                OtherAnalyzers.Add(SuppressionAnalyzer)
                );

            workspace.TryApplyChanges(
                workspace.CurrentSolution.WithAnalyzerReferences(new[] { analyzerReference })
                );
        }
Exemple #6
0
        public DiagnosticTaggerWrapper(
            TestWorkspace workspace,
            IReadOnlyDictionary <string, ImmutableArray <DiagnosticAnalyzer> >?analyzerMap = null,
            IDiagnosticUpdateSource?updateSource = null,
            bool createTaggerProvider            = true
            )
        {
            _threadingContext = workspace.GetService <IThreadingContext>();
            _listenerProvider = workspace.GetService <IAsynchronousOperationListenerProvider>();

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

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

            _workspace = workspace;

            _registrationService =
                (SolutionCrawlerRegistrationService)workspace.Services.GetRequiredService <ISolutionCrawlerRegistrationService>();
            _registrationService.Register(workspace);

            if (
                !_registrationService
                .GetTestAccessor()
                .TryGetWorkCoordinator(workspace, out var coordinator)
                )
            {
                throw new InvalidOperationException();
            }

            AnalyzerService = (DiagnosticAnalyzerService?)_registrationService
                              .GetTestAccessor()
                              .AnalyzerProviders.SelectMany(pair => pair.Value)
                              .SingleOrDefault(
                lazyProvider =>
                lazyProvider.Metadata.Name == WellKnownSolutionCrawlerAnalyzers.Diagnostic &&
                lazyProvider.Metadata.HighPriorityForActiveFile
                )
                              ?.Value;
            DiagnosticService =
                (DiagnosticService)workspace.ExportProvider.GetExportedValue <IDiagnosticService>();

            if (updateSource is object)
            {
                DiagnosticService.Register(updateSource);
            }

            if (createTaggerProvider)
            {
                _ = TaggerProvider;
            }
        }
Exemple #7
0
        private static void MakeProjectsAndDocumentsRooted(TestWorkspace workspace)
        {
            const string defaultRootFilePath = @"z:\";
            var          newSolution         = workspace.CurrentSolution;

            foreach (var projectId in workspace.CurrentSolution.ProjectIds)
            {
                var project = newSolution.GetProject(projectId);

                string projectRootFilePath;
                if (!PathUtilities.IsAbsolute(project.FilePath))
                {
                    projectRootFilePath = defaultRootFilePath;
                    newSolution         = newSolution.WithProjectFilePath(
                        projectId,
                        Path.Combine(projectRootFilePath, project.FilePath)
                        );
                }
                else
                {
                    projectRootFilePath = PathUtilities.GetPathRoot(project.FilePath);
                }

                foreach (var documentId in project.DocumentIds)
                {
                    var document = newSolution.GetDocument(documentId);
                    if (!PathUtilities.IsAbsolute(document.FilePath))
                    {
                        newSolution = newSolution.WithDocumentFilePath(
                            documentId,
                            Path.Combine(projectRootFilePath, document.FilePath)
                            );
                    }
                    else
                    {
                        Assert.Equal(
                            projectRootFilePath,
                            PathUtilities.GetPathRoot(document.FilePath)
                            );
                    }
                }
            }

            var applied = workspace.TryApplyChanges(newSolution);

            Assert.True(applied);
            return;
        }
Exemple #8
0
        public DiagnosticTaggerWrapper(
            TestWorkspace workspace,
            IReadOnlyDictionary <string, ImmutableArray <DiagnosticAnalyzer> >?analyzerMap = null,
            IDiagnosticUpdateSource?updateSource = null,
            bool createTaggerProvider            = true)
        {
            _threadingContext = workspace.GetService <IThreadingContext>();
            _listenerProvider = workspace.GetService <IAsynchronousOperationListenerProvider>();

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

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

            // Change the background analysis scope to OpenFiles instead of ActiveFile (default),
            // so that every diagnostic tagger test does not need to mark test files as "active" file.
            var csKey = new OptionKey2(SolutionCrawlerOptions.BackgroundAnalysisScopeOption, LanguageNames.CSharp);
            var vbKey = new OptionKey2(SolutionCrawlerOptions.BackgroundAnalysisScopeOption, LanguageNames.VisualBasic);

            workspace.SetOptions(workspace.Options
                                 .WithChangedOption(csKey, BackgroundAnalysisScope.OpenFiles)
                                 .WithChangedOption(vbKey, BackgroundAnalysisScope.OpenFiles));

            _workspace = workspace;

            _registrationService = (SolutionCrawlerRegistrationService)workspace.Services.GetRequiredService <ISolutionCrawlerRegistrationService>();
            _registrationService.Register(workspace);

            if (!_registrationService.GetTestAccessor().TryGetWorkCoordinator(workspace, out var coordinator))
            {
                throw new InvalidOperationException();
            }

            AnalyzerService   = (DiagnosticAnalyzerService?)_registrationService.GetTestAccessor().AnalyzerProviders.SelectMany(pair => pair.Value).SingleOrDefault(lazyProvider => lazyProvider.Metadata.Name == WellKnownSolutionCrawlerAnalyzers.Diagnostic && lazyProvider.Metadata.HighPriorityForActiveFile)?.Value;
            DiagnosticService = (DiagnosticService)workspace.ExportProvider.GetExportedValue <IDiagnosticService>();

            if (updateSource is object)
            {
                DiagnosticService.Register(updateSource);
            }

            if (createTaggerProvider)
            {
                _ = TaggerProvider;
            }
        }
Exemple #9
0
 protected override void InitializeWorkspace(
     TestWorkspace workspace,
     TestParameters parameters
     )
 {
     workspace.TryApplyChanges(
         workspace.CurrentSolution.WithOptions(
             workspace.Options
             .WithChangedOption(
                 SymbolSearchOptions.SuggestForTypesInNuGetPackages,
                 LanguageNames.CSharp,
                 true
                 )
             .WithChangedOption(
                 SymbolSearchOptions.SuggestForTypesInReferenceAssemblies,
                 LanguageNames.CSharp,
                 true
                 )
             )
         );
 }
        private static void AddAnalyzerConfigDocumentWithOptions(TestWorkspace workspace, IDictionary <OptionKey, object> options)
        {
            Debug.Assert(options != null);
            var analyzerConfigText = GenerateAnalyzerConfigText(options);

            var newSolution = workspace.CurrentSolution;

            foreach (var project in workspace.Projects)
            {
                Assert.True(PathUtilities.IsAbsolute(project.FilePath));
                var projectRootFilePath = PathUtilities.GetPathRoot(project.FilePath);
                var documentId          = DocumentId.CreateNewId(project.Id);
                newSolution = newSolution.AddAnalyzerConfigDocument(
                    documentId,
                    ".editorconfig",
                    SourceText.From(analyzerConfigText),
                    filePath: Path.Combine(projectRootFilePath, ".editorconfig"));
            }

            var applied = workspace.TryApplyChanges(newSolution);

            Assert.True(applied);
            return;
        public DiagnosticTaggerWrapper(
            TestWorkspace workspace,
            IReadOnlyDictionary <string, ImmutableArray <DiagnosticAnalyzer> >?analyzerMap = null,
            IDiagnosticUpdateSource?updateSource = null,
            bool createTaggerProvider            = true)
        {
            _threadingContext = workspace.GetService <IThreadingContext>();
            _listenerProvider = workspace.GetService <IAsynchronousOperationListenerProvider>();

            if (updateSource == null)
            {
                updateSource = AnalyzerService = new MyDiagnosticAnalyzerService(_listenerProvider.GetListener(FeatureAttribute.DiagnosticService));
            }

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

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

            _workspace = workspace;

            _registrationService = workspace.Services.GetRequiredService <ISolutionCrawlerRegistrationService>();
            _registrationService.Register(workspace);

            DiagnosticService = new DiagnosticService(_listenerProvider, Array.Empty <Lazy <IEventListener, EventListenerMetadata> >());
            DiagnosticService.Register(updateSource);

            if (createTaggerProvider)
            {
                _ = TaggerProvider;
            }

            if (AnalyzerService != null)
            {
                _incrementalAnalyzers   = ImmutableArray.Create(AnalyzerService.CreateIncrementalAnalyzer(workspace));
                _solutionCrawlerService = workspace.Services.GetService <ISolutionCrawlerRegistrationService>() as SolutionCrawlerRegistrationService;
            }
        }
Exemple #12
0
        public void GlobalSetup()
        {
            _useExportProviderAttribute.Before(null);

            if (_workspace != null)
            {
                throw new InvalidOperationException();
            }

            _workspace = TestWorkspace.Create(
                @"<Workspace>
    <Project Language=""NoCompilation"" CommonReferences=""false"">
        <Document>
            // a no-compilation document
        </Document>
    </Project>
</Workspace>");

            // Ensure we always use the storage service, no matter what the size of the solution.
            _workspace.TryApplyChanges(_workspace.CurrentSolution.WithOptions(
                                           _workspace.CurrentSolution.Options.WithChangedOption(StorageOptions.SolutionSizeThreshold, -1)));

            _storageService = new SQLitePersistentStorageService(
                _workspace.Services.GetService <IOptionService>(),
                new LocationService(),
                new SolutionSizeTracker());

            _storage = _storageService.GetStorageWorker(_workspace.CurrentSolution);
            if (_storage == NoOpPersistentStorage.Instance)
            {
                throw new InvalidOperationException("We didn't properly get the sqlite storage instance.");
            }

            Console.WriteLine("Storage type: " + _storage.GetType());
            _document = _workspace.CurrentSolution.Projects.Single().Documents.Single();
            _random   = new Random(0);
        }