public async Task MsBuildModelWatcher_Dispose_CallsUnsubscribe()
        {
            var             threadingService = IProjectThreadingServiceFactory.Create();
            var             fileSystem       = new IFileSystemMock();
            HandlerCallback subscription     = null;
            HandlerCallback unsubscription   = null;
            var             xml             = @"<Project></Project>";
            var             file            = @"C:\Test\Test.proj";
            var             msbuildAccessor = IMsBuildAccessorFactory.ImplementGetProjectXmlAndXmlChangedEvents(xml, newSub => subscription = newSub,
                                                                                                                sub => unsubscription = sub);
            var project = IUnconfiguredProjectFactory.Create();

            var watcher = new MsBuildModelWatcher(threadingService, fileSystem, msbuildAccessor, project);
            await watcher.InitializeAsync(file);

            Assert.NotNull(subscription);
            watcher.XmlHandler(xml);
            Assert.True(fileSystem.FileExists(file));
            Assert.Equal(xml, fileSystem.ReadAllText(file));

            watcher.Dispose();
            Assert.NotNull(unsubscription);
            Assert.Equal(subscription, unsubscription);

            // Ensure multiple calls to dispose don't cause multiple calls to unsubscribe
            watcher.Dispose();
            Mock.Get(msbuildAccessor).Verify(m => m.UnsubscribeProjectXmlChangedEventAsync(project, unsubscription), Times.Once);
        }
Пример #2
0
        private EditProjectFileCommand CreateInstance(
            UnconfiguredProject unconfiguredProject = null,
            bool implementCapabilities       = true,
            IMsBuildAccessor msbuildAccessor = null,
            IFileSystem fileSystem           = null,
            ITextDocumentFactoryService textDocumentService      = null,
            IVsEditorAdaptersFactoryService editorAdapterService = null,
            IProjectThreadingService threadingService            = null,
            IVsShellUtilitiesHelper shellUtilities = null
            )
        {
            UnitTestHelper.IsRunningUnitTests = true;
            var uProj        = unconfiguredProject ?? IUnconfiguredProjectFactory.Create();
            var capabilities = IProjectCapabilitiesServiceFactory.ImplementsContains(CapabilityChecker(implementCapabilities));
            var msbuild      = msbuildAccessor ?? IMsBuildAccessorFactory.Create();
            var fs           = fileSystem ?? new IFileSystemMock();
            var tds          = textDocumentService ?? ITextDocumentFactoryServiceFactory.Create();
            var eas          = editorAdapterService ?? IVsEditorAdaptersFactoryServiceFactory.Create();
            var threadServ   = threadingService ?? IProjectThreadingServiceFactory.Create();
            var shellUt      = shellUtilities ?? new TestShellUtilitiesHelper(
                (sp, path) => Tuple.Create(IVsHierarchyFactory.Create(), (uint)1, IVsPersistDocDataFactory.Create(), (uint)1),
                (sp, path, edType, logView) => IVsWindowFrameFactory.Create());

            return(new EditProjectFileCommand(uProj, capabilities, IServiceProviderFactory.Create(), msbuild, fs, tds, eas, threadServ, shellUt));
        }
Пример #3
0
        private EditProjectFileCommand SetupScenario(string projectXml, string tempPath, string tempProjectFile, string projectFile, string caption,
                                                     IFileSystemMock fileSystem, ITextDocument textDoc, IVsWindowFrame frame)
        {
            fileSystem.SetTempFile(tempPath);
            var configuredProject   = ConfiguredProjectFactory.Create();
            var unconfiguredProject = IUnconfiguredProjectFactory.Create(filePath: projectFile, configuredProject: configuredProject);
            var shellUtilities      = new TestShellUtilitiesHelper((sp, path) =>
            {
                Assert.Equal(tempProjectFile, path);
                return(Tuple.Create(IVsHierarchyFactory.Create(), (uint)0, IVsPersistDocDataFactory.ImplementAsIVsTextBuffer(), (uint)0));
            }, (sp, path, factoryGuid, logicalView) =>
            {
                Assert.Equal(tempProjectFile, path);
                Assert.Equal(XmlGuid, factoryGuid);
                Assert.Equal(Guid.Empty, logicalView);

                return(frame);
            });

            var textBuffer           = ITextBufferFactory.ImplementSnapshot(projectXml);
            var editorFactoryService = IVsEditorAdaptersFactoryServiceFactory.ImplementGetDocumentBuffer(textBuffer);

            Mock.Get(textDoc).SetupGet(t => t.TextBuffer).Returns(textBuffer);
            var textDocFactory = ITextDocumentFactoryServiceFactory.ImplementGetTextDocument(textDoc, true);

            var msbuildAccessor = IMsBuildAccessorFactory.Implement(projectXml, async(writeLock, callback) =>
            {
                await callback();
                Assert.True(writeLock);
            });

            var threadingService = IProjectThreadingServiceFactory.Create();

            return(CreateInstance(unconfiguredProject, true, msbuildAccessor, fileSystem, textDocFactory, editorFactoryService, threadingService, shellUtilities));
        }
        public async Task MsBuildModelWatcher_InitializeAsync_SetsUpSubscription()
        {
            var             threadingService = IProjectThreadingServiceFactory.Create();
            var             fileSystem       = new IFileSystemMock();
            HandlerCallback subscription     = null;
            var             msbuildAccessor  = IMsBuildAccessorFactory.ImplementGetProjectXmlAndXmlChangedEvents("", newSub => subscription = newSub,
                                                                                                                 sub => Assert.False(true, "Should not have called Unsubscribe in this test, as dispose isn't called."));
            var project = IUnconfiguredProjectFactory.Create();

            var watcher = new MsBuildModelWatcher(threadingService, fileSystem, msbuildAccessor, project);

            await watcher.InitializeAsync(@"C:\Test\Test.proj");

            Assert.NotNull(subscription);
            Assert.Equal(watcher.ProjectXmlHandler, subscription);
            Mock.Get(msbuildAccessor).Verify(m => m.SubscribeProjectXmlChangedEventAsync(project, subscription), Times.Once);
            Assert.Equal(0, fileSystem.Files.Count);
        }
        public async Task MsBuildModelWatcher_HandlerCalled_WritesNewXmlToDisk()
        {
            var             threadingService = IProjectThreadingServiceFactory.Create();
            var             fileSystem       = new IFileSystemMock();
            HandlerCallback subscription     = null;
            var             xml             = @"<Project></Project>";
            var             file            = @"C:\Test\Test.proj";
            var             msbuildAccessor = IMsBuildAccessorFactory.ImplementGetProjectXmlAndXmlChangedEvents(xml, newSub => subscription = newSub,
                                                                                                                sub => Assert.False(true, "Should not have called Unsubscribe in this test, as dispose isn't called."));
            var project = IUnconfiguredProjectFactory.Create();

            var watcher = new MsBuildModelWatcher(threadingService, fileSystem, msbuildAccessor, project);
            await watcher.InitializeAsync(file);

            Assert.NotNull(subscription);
            watcher.XmlHandler(xml);
            Assert.True(fileSystem.FileExists(file));
            Assert.Equal(xml, fileSystem.ReadAllText(file));
        }