|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using Microsoft.Extensions.Logging.Abstractions; |
| 7 | +using Microsoft.PowerShell.EditorServices.Extensions; |
| 8 | +using Microsoft.PowerShell.EditorServices.Services; |
| 9 | +using Microsoft.PowerShell.EditorServices.Services.Extension; |
| 10 | +using Microsoft.PowerShell.EditorServices.Services.TextDocument; |
| 11 | +using OmniSharp.Extensions.LanguageServer.Protocol; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace PowerShellEditorServices.Test.Extensions |
| 15 | +{ |
| 16 | + [Trait("Category", "Extensions")] |
| 17 | + public class EditorOperationsServiceTests |
| 18 | + { |
| 19 | + [Fact] |
| 20 | + public void GetWorkspaceOpenDocumentsReturnsOnlyOpenDocumentsAndCurrentInMemoryState() |
| 21 | + { |
| 22 | + WorkspaceService workspaceService = new(NullLoggerFactory.Instance); |
| 23 | + |
| 24 | + ScriptFile openSaved = CreateFileBuffer(workspaceService, "open-saved.ps1"); |
| 25 | + openSaved.IsOpen = true; |
| 26 | + openSaved.IsInMemory = false; |
| 27 | + |
| 28 | + ScriptFile openUnsaved = CreateFileBuffer(workspaceService, "open-unsaved.ps1"); |
| 29 | + openUnsaved.IsOpen = true; |
| 30 | + openUnsaved.IsInMemory = true; |
| 31 | + |
| 32 | + ScriptFile closed = CreateFileBuffer(workspaceService, "closed.ps1"); |
| 33 | + closed.IsOpen = false; |
| 34 | + closed.IsInMemory = false; |
| 35 | + |
| 36 | + EditorOperationsService editorOperationsService = new( |
| 37 | + psesHost: null, |
| 38 | + workspaceService, |
| 39 | + languageServer: null); |
| 40 | + |
| 41 | + WorkspaceOpenDocument[] documents = editorOperationsService.GetWorkspaceOpenDocuments(); |
| 42 | + |
| 43 | + Assert.Equal(2, documents.Length); |
| 44 | + Assert.Contains(documents, static document => document.Path.EndsWith("open-saved.ps1") && document.Saved); |
| 45 | + Assert.Contains(documents, static document => document.Path.EndsWith("open-unsaved.ps1") && !document.Saved); |
| 46 | + Assert.DoesNotContain(documents, static document => document.Path.EndsWith("closed.ps1")); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void GetWorkspaceOpenDocumentsTracksEditedAndUntitledSaveStates() |
| 51 | + { |
| 52 | + WorkspaceService workspaceService = new(NullLoggerFactory.Instance); |
| 53 | + |
| 54 | + ScriptFile openSaved = CreateFileBuffer(workspaceService, "open-saved.ps1"); |
| 55 | + openSaved.IsOpen = true; |
| 56 | + |
| 57 | + ScriptFile openUntitled = workspaceService.GetFileBuffer("untitled:Untitled-1", initialBuffer: string.Empty); |
| 58 | + openUntitled.IsOpen = true; |
| 59 | + |
| 60 | + EditorOperationsService editorOperationsService = new( |
| 61 | + psesHost: null, |
| 62 | + workspaceService, |
| 63 | + languageServer: null); |
| 64 | + |
| 65 | + WorkspaceOpenDocument[] initialDocuments = editorOperationsService.GetWorkspaceOpenDocuments(); |
| 66 | + Assert.Contains(initialDocuments, static document => document.Path.EndsWith("open-saved.ps1") && document.Saved); |
| 67 | + Assert.Contains(initialDocuments, static document => document.Path.StartsWith("untitled:", StringComparison.Ordinal) && !document.Saved); |
| 68 | + |
| 69 | + openSaved.ApplyChange(new FileChange |
| 70 | + { |
| 71 | + Line = 1, |
| 72 | + Offset = 1, |
| 73 | + EndLine = 1, |
| 74 | + EndOffset = 1, |
| 75 | + InsertString = "Set-StrictMode -Version Latest" |
| 76 | + }); |
| 77 | + Assert.Contains("Set-StrictMode -Version Latest", openSaved.Contents, StringComparison.Ordinal); |
| 78 | + |
| 79 | + WorkspaceOpenDocument[] editedDocuments = editorOperationsService.GetWorkspaceOpenDocuments(); |
| 80 | + Assert.Contains(editedDocuments, static document => document.Path.EndsWith("open-saved.ps1") && !document.Saved); |
| 81 | + |
| 82 | + MarkAsSaved(openSaved); |
| 83 | + MarkAsSaved(openUntitled); |
| 84 | + |
| 85 | + WorkspaceOpenDocument[] savedDocuments = editorOperationsService.GetWorkspaceOpenDocuments(); |
| 86 | + Assert.Contains(savedDocuments, static document => document.Path.EndsWith("open-saved.ps1") && document.Saved); |
| 87 | + Assert.Contains(savedDocuments, static document => document.Path.StartsWith("untitled:", StringComparison.Ordinal) && !document.Saved); |
| 88 | + } |
| 89 | + |
| 90 | + private static ScriptFile CreateFileBuffer(WorkspaceService workspaceService, string fileName) |
| 91 | + { |
| 92 | + string filePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"), fileName); |
| 93 | + return workspaceService.GetFileBuffer(DocumentUri.FromFileSystemPath(filePath), initialBuffer: string.Empty); |
| 94 | + } |
| 95 | + |
| 96 | + private static void MarkAsSaved(ScriptFile scriptFile) => scriptFile.IsInMemory = scriptFile.IsUntitled; |
| 97 | + } |
| 98 | +} |
0 commit comments