Exemplo n.º 1
0
        internal ProjectSnapshotManagerBase CreateProjectSnapshotManager(bool allowNotifyListeners = false)
        {
            var snapshotManager = TestProjectSnapshotManager.Create(LegacyDispatcher);

            snapshotManager.AllowNotifyListeners = allowNotifyListeners;

            return(snapshotManager);
        }
Exemplo n.º 2
0
 public DefaultWorkspaceSemanticTokensRefreshTriggerTest()
 {
     ProjectManager = TestProjectSnapshotManager.Create(LegacyDispatcher);
     ProjectManager.AllowNotifyListeners = true;
     HostProject = new HostProject("/path/to/project.csproj", RazorConfiguration.Default, "TestRootNamespace");
     ProjectManager.ProjectAdded(HostProject);
     HostDocument = new HostDocument("/path/to/file.razor", "file.razor");
     ProjectManager.DocumentAdded(HostProject, HostDocument, new EmptyTextLoader(HostDocument.FilePath));
 }
Exemplo n.º 3
0
        public DefaultGeneratedCodeContainerStoreTest()
        {
            var documentVersionCache = Mock.Of <DocumentVersionCache>();
            var csharpPublisher      = Mock.Of <CSharpPublisher>();

            Store          = new DefaultGeneratedCodeContainerStore(Dispatcher, documentVersionCache, csharpPublisher);
            ProjectManager = TestProjectSnapshotManager.Create(Dispatcher);
            Store.Initialize(ProjectManager);
        }
