forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionServiceTests.cs
255 lines (208 loc) · 9.51 KB
/
ExtensionServiceTests.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.PowerShell.EditorServices.Components;
using Microsoft.PowerShell.EditorServices.Extensions;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using Microsoft.PowerShell.EditorServices.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.PowerShell.EditorServices.Test.Extensions
{
public class ExtensionServiceTests : IAsyncLifetime
{
private ScriptFile currentFile;
private EditorContext commandContext;
private ExtensionService extensionService;
private PowerShellContext powerShellContext;
private TestEditorOperations editorOperations;
private AsyncQueue<Tuple<EventType, EditorCommand>> extensionEventQueue =
new AsyncQueue<Tuple<EventType, EditorCommand>>();
private enum EventType
{
Add,
Update,
Remove
}
public async Task InitializeAsync()
{
var logger = Logging.NullLogger;
this.powerShellContext = PowerShellContextFactory.Create(logger);
await this.powerShellContext.ImportCommandsModuleAsync(
TestUtilities.NormalizePath("../../../../../module/PowerShellEditorServices/Commands"));
this.extensionService = new ExtensionService(this.powerShellContext);
this.editorOperations = new TestEditorOperations();
this.extensionService.CommandAdded += ExtensionService_ExtensionAdded;
this.extensionService.CommandUpdated += ExtensionService_ExtensionUpdated;
this.extensionService.CommandRemoved += ExtensionService_ExtensionRemoved;
await this.extensionService.InitializeAsync(
this.editorOperations,
new ComponentRegistry());
var filePath = TestUtilities.NormalizePath("c:/Test/Test.ps1");
this.currentFile = new ScriptFile(filePath, filePath, "This is a test file", new Version("5.0"));
this.commandContext =
new EditorContext(
this.editorOperations,
currentFile,
new BufferPosition(1, 1),
BufferRange.None);
}
public Task DisposeAsync()
{
this.powerShellContext.Dispose();
return Task.FromResult(true);
}
[Fact]
public async Task CanRegisterAndInvokeCommandWithCmdletName()
{
await extensionService.PowerShellContext.ExecuteScriptStringAsync(
TestUtilities.NormalizeNewlines("function Invoke-Extension { $global:extensionValue = 5 }\n") +
"Register-EditorCommand -Name \"test.function\" -DisplayName \"Function extension\" -Function \"Invoke-Extension\"");
// Wait for the add event
EditorCommand command = await this.AssertExtensionEvent(EventType.Add, "test.function");
// Invoke the command
await extensionService.InvokeCommandAsync("test.function", this.commandContext);
// Assert the expected value
PSCommand psCommand = new PSCommand();
psCommand.AddScript("$global:extensionValue");
var results = await powerShellContext.ExecuteCommandAsync<int>(psCommand);
Assert.Equal(5, results.FirstOrDefault());
}
[Fact]
public async Task CanRegisterAndInvokeCommandWithScriptBlock()
{
await extensionService.PowerShellContext.ExecuteScriptStringAsync(
"Register-EditorCommand -Name \"test.scriptblock\" -DisplayName \"ScriptBlock extension\" -ScriptBlock { $global:extensionValue = 10 }");
// Wait for the add event
EditorCommand command = await this.AssertExtensionEvent(EventType.Add, "test.scriptblock");
// Invoke the command
await extensionService.InvokeCommandAsync("test.scriptblock", this.commandContext);
// Assert the expected value
PSCommand psCommand = new PSCommand();
psCommand.AddScript("$global:extensionValue");
var results = await powerShellContext.ExecuteCommandAsync<int>(psCommand);
Assert.Equal(10, results.FirstOrDefault());
}
[Fact]
public async Task CanUpdateRegisteredCommand()
{
// Register a command and then update it
await extensionService.PowerShellContext.ExecuteScriptStringAsync(TestUtilities.NormalizeNewlines(
"function Invoke-Extension { Write-Output \"Extension output!\" }\n" +
"Register-EditorCommand -Name \"test.function\" -DisplayName \"Function extension\" -Function \"Invoke-Extension\"\n" +
"Register-EditorCommand -Name \"test.function\" -DisplayName \"Updated Function extension\" -Function \"Invoke-Extension\""));
// Wait for the add and update events
await this.AssertExtensionEvent(EventType.Add, "test.function");
EditorCommand updatedCommand = await this.AssertExtensionEvent(EventType.Update, "test.function");
Assert.Equal("Updated Function extension", updatedCommand.DisplayName);
}
[Fact]
public async Task CanUnregisterCommand()
{
// Add the command and wait for the add event
await extensionService.PowerShellContext.ExecuteScriptStringAsync(
"Register-EditorCommand -Name \"test.scriptblock\" -DisplayName \"ScriptBlock extension\" -ScriptBlock { Write-Output \"Extension output!\" }");
await this.AssertExtensionEvent(EventType.Add, "test.scriptblock");
// Remove the command and wait for the remove event
await extensionService.PowerShellContext.ExecuteScriptStringAsync(
"Unregister-EditorCommand -Name \"test.scriptblock\"");
await this.AssertExtensionEvent(EventType.Remove, "test.scriptblock");
// Ensure that the command has been unregistered
await Assert.ThrowsAsync<KeyNotFoundException>(() =>
extensionService.InvokeCommandAsync("test.scriptblock", this.commandContext));
}
private async Task<EditorCommand> AssertExtensionEvent(EventType expectedEventType, string expectedExtensionName)
{
var eventExtensionTuple =
await this.extensionEventQueue.DequeueAsync(
new CancellationTokenSource(5000).Token);
Assert.Equal(expectedEventType, eventExtensionTuple.Item1);
Assert.Equal(expectedExtensionName, eventExtensionTuple.Item2.Name);
return eventExtensionTuple.Item2;
}
private async void ExtensionService_ExtensionAdded(object sender, EditorCommand e)
{
await this.extensionEventQueue.EnqueueAsync(
new Tuple<EventType, EditorCommand>(EventType.Add, e));
}
private async void ExtensionService_ExtensionUpdated(object sender, EditorCommand e)
{
await this.extensionEventQueue.EnqueueAsync(
new Tuple<EventType, EditorCommand>(EventType.Update, e));
}
private async void ExtensionService_ExtensionRemoved(object sender, EditorCommand e)
{
await this.extensionEventQueue.EnqueueAsync(
new Tuple<EventType, EditorCommand>(EventType.Remove, e));
}
}
public class TestEditorOperations : IEditorOperations
{
public string GetWorkspacePath()
{
throw new NotImplementedException();
}
public string GetWorkspaceRelativePath(string filePath)
{
throw new NotImplementedException();
}
public Task NewFileAsync()
{
throw new NotImplementedException();
}
public Task OpenFileAsync(string filePath)
{
throw new NotImplementedException();
}
public Task OpenFileAsync(string filePath, bool preview)
{
throw new NotImplementedException();
}
public Task CloseFileAsync(string filePath)
{
throw new NotImplementedException();
}
public Task SaveFileAsync(string filePath)
{
return SaveFileAsync(filePath, null);
}
public Task SaveFileAsync(string filePath, string newSavePath)
{
throw new NotImplementedException();
}
public Task InsertTextAsync(string filePath, string text, BufferRange insertRange)
{
throw new NotImplementedException();
}
public Task SetSelectionAsync(BufferRange selectionRange)
{
throw new NotImplementedException();
}
public Task<EditorContext> GetEditorContextAsync()
{
throw new NotImplementedException();
}
public Task ShowInformationMessageAsync(string message)
{
throw new NotImplementedException();
}
public Task ShowErrorMessageAsync(string message)
{
throw new NotImplementedException();
}
public Task ShowWarningMessageAsync(string message)
{
throw new NotImplementedException();
}
public Task SetStatusBarMessageAsync(string message, int? timeout)
{
throw new NotImplementedException();
}
}
}