-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathExtensionCommandTests.cs
196 lines (160 loc) · 8.11 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
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Extensions;
using Microsoft.PowerShell.EditorServices.Extensions.Services;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.PowerShell.EditorServices.Test.Extensions
{
// TODO:
// These tests require being able to instantiate a language server and use the service provider.
// Re-enable them when we have mocked out more infrastructure for testing.
/*
public class ExtensionCommandTests : IDisposable
{
private readonly PowerShellContextService _powershellContextService;
private readonly IExtensionCommandService _extensionCommandService;
private readonly ExtensionService _extensionService;
public ExtensionCommandTests()
{
_powershellContextService = PowerShellContextFactory.Create(NullLogger.Instance);
_extensionCommandService = EditorObject.Instance.GetExtensionServiceProvider().ExtensionCommands;
}
[Trait("Category", "Extensions")]
[Fact]
public async Task CanRegisterAndInvokeCommandWithCmdletName()
{
string filePath = TestUtilities.NormalizePath("C:\\Temp\\Test.ps1");
var currentFile = new ScriptFile(new Uri(filePath), "This is a test file", new Version("7.0"));
var editorContext = new EditorContext(
editorOperations: null,
currentFile,
new BufferPosition(line: 1, column: 1),
BufferRange.None);
EditorCommand commandAdded = null;
_extensionCommandService.CommandAdded += (object sender, EditorCommand command) =>
{
commandAdded = command;
};
string commandName = "test.function";
string commandDisplayName = "Function extension";
await _powershellContextService.ExecuteScriptStringAsync(
TestUtilities.NormalizeNewlines($@"
function Invoke-Extension {{ $global:testValue = 5 }}
Register-EditorCommand -Name {commandName} -DisplayName ""{commandDisplayName}"" -Function Invoke-Extension"));
Assert.NotNull(commandAdded);
Assert.Equal(commandAdded.Name, commandName);
Assert.Equal(commandAdded.DisplayName, commandDisplayName);
// Invoke the command
await _extensionCommandService.InvokeCommandAsync(commandName, editorContext);
// Assert the expected value
PSCommand psCommand = new PSCommand().AddScript("$global:extensionValue");
IEnumerable<int> results = await _powershellContextService.ExecuteCommandAsync<int>(psCommand);
Assert.Equal(5, results.FirstOrDefault());
}
[Trait("Category", "Extensions")]
[Fact]
public async Task CanRegisterAndInvokeCommandWithScriptBlock()
{
string filePath = TestUtilities.NormalizePath("C:\\Temp\\Test.ps1");
var currentFile = new ScriptFile(new Uri(filePath), "This is a test file", new Version("7.0"));
var editorContext = new EditorContext(
editorOperations: null,
currentFile,
new BufferPosition(line: 1, column: 1),
BufferRange.None);
EditorCommand commandAdded = null;
_extensionCommandService.CommandAdded += (object sender, EditorCommand command) =>
{
commandAdded = command;
};
string commandName = "test.scriptblock";
string commandDisplayName = "ScriptBlock extension";
await _powershellContextService.ExecuteCommandAsync(new PSCommand()
.AddCommand("Register-EditorCommand")
.AddParameter("Name", commandName)
.AddParameter("DisplayName", commandDisplayName)
.AddParameter("ScriptBlock", ScriptBlock.Create("$global:extensionValue = 10")));
Assert.NotNull(commandAdded);
Assert.Equal(commandName, commandAdded.Name);
Assert.Equal(commandDisplayName, commandAdded.DisplayName);
// Invoke the command
await _extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext);
// Assert the expected value
PSCommand psCommand = new PSCommand().AddScript("$global:extensionValue");
IEnumerable<int> results = await _powershellContextService.ExecuteCommandAsync<int>(psCommand);
Assert.Equal(10, results.FirstOrDefault());
}
[Trait("Category", "Extensions")]
[Fact]
public async Task CanUpdateRegisteredCommand()
{
EditorCommand updatedCommand = null;
_extensionCommandService.CommandUpdated += (object sender, EditorCommand command) =>
{
updatedCommand = command;
};
string commandName = "test.function";
string commandDisplayName = "Updated function extension";
// Register a command and then update it
await _powershellContextService.ExecuteScriptStringAsync(TestUtilities.NormalizeNewlines(
"function Invoke-Extension { Write-Output \"Extension output!\" }\n" +
$"Register-EditorCommand -Name \"{commandName}\" -DisplayName \"Old function extension\" -Function \"Invoke-Extension\"\n" +
$"Register-EditorCommand -Name \"{commandName}\" -DisplayName \"{commandDisplayName}\" -Function \"Invoke-Extension\""));
// Wait for the add and update events
Assert.NotNull(updatedCommand);
Assert.Equal(commandName, updatedCommand.Name);
Assert.Equal(commandDisplayName, updatedCommand.DisplayName);
}
[Trait("Category", "Extensions")]
[Fact]
public async Task CanUnregisterCommand()
{
string filePath = TestUtilities.NormalizePath("C:\\Temp\\Test.ps1");
var currentFile = new ScriptFile(new Uri(filePath), "This is a test file", new Version("7.0"));
var editorContext = new EditorContext(
editorOperations: null,
currentFile,
new BufferPosition(line: 1, column: 1),
BufferRange.None);
string commandName = "test.scriptblock";
string commandDisplayName = "ScriptBlock extension";
EditorCommand removedCommand = null;
_extensionCommandService.CommandRemoved += (object sender, EditorCommand command) =>
{
removedCommand = command;
};
// Add the command and wait for the add event
await _powershellContextService.ExecuteCommandAsync(new PSCommand()
.AddCommand("Register-EditorCommand")
.AddParameter("Name", commandName)
.AddParameter("DisplayName", commandDisplayName)
.AddParameter("ScriptBlock", ScriptBlock.Create("Write-Output \"Extension output!\"")));
// Remove the command and wait for the remove event
await _powershellContextService.ExecuteCommandAsync(new PSCommand()
.AddCommand("Unregister-EditorCommand")
.AddParameter("Name", commandName));
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));
}
public void Dispose()
{
_powershellContextService.Dispose();
}
}
*/
}