Exemplo n.º 4
0
        public DefaultGeneratedCodeContainerStoreTest()
        {
            var documentVersionCache = Mock.Of <DocumentVersionCache>();
            var server = new Lazy <ILanguageServer>(() => null);

            Store          = new DefaultGeneratedCodeContainerStore(Dispatcher, documentVersionCache, server);
            ProjectManager = TestProjectSnapshotManager.Create(Dispatcher);
            Store.Initialize(ProjectManager);
        }
 public DefaultGeneratedDocumentPublisherTest()
 {
     Server         = new TestServer();
     ProjectManager = TestProjectSnapshotManager.Create(Dispatcher);
     ProjectManager.AllowNotifyListeners = true;
     HostProject = new HostProject("/path/to/project.csproj", RazorConfiguration.Default, "TestRootNamespace");
     ProjectManager.ProjectAdded(HostProject);
     HostDocument = new HostDocument("/path/to/file.razor", "file.razor");
     ProjectManager.DocumentAdded(HostProject, HostDocument, new EmptyTextLoader(HostDocument.FilePath));
 }
        public DocumentOutputReferenceCapturerTest()
        {
            ProjectManager = TestProjectSnapshotManager.Create(Dispatcher);
            ProjectManager.AllowNotifyListeners = true;

            HostProject = new HostProject("C:/path/to/project.csproj", RazorConfiguration.Default, "TestNamespace");
            ProjectManager.ProjectAdded(HostProject);
            HostDocument = new HostDocument("C:/path/to/file.razor", "file.razor");
            ProjectManager.DocumentAdded(HostProject, HostDocument, new EmptyTextLoader(HostDocument.FilePath));

            DocumentOutputReferenceCapturer = new DocumentOutputReferenceCapturer();
            DocumentOutputReferenceCapturer.Initialize(ProjectManager);
        }
        public void DocumentAdded_IgnoresClosedDocument()
        {
            // Arrange
            var projectManager = TestProjectSnapshotManager.Create(Dispatcher);

            projectManager.ProjectAdded(HostProject1);
            projectManager.ProjectAdded(HostProject2);
            projectManager.AllowNotifyListeners = true;

            var queue = new TestOpenDocumentGenerator(Dispatcher);

            queue.Initialize(projectManager);

            // Act & Assert
            projectManager.DocumentAdded(HostProject1, Documents[0], null);

            Assert.False(queue.IsScheduledOrRunning, "Queue should not be scheduled when a document is added for the first time (closed by default)");
        }
        public void UpdateProject_UpdatesProjectWorkspaceState()
        {
            // Arrange
            var projectManager = TestProjectSnapshotManager.Create(Dispatcher);
            var hostProject    = new HostProject("/path/to/project.csproj", RazorConfiguration.Default, "TestRootNamespace");

            projectManager.ProjectAdded(hostProject);
            var projectService        = CreateProjectService(Mock.Of <ProjectResolver>(), projectManager);
            var projectWorkspaceState = new ProjectWorkspaceState(Array.Empty <TagHelperDescriptor>(), LanguageVersion.LatestMajor);

            // Act
            projectService.UpdateProject(hostProject.FilePath, hostProject.Configuration, hostProject.RootNamespace, projectWorkspaceState, EmptyDocuments);

            // Assert
            var project = projectManager.GetLoadedProject(hostProject.FilePath);

            Assert.Same(projectWorkspaceState, project.ProjectWorkspaceState);
        }
        public void Enqueue_IgnoresClosedDocuments()
        {
            // Arrange
            var projectManager = TestProjectSnapshotManager.Create(Dispatcher);

            projectManager.ProjectAdded(HostProject1);
            projectManager.DocumentAdded(HostProject1, Documents[0], null);

            var project = projectManager.GetLoadedProject(HostProject1.FilePath);

            var queue = new TestOpenDocumentGenerator(Dispatcher);

            queue.Initialize(projectManager);

            // Act & Assert
            queue.Enqueue(project.GetDocument(Documents[0].FilePath));

            Assert.False(queue.IsScheduledOrRunning, "Queue should not have anything pending");
        }
        public RazorDiagnosticsPublisherTest()
        {
            var testProjectManager = TestProjectSnapshotManager.Create(Dispatcher);
            var hostProject        = new HostProject("/C:/project/project.csproj", RazorConfiguration.Default, "TestRootNamespace");

            testProjectManager.ProjectAdded(hostProject);
            var sourceText         = SourceText.From(string.Empty);
            var textAndVersion     = TextAndVersion.Create(sourceText, VersionStamp.Default);
            var openedHostDocument = new HostDocument("/C:/project/open_document.cshtml", "/C:/project/open_document.cshtml");

            testProjectManager.DocumentAdded(hostProject, openedHostDocument, TextLoader.From(textAndVersion));
            testProjectManager.DocumentOpened(hostProject.FilePath, openedHostDocument.FilePath, sourceText);
            var closedHostDocument = new HostDocument("/C:/project/closed_document.cshtml", "/C:/project/closed_document.cshtml");

            testProjectManager.DocumentAdded(hostProject, closedHostDocument, TextLoader.From(textAndVersion));

            OpenedDocument = testProjectManager.Projects[0].GetDocument(openedHostDocument.FilePath);
            ClosedDocument = testProjectManager.Projects[0].GetDocument(closedHostDocument.FilePath);
            ProjectManager = testProjectManager;
        }
