public async Task Dispose_RemovesTempFile()
        {
            var projectFilePath = @"C:\ConsoleApp\ConsoleApp1\ConsoleApp1.csproj";
            // Use some file encoding that's not the default
            var encoding            = Encoding.Default.Equals(Encoding.UTF8) ? Encoding.UTF32 : Encoding.UTF8;
            var unconfiguredProject = UnconfiguredProjectFactory.Create(filePath: projectFilePath, projectEncoding: encoding);

            var xml = "<Project />";

            var msbuildAccessor = IProjectXmlAccessorFactory.ImplementGetProjectXml(xml);

            var docData      = IVsPersistDocDataFactory.ImplementAsIVsTextBufferGetStateFlags(~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY);
            var textBuffer   = ITextBufferFactory.ImplementSnapshot("<Project></Project>");
            var textDocument = ITextDocumentFactory.ImplementTextBuffer(textBuffer);

            var fileSystem   = new IFileSystemMock();
            var tempFilePath = @"C:\Temp\asdf.1234";

            fileSystem.SetTempFile(tempFilePath);

            var shellUtility         = IVsShellUtilitiesHelperFactory.ImplementGetRDTInfo(Path.Combine(tempFilePath, "ConsoleApp1.csproj"), docData);
            var editorAdapterFactory = IVsEditorAdaptersFactoryServiceFactory.ImplementGetDocumentBuffer(textBuffer);
            var textDocumentService  = ITextDocumentFactoryServiceFactory.ImplementGetTextDocument(textDocument, true);

            var textBufferManager = new TempFileTextBufferManager(unconfiguredProject,
                                                                  msbuildAccessor,
                                                                  editorAdapterFactory,
                                                                  textDocumentService,
                                                                  shellUtility,
                                                                  fileSystem,
                                                                  new IProjectThreadingServiceMock(),
                                                                  IServiceProviderFactory.Create());

            await textBufferManager.InitializeBufferAsync();

            Assert.True(fileSystem.DirectoryExists(tempFilePath));
            Assert.True(fileSystem.FileExists(Path.Combine(tempFilePath, "ConsoleApp1.csproj")));

            await textBufferManager.DisposeAsync();

            Assert.False(fileSystem.DirectoryExists(tempFilePath));
        }
        public async Task AbstractEditProjectFileCommand_CorrectNode_CreatesWindowCorrectly()
        {
            var projectPath   = $"C:\\Project1\\Project1.{EditProjectFileCommand.Extension}";
            var tempDirectory = "C:\\Temp\\asdf.xyz";
            var tempProjFile  = $"{tempDirectory}\\Project1.{EditProjectFileCommand.Extension}";
            var projectXml    = @"<Project></Project>";
            var autoOpenSet   = false;
            var listenerSet   = false;
            IVsWindowFrameNotify2 notifier = null;

            var fileSystem = new IFileSystemMock();
            var textDoc    = ITextDocumentFactory.Create();
            var frame      = IVsWindowFrameFactory.ImplementShowAndSetProperty(VSConstants.S_OK, (property, obj) =>
            {
                switch (property)
                {
                case (int)__VSFPROPID5.VSFPROPID_DontAutoOpen:
                    autoOpenSet = true;
                    break;

                case (int)__VSFPROPID.VSFPROPID_ViewHelper:
                    listenerSet = true;
                    Assert.IsAssignableFrom <IVsWindowFrameNotify2>(obj);
                    notifier = obj as IVsWindowFrameNotify2;
                    break;

                default:
                    Assert.False(true, $"Unexpected property ID {property}");
                    break;
                }

                return(VSConstants.S_OK);
            });

            var modelWatcher        = IMsBuildModelWatcherFactory.CreateInstance();
            var modelWatcherFactory = IExportFactoryFactory.ImplementCreateValue(() => modelWatcher);

            var command = SetupScenario(projectXml, tempDirectory, tempProjFile, projectPath, fileSystem, textDoc, frame, modelWatcherFactory);
            var tree    = ProjectTreeParser.Parse(@"
Root (flags: {ProjectRoot})
");
            var nodes   = ImmutableHashSet.Create(tree);

            // Verify the frame was setup correctly
            Assert.True(await command.TryHandleCommandAsync(nodes, CommandId, true, 0, IntPtr.Zero, IntPtr.Zero));
            Assert.True(fileSystem.DirectoryExists(tempDirectory));
            Assert.Equal(projectXml, fileSystem.ReadAllText(tempProjFile));
            Mock.Get(frame).Verify(f => f.Show());
            Assert.True(autoOpenSet);
            Assert.True(listenerSet);
            Assert.NotNull(notifier);

            // Verify that the model watcher was correctly initialized
            Mock.Get(modelWatcher).Verify(m => m.InitializeAsync(tempProjFile), Times.Once);
            Mock.Get(modelWatcher).Verify(m => m.Dispose(), Times.Never);

            // Now see if the event correctly saved the text from the buffer into the project file
            var args = new TextDocumentFileActionEventArgs(tempProjFile, DateTime.Now, FileActionTypes.ContentSavedToDisk);

            Mock.Get(textDoc).Raise(t => t.FileActionOccurred += null, args);
            Assert.Equal(projectXml, fileSystem.ReadAllText(projectPath));
            Mock.Get(modelWatcher).Verify(m => m.Dispose(), Times.Never);

            // Finally, ensure the cleanup works as expected. We don't do anything with the passed option. The notifier
            // should remove the file temp file from the filesystem.
            Assert.Equal(VSConstants.S_OK, notifier.OnClose(0));
            Assert.False(fileSystem.DirectoryExists(tempDirectory));
            Assert.False(fileSystem.FileExists(tempProjFile));

            // Verify that dispose was called on the model watcher when the window was closed
            Mock.Get(modelWatcher).Verify(m => m.Dispose(), Times.Once);
        }