public async Task NonStandardEncoding_UsesProjectEncoding()
        {
            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 msbuildAccessor = IProjectXmlAccessorFactory.ImplementGetProjectXml("<Project />");

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

            fileSystem.SetTempFile(tempFilePath);

            var textBufferManager = new TempFileTextBufferManager(unconfiguredProject,
                                                                  msbuildAccessor,
                                                                  IVsEditorAdaptersFactoryServiceFactory.Create(),
                                                                  ITextDocumentFactoryServiceFactory.Create(),
                                                                  IVsShellUtilitiesHelperFactory.Create(),
                                                                  fileSystem,
                                                                  new IProjectThreadingServiceMock(),
                                                                  IServiceProviderFactory.Create());

            await textBufferManager.InitializeBufferAsync();

            var tempProjectPath = Path.Combine(tempFilePath, "ConsoleApp1.csproj");
            var tempFile        = fileSystem.Files.First(data => StringComparers.Paths.Equals(tempProjectPath, data.Key));

            Assert.Equal(encoding, tempFile.Value.FileEncoding);
        }
        public async Task Initialize_CreatesTempFile()
        {
            var projectFilePath     = @"C:\ConsoleApp\ConsoleApp1\ConsoleApp1.csproj";
            var unconfiguredProject = UnconfiguredProjectFactory.Create(filePath: projectFilePath, projectEncoding: Encoding.Default);

            var msbuildAccessor = IProjectXmlAccessorFactory.ImplementGetProjectXml("<Project />");

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

            fileSystem.SetTempFile(tempFilePath);

            var textBufferManager = new TempFileTextBufferManager(unconfiguredProject,
                                                                  msbuildAccessor,
                                                                  IVsEditorAdaptersFactoryServiceFactory.Create(),
                                                                  ITextDocumentFactoryServiceFactory.Create(),
                                                                  IVsShellUtilitiesHelperFactory.Create(),
                                                                  fileSystem,
                                                                  new IProjectThreadingServiceMock(),
                                                                  IServiceProviderFactory.Create());

            await textBufferManager.InitializeBufferAsync();

            var tempProjectPath = Path.Combine(tempFilePath, "ConsoleApp1.csproj");

            Assert.True(fileSystem.FileExists(tempProjectPath));
            Assert.Equal("<Project />", fileSystem.ReadAllText(tempProjectPath));
        }
        public async Task NoChangesFrom_DoesNotOverwriteNewChanges()
        {
            var projectFilePath = @"C:\ConsoleApp\ConsoleApp1\ConsoleApp1.csproj";
            // Use some file encoding that's not the default
            var unconfiguredProject = UnconfiguredProjectFactory.Create(filePath: projectFilePath, projectEncoding: Encoding.UTF8);

            var projectXml = "<Project />";
            var bufferXml  = "<Project></Project>";


            var docData      = IVsPersistDocDataFactory.ImplementAsIVsTextBufferIsDocDataDirty(true, VSConstants.S_OK);
            var textBuffer   = ITextBufferFactory.ImplementSnapshot(() => bufferXml);
            var textDocument = ITextDocumentFactory.ImplementTextBuffer(textBuffer);

            var fileSystem      = new IFileSystemMock();
            var tempFilePath    = @"C:\Temp\asdf.1234";
            var tempProjectPath = Path.Combine(tempFilePath, "ConsoleApp1.csproj");

            fileSystem.SetTempFile(tempFilePath);

            var msbuildAccessor = IProjectXmlAccessorFactory.Implement(() => projectXml, xml => fileSystem.WriteAllText(tempProjectPath, xml));

            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();

            var tempFile = fileSystem.Files.First(data => StringComparers.Paths.Equals(tempProjectPath, data.Key));

            // First save. File system should be "<Project></Project>", last saved is also "<Project></Project>"
            await textBufferManager.SaveAsync();

            Assert.Equal("<Project></Project>", fileSystem.ReadAllText(tempProjectPath));

            // Now we simulate some changes to the buffer and call Reset. Both the last saved and current project xml should be "<Project></Project>", so
            // the buffer shouldn't be reset
            projectXml = bufferXml;
            bufferXml  = "<Project>asdf</Project>";
            await textBufferManager.ResetBufferAsync();

            Mock.Get(textBuffer).Verify(t => t.Replace(It.IsAny <Span>(), It.IsAny <string>()), Times.Never);
            Assert.Equal("<Project></Project>", fileSystem.ReadAllText(tempProjectPath));
        }
        public async Task ResetBufferDirtyDocData_UpdatesFileNotBuffer()
        {
            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.Implement(() => xml, s => { });

            var docData      = IVsPersistDocDataFactory.ImplementAsIVsTextBufferIsDocDataDirty(true, VSConstants.S_OK);
            var textBuffer   = ITextBufferFactory.ImplementSnapshot("<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();

            var tempProjectPath = Path.Combine(tempFilePath, "ConsoleApp1.csproj");
            var tempFile        = fileSystem.Files.First(data => StringComparers.Paths.Equals(tempProjectPath, data.Key));

            Assert.Equal("<Project />", fileSystem.ReadAllText(tempProjectPath));

            xml = "<Project></Project>";
            await textBufferManager.ResetBufferAsync();

            Mock.Get(textBuffer).Verify(t => t.Replace(It.IsAny <Span>(), It.IsAny <string>()), Times.Never);
            Assert.Equal("<Project></Project>", fileSystem.ReadAllText(tempProjectPath));
            Mock.Get(unconfiguredProject).Verify(u => u.SaveAsync(null), Times.Once);
        }
        public async Task SetReadonly_SetsOnlyReadonlyFlag()
        {
            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();

            await textBufferManager.SetReadOnlyAsync(true);

            Mock.Get(docData).As <IVsTextBuffer>().Verify(t =>
                                                          t.SetStateFlags(~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY));
            await textBufferManager.SetReadOnlyAsync(false);

            Mock.Get(docData).As <IVsTextBuffer>().Verify(t => t.SetStateFlags(~(uint)BUFFERSTATEFLAGS.BSF_USER_READONLY));
        }