Exemplo n.º 11
0
        public void ProjectSnapshotManager_WorkspacePopulated_DoesNotFireTwice()
        {
            // Arrange
            var responseRouterReturns = new Mock <IResponseRouterReturns>(MockBehavior.Strict);

            responseRouterReturns.Setup(r => r.ReturningVoid(It.IsAny <CancellationToken>()))
            .Returns(() => Task.CompletedTask);

            var clientNotifierService = new Mock <ClientNotifierServiceBase>(MockBehavior.Strict);

            clientNotifierService.Setup(l => l.SendRequestAsync(_razorServerReadyEndpoint))
            .Returns(Task.FromResult(responseRouterReturns.Object));

            var razorServerReadyPublisher = new RazorServerReadyPublisher(Dispatcher, clientNotifierService.Object);

            var projectManager = TestProjectSnapshotManager.Create(Dispatcher);

            projectManager.AllowNotifyListeners = true;

            razorServerReadyPublisher.Initialize(projectManager);
            var document = TestDocumentSnapshot.Create("C:/file.cshtml");

            document.TryGetText(out var text);
            document.TryGetTextVersion(out var textVersion);
            var textAndVersion = TextAndVersion.Create(text, textVersion);

            projectManager.ProjectAdded(document.ProjectInternal.HostProject);

            // Act
            projectManager.ProjectWorkspaceStateChanged(document.ProjectInternal.HostProject.FilePath, CreateProjectWorkspace());

            clientNotifierService.VerifyAll();
            responseRouterReturns.VerifyAll();

            projectManager.ProjectWorkspaceStateChanged(document.ProjectInternal.HostProject.FilePath, CreateProjectWorkspace());

            // Assert
            clientNotifierService.VerifyAll();
            responseRouterReturns.Verify(r => r.ReturningVoid(It.IsAny <CancellationToken>()), Times.Once);
        }
        public async Task Queue_ProcessesNotifications_AndGoesBackToSleep()
        {
            // Arrange
            var projectManager = TestProjectSnapshotManager.Create(Dispatcher);
            await Dispatcher.RunOnDispatcherThreadAsync(() =>
            {
                projectManager.ProjectAdded(HostProject1);
                projectManager.ProjectAdded(HostProject2);
                projectManager.DocumentAdded(HostProject1, Documents[0], null);
                projectManager.DocumentAdded(HostProject1, Documents[1], null);
            }, CancellationToken.None);

            var project = await Dispatcher.RunOnDispatcherThreadAsync(
                () => projectManager.GetLoadedProject(HostProject1.FilePath), CancellationToken.None);

            var queue = new TestBackgroundDocumentGenerator(Dispatcher)
            {
                Delay = TimeSpan.FromMilliseconds(1),
                BlockBackgroundWorkStart      = new ManualResetEventSlim(initialState: false),
                NotifyBackgroundWorkStarting  = new ManualResetEventSlim(initialState: false),
                BlockBackgroundWorkCompleting = new ManualResetEventSlim(initialState: false),
                NotifyBackgroundWorkCompleted = new ManualResetEventSlim(initialState: false),
            };

            // Act & Assert
            await Dispatcher.RunOnDispatcherThreadAsync(
                () => queue.Enqueue(project.GetDocument(Documents[0].FilePath)), CancellationToken.None);

            Assert.True(queue.IsScheduledOrRunning, "Queue should be scheduled during Enqueue");
            Assert.True(queue.HasPendingNotifications, "Queue should have a notification created during Enqueue");

            // Allow the background work to proceed.
            queue.BlockBackgroundWorkStart.Set();
            queue.BlockBackgroundWorkCompleting.Set();

            queue.NotifyBackgroundWorkCompleted.Wait(TimeSpan.FromSeconds(3));

            Assert.False(queue.IsScheduledOrRunning, "Queue should not have restarted");
            Assert.False(queue.HasPendingNotifications, "Queue should have processed all notifications");
        }
        public void ProjectChanged_ProcessesNotifications_AndGoesBackToSleep()
        {
            // Arrange
            var projectManager = TestProjectSnapshotManager.Create(Dispatcher);

            projectManager.ProjectAdded(HostProject1);
            projectManager.ProjectAdded(HostProject2);
            projectManager.DocumentAdded(HostProject1, Documents[0], null);
            projectManager.DocumentOpened(HostProject1.FilePath, Documents[0].FilePath, SourceText.From(string.Empty));
            projectManager.AllowNotifyListeners = true;

            var queue = new TestOpenDocumentGenerator(Dispatcher)
            {
                Delay = TimeSpan.FromMilliseconds(1),
                BlockBackgroundWorkStart      = new ManualResetEventSlim(initialState: false),
                NotifyBackgroundWorkStarting  = new ManualResetEventSlim(initialState: false),
                BlockBackgroundWorkCompleting = new ManualResetEventSlim(initialState: false),
                NotifyBackgroundWorkCompleted = new ManualResetEventSlim(initialState: false),
            };

            queue.Initialize(projectManager);

            var newWorkspaceState = new ProjectWorkspaceState(Array.Empty <TagHelperDescriptor>(), LanguageVersion.CSharp7_3);


            // Act & Assert
            projectManager.ProjectWorkspaceStateChanged(HostProject1.FilePath, newWorkspaceState);

            Assert.True(queue.IsScheduledOrRunning, "Queue should be scheduled during Enqueue");
            Assert.True(queue.HasPendingNotifications, "Queue should have a notification created during Enqueue");

            // Allow the background work to proceed.
            queue.BlockBackgroundWorkStart.Set();
            queue.BlockBackgroundWorkCompleting.Set();

            Assert.True(queue.NotifyBackgroundWorkCompleted.Wait(TimeSpan.FromSeconds(3)));

            Assert.False(queue.IsScheduledOrRunning, "Queue should not have restarted");
            Assert.False(queue.HasPendingNotifications, "Queue should have processed all notifications");
        }
        public void Enqueue_ProcessesNotifications_AndGoesBackToSleep()
        {
            // Arrange
            var projectManager = TestProjectSnapshotManager.Create(Dispatcher);

            projectManager.ProjectAdded(HostProject1);
            projectManager.ProjectAdded(HostProject2);
            projectManager.DocumentAdded(HostProject1, Documents[0], null);
            projectManager.DocumentOpened(HostProject1.FilePath, Documents[0].FilePath, SourceText.From(string.Empty));
            projectManager.DocumentAdded(HostProject1, Documents[1], null);
            projectManager.DocumentOpened(HostProject1.FilePath, Documents[1].FilePath, SourceText.From(string.Empty));

            var project = projectManager.GetLoadedProject(HostProject1.FilePath);

            var queue = new TestOpenDocumentGenerator(Dispatcher)
            {
                Delay = TimeSpan.FromMilliseconds(1),
                BlockBackgroundWorkStart      = new ManualResetEventSlim(initialState: false),
                NotifyBackgroundWorkStarting  = new ManualResetEventSlim(initialState: false),
                BlockBackgroundWorkCompleting = new ManualResetEventSlim(initialState: false),
                NotifyBackgroundWorkCompleted = new ManualResetEventSlim(initialState: false),
            };

            queue.Initialize(projectManager);

            // Act & Assert
            queue.Enqueue(project.GetDocument(Documents[0].FilePath));

            Assert.True(queue.IsScheduledOrRunning, "Queue should be scheduled during Enqueue");
            Assert.True(queue.HasPendingNotifications, "Queue should have a notification created during Enqueue");

            // Allow the background work to proceed.
            queue.BlockBackgroundWorkStart.Set();
            queue.BlockBackgroundWorkCompleting.Set();

            Assert.True(queue.NotifyBackgroundWorkCompleted.Wait(TimeSpan.FromSeconds(3)), "Timed out waiting for background work to complete");

            Assert.False(queue.IsScheduledOrRunning, "Queue should not have restarted");
            Assert.False(queue.HasPendingNotifications, "Queue should have processed all notifications");
        }
        public void UpdateProject_DoesNotRemoveOrAddDocuments()
        {
            // Arrange
            var projectManager = TestProjectSnapshotManager.Create(Dispatcher);
            var hostProject    = new HostProject("/path/to/project.csproj", RazorConfiguration.Default, "TestRootNamespace");

            projectManager.ProjectAdded(hostProject);
            var hostDocument = new HostDocument("/path/to/file.cshtml", "file.cshtml", FileKinds.Legacy);

            projectManager.DocumentAdded(hostProject, hostDocument, Mock.Of <TextLoader>());
            var projectService  = CreateProjectService(Mock.Of <ProjectResolver>(), projectManager);
            var unknownDocument = new DocumentSnapshotHandle("/path/to/other/file.cshtml", "file.cshtml", FileKinds.Legacy);

            // Act
            projectService.UpdateProject(hostProject.FilePath, hostProject.Configuration, hostProject.RootNamespace, ProjectWorkspaceState.Default, new[] { unknownDocument });

            // Assert
            var project          = projectManager.GetLoadedProject(hostProject.FilePath);
            var documentFilePath = Assert.Single(project.DocumentFilePaths);

            Assert.Equal(hostDocument.FilePath, documentFilePath);
        }
        public void UpdateProject_UpdatingDocument_MapsRelativeFilePathToActualDocument()
        {
            // Arrange
            var projectManager = TestProjectSnapshotManager.Create(Dispatcher);
            var hostProject    = new HostProject("/path/to/project.csproj", RazorConfiguration.Default, "TestRootNamespace");

            projectManager.ProjectAdded(hostProject);
            var hostDocument = new HostDocument("/path/to/file.cshtml", "file.cshtml", FileKinds.Legacy);

            projectManager.DocumentAdded(hostProject, hostDocument, Mock.Of <TextLoader>());
            var projectService = CreateProjectService(Mock.Of <ProjectResolver>(), projectManager);
            var newDocument    = new DocumentSnapshotHandle("file.cshtml", "file.cshtml", FileKinds.Component);

            // Act
            projectService.UpdateProject(hostProject.FilePath, hostProject.Configuration, hostProject.RootNamespace, ProjectWorkspaceState.Default, new[] { newDocument });

            // Assert
            var project  = projectManager.GetLoadedProject(hostProject.FilePath);
            var document = project.GetDocument(hostDocument.FilePath);

            Assert.NotNull(document);
            Assert.Equal(FileKinds.Component, document.FileKind);
        }
        public void Enqueue_ProcessesNotifications_AndRestarts()
        {
            // Arrange
            var projectManager = TestProjectSnapshotManager.Create(Dispatcher);

            projectManager.ProjectAdded(HostProject1);
            projectManager.ProjectAdded(HostProject2);
            projectManager.DocumentAdded(HostProject1, Documents[0], null);
            projectManager.DocumentOpened(HostProject1.FilePath, Documents[0].FilePath, SourceText.From(string.Empty));
            projectManager.DocumentAdded(HostProject1, Documents[1], null);
            projectManager.DocumentOpened(HostProject1.FilePath, Documents[1].FilePath, SourceText.From(string.Empty));

            var project = projectManager.GetLoadedProject(HostProject1.FilePath);

            var queue = new TestOpenDocumentGenerator(Dispatcher)
            {
                Delay = TimeSpan.FromMilliseconds(1),
                BlockBackgroundWorkStart         = new ManualResetEventSlim(initialState: false),
                NotifyBackgroundWorkStarting     = new ManualResetEventSlim(initialState: false),
                NotifyBackgroundCapturedWorkload = new ManualResetEventSlim(initialState: false),
                BlockBackgroundWorkCompleting    = new ManualResetEventSlim(initialState: false),
                NotifyBackgroundWorkCompleted    = new ManualResetEventSlim(initialState: false),
            };

            queue.Initialize(projectManager);

            // Act & Assert
            queue.Enqueue(project.GetDocument(Documents[0].FilePath));

            Assert.True(queue.IsScheduledOrRunning, "Queue should be scheduled during Enqueue");
            Assert.True(queue.HasPendingNotifications, "Queue should have a notification created during Enqueue");

            // Allow the background work to start.
            queue.BlockBackgroundWorkStart.Set();

            Assert.True(queue.NotifyBackgroundWorkStarting.Wait(TimeSpan.FromSeconds(1)), "Timed out waiting for background work to start");
            Assert.True(queue.IsScheduledOrRunning, "Worker should be processing now");

            Assert.True(queue.NotifyBackgroundCapturedWorkload.Wait(TimeSpan.FromSeconds(1)), "Timed out waiting for background work to be captured");
            Assert.False(queue.HasPendingNotifications, "Worker should have taken all notifications");

            queue.Enqueue(project.GetDocument(Documents[1].FilePath));
            Assert.True(queue.HasPendingNotifications); // Now we should see the worker restart when it finishes.

            // Allow work to complete, which should restart the timer.
            queue.BlockBackgroundWorkCompleting.Set();

            Assert.True(queue.NotifyBackgroundWorkCompleted.Wait(TimeSpan.FromSeconds(3)), "Timed out waiting for background work to complete");
            queue.NotifyBackgroundWorkCompleted.Reset();

            // It should start running again right away.
            Assert.True(queue.IsScheduledOrRunning, "Queue should be scheduled during Enqueue");
            Assert.True(queue.HasPendingNotifications, "Queue should have a notification created during Enqueue");

            // Allow the background work to proceed.
            queue.BlockBackgroundWorkStart.Set();

            queue.BlockBackgroundWorkCompleting.Set();
            Assert.True(queue.NotifyBackgroundWorkCompleted.Wait(TimeSpan.FromSeconds(3)), "Timed out waiting for background work to complete again");

            Assert.False(queue.IsScheduledOrRunning, "Queue should not have restarted");
            Assert.False(queue.HasPendingNotifications, "Queue should have processed all notifications");
        }