forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionCommandTests.cs
203 lines (171 loc) · 8.92 KB
/
ExtensionCommandTests.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Extensions;
using Microsoft.PowerShell.EditorServices.Extensions.Services;
using Microsoft.PowerShell.EditorServices.Services.Extension;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Test;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using Xunit;
namespace PowerShellEditorServices.Test.Extensions
{
[Trait("Category", "Extensions")]
public class ExtensionCommandTests : IDisposable
{
private readonly PsesInternalHost psesHost;
private readonly ExtensionCommandService extensionCommandService;
public ExtensionCommandTests()
{
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
ExtensionService extensionService = new(
languageServer: null,
serviceProvider: null,
editorOperations: null,
executionService: psesHost);
#pragma warning disable VSTHRD002
extensionService.InitializeAsync().Wait();
#pragma warning restore VSTHRD002
extensionCommandService = new(extensionService);
}
public void Dispose()
{
#pragma warning disable VSTHRD002
psesHost.StopAsync().Wait();
#pragma warning restore VSTHRD002
GC.SuppressFinalize(this);
}
[Fact]
public async Task CanRegisterAndInvokeCommandWithCmdletName()
{
string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1");
ScriptFile currentFile = ScriptFile.Create(new Uri(filePath), "This is a test file", new Version("7.0"));
EditorContext editorContext = new(
editorOperations: null,
currentFile,
new BufferPosition(line: 1, column: 1),
BufferRange.None);
EditorCommand commandAdded = null;
extensionCommandService.CommandAdded += (_, command) => commandAdded = command;
const string commandName = "test.function";
const string commandDisplayName = "Function extension";
await psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript(
"function Invoke-Extension { $global:extensionValue = 5 }; " +
$"Register-EditorCommand -Name {commandName} -DisplayName \"{commandDisplayName}\" -Function Invoke-Extension"),
CancellationToken.None);
Assert.NotNull(commandAdded);
Assert.Equal(commandName, commandAdded.Name);
Assert.Equal(commandDisplayName, commandAdded.DisplayName);
// Invoke the command
await extensionCommandService.InvokeCommandAsync(commandName, editorContext);
// Assert the expected value
PSCommand psCommand = new PSCommand().AddScript("$global:extensionValue");
IEnumerable<int> results = await psesHost.ExecutePSCommandAsync<int>(psCommand, CancellationToken.None);
Assert.Equal(5, results.FirstOrDefault());
}
[Fact]
public async Task CanRegisterAndInvokeCommandWithScriptBlock()
{
string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1");
ScriptFile currentFile = ScriptFile.Create(new Uri(filePath), "This is a test file", new Version("7.0"));
EditorContext editorContext = new(
editorOperations: null,
currentFile,
new BufferPosition(line: 1, column: 1),
BufferRange.None);
EditorCommand commandAdded = null;
extensionCommandService.CommandAdded += (_, command) => commandAdded = command;
const string commandName = "test.scriptblock";
const string commandDisplayName = "ScriptBlock extension";
await psesHost.ExecutePSCommandAsync(
new PSCommand()
.AddCommand("Register-EditorCommand")
.AddParameter("Name", commandName)
.AddParameter("DisplayName", commandDisplayName)
.AddParameter("ScriptBlock", ScriptBlock.Create("$global:extensionValue = 10")),
CancellationToken.None);
Assert.NotNull(commandAdded);
Assert.Equal(commandName, commandAdded.Name);
Assert.Equal(commandDisplayName, commandAdded.DisplayName);
// Invoke the command.
// TODO: What task was this cancelling?
await extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext);
// Assert the expected value
PSCommand psCommand = new PSCommand().AddScript("$global:extensionValue");
IEnumerable<int> results = await psesHost.ExecutePSCommandAsync<int>(psCommand, CancellationToken.None);
Assert.Equal(10, results.FirstOrDefault());
}
[Fact]
public async Task CanUpdateRegisteredCommand()
{
EditorCommand updatedCommand = null;
extensionCommandService.CommandUpdated += (_, command) => updatedCommand = command;
const string commandName = "test.function";
const string commandDisplayName = "Updated function extension";
// Register a command and then update it
await psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript(
"function Invoke-Extension { Write-Output \"Extension output!\" }; " +
$"Register-EditorCommand -Name {commandName} -DisplayName \"Old function extension\" -Function Invoke-Extension; " +
$"Register-EditorCommand -Name {commandName} -DisplayName \"{commandDisplayName}\" -Function Invoke-Extension"),
CancellationToken.None);
// Wait for the add and update events
Assert.NotNull(updatedCommand);
Assert.Equal(commandName, updatedCommand.Name);
Assert.Equal(commandDisplayName, updatedCommand.DisplayName);
}
[Fact]
public async Task CanUnregisterCommand()
{
string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1");
ScriptFile currentFile = ScriptFile.Create(new Uri(filePath), "This is a test file", new Version("7.0"));
EditorContext editorContext = new(
editorOperations: null,
currentFile,
new BufferPosition(line: 1, column: 1),
BufferRange.None);
const string commandName = "test.scriptblock";
const string commandDisplayName = "ScriptBlock extension";
EditorCommand removedCommand = null;
extensionCommandService.CommandRemoved += (_, command) => removedCommand = command;
// Add the command and wait for the add event
await psesHost.ExecutePSCommandAsync(
new PSCommand()
.AddCommand("Register-EditorCommand")
.AddParameter("Name", commandName)
.AddParameter("DisplayName", commandDisplayName)
.AddParameter("ScriptBlock", ScriptBlock.Create("Write-Output \"Extension output!\"")),
CancellationToken.None);
// Remove the command and wait for the remove event
await psesHost.ExecutePSCommandAsync(
new PSCommand().AddCommand("Unregister-EditorCommand").AddParameter("Name", commandName),
CancellationToken.None);
Assert.NotNull(removedCommand);
Assert.Equal(commandName, removedCommand.Name);
Assert.Equal(commandDisplayName, removedCommand.DisplayName);
// Ensure that the command has been unregistered
await Assert.ThrowsAsync<KeyNotFoundException>(
() => extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext));
}
[Fact]
public async Task CannotRemovePSEditorVariable()
{
ActionPreferenceStopException exception = await Assert.ThrowsAsync<ActionPreferenceStopException>(
() => psesHost.ExecutePSCommandAsync<string>(
new PSCommand().AddScript("Remove-Variable psEditor -ErrorAction Stop"),
CancellationToken.None)
);
Assert.Equal(
"The running command stopped because the preference variable \"ErrorActionPreference\" or common parameter is set to Stop: Cannot remove variable psEditor because it is constant or read-only. If the variable is read-only, try the operation again specifying the Force option.",
exception.Message);
}
}
}