Repository navigation
Expand file tree
/
Copy pathEditorOperationsServiceTests.cs
More file actions
147 lines (121 loc) · 6.25 KB
/
Copy pathEditorOperationsServiceTests.cs
File metadata and controls
147 lines (121 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.IO;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Extensions;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.Configuration;
using Microsoft.PowerShell.EditorServices.Services.Extension;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Newtonsoft.Json;
using OmniSharp.Extensions.LanguageServer.Protocol;
using Xunit;
namespace PowerShellEditorServices.Test.Extensions
{
[CollectionDefinition("Console", DisableParallelization = true)]
public class ConsoleCollectionDefinition
{
}
[Collection("Console")]
[Trait("Category", "Extensions")]
public class EditorOperationsServiceTests
{
[Fact]
public void GetWorkspaceOpenDocumentsReturnsOnlyOpenDocumentsAndCurrentInMemoryState()
{
WorkspaceService workspaceService = new(NullLoggerFactory.Instance);
ScriptFile openSaved = CreateFileBuffer(workspaceService, "open-saved.ps1");
openSaved.IsOpen = true;
openSaved.IsInMemory = false;
ScriptFile openUnsaved = CreateFileBuffer(workspaceService, "open-unsaved.ps1");
openUnsaved.IsOpen = true;
openUnsaved.IsInMemory = true;
ScriptFile closed = CreateFileBuffer(workspaceService, "closed.ps1");
closed.IsOpen = false;
closed.IsInMemory = false;
EditorOperationsService editorOperationsService = new(
psesHost: null,
workspaceService,
languageServer: null,
new ConfigurationService());
WorkspaceOpenDocument[] documents = editorOperationsService.GetWorkspaceOpenDocuments();
Assert.Equal(2, documents.Length);
Assert.Contains(documents, static document => document.Path.EndsWith("open-saved.ps1") && document.Saved);
Assert.Contains(documents, static document => document.Path.EndsWith("open-unsaved.ps1") && !document.Saved);
Assert.DoesNotContain(documents, static document => document.Path.EndsWith("closed.ps1"));
}
[Fact]
public void GetWorkspaceOpenDocumentsTracksEditedAndUntitledSaveStates()
{
WorkspaceService workspaceService = new(NullLoggerFactory.Instance);
ScriptFile openSaved = CreateFileBuffer(workspaceService, "open-saved.ps1");
openSaved.IsOpen = true;
ScriptFile openUntitled = workspaceService.GetFileBuffer("untitled:Untitled-1", initialBuffer: string.Empty);
openUntitled.IsOpen = true;
EditorOperationsService editorOperationsService = new(
psesHost: null,
workspaceService,
languageServer: null,
new ConfigurationService());
WorkspaceOpenDocument[] initialDocuments = editorOperationsService.GetWorkspaceOpenDocuments();
Assert.Contains(initialDocuments, static document => document.Path.EndsWith("open-saved.ps1") && document.Saved);
Assert.Contains(initialDocuments, static document => document.Path.StartsWith("untitled:", StringComparison.Ordinal) && !document.Saved);
openSaved.ApplyChange(new FileChange
{
Line = 1,
Offset = 1,
EndLine = 1,
EndOffset = 1,
InsertString = "Set-StrictMode -Version Latest"
});
Assert.Contains("Set-StrictMode -Version Latest", openSaved.Contents, StringComparison.Ordinal);
WorkspaceOpenDocument[] editedDocuments = editorOperationsService.GetWorkspaceOpenDocuments();
Assert.Contains(editedDocuments, static document => document.Path.EndsWith("open-saved.ps1") && !document.Saved);
MarkAsSaved(openSaved);
MarkAsSaved(openUntitled);
WorkspaceOpenDocument[] savedDocuments = editorOperationsService.GetWorkspaceOpenDocuments();
Assert.Contains(savedDocuments, static document => document.Path.EndsWith("open-saved.ps1") && document.Saved);
Assert.Contains(savedDocuments, static document => document.Path.StartsWith("untitled:", StringComparison.Ordinal) && !document.Saved);
}
[Fact]
public void LanguageServerSettingsUpdatesForceClearScrollbackBuffer()
{
LanguageServerSettings incomingSettings = JsonConvert.DeserializeObject<LanguageServerSettings>(
"{\"integratedConsole\":{\"forceClearScrollbackBuffer\":true}}");
LanguageServerSettings currentSettings = new();
currentSettings.Update(incomingSettings, workspaceRootPath: string.Empty, NullLogger.Instance);
Assert.True(currentSettings.IntegratedConsole.ForceClearScrollbackBuffer);
}
[Fact]
public void ClearTerminalWritesEraseSavedLinesWhenConfigured()
{
WorkspaceService workspaceService = new(NullLoggerFactory.Instance);
ConfigurationService configurationService = new();
configurationService.CurrentSettings.IntegratedConsole.ForceClearScrollbackBuffer = true;
EditorOperationsService editorOperationsService = new(
psesHost: null,
workspaceService,
languageServer: null,
configurationService);
StringWriter output = new();
TextWriter originalOutput = Console.Out;
try
{
Console.SetOut(output);
editorOperationsService.ClearTerminal();
}
finally
{
Console.SetOut(originalOutput);
}
Assert.Equal("\u001b[3J", output.ToString());
}
private static ScriptFile CreateFileBuffer(WorkspaceService workspaceService, string fileName)
{
string filePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"), fileName);
return workspaceService.GetFileBuffer(DocumentUri.FromFileSystemPath(filePath), initialBuffer: string.Empty);
}
private static void MarkAsSaved(ScriptFile scriptFile) => scriptFile.IsInMemory = scriptFile.IsUntitled;
}
}