1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
3
4
- using Microsoft . Extensions . Logging . Abstractions ;
5
- using Microsoft . PowerShell . EditorServices . Extensions ;
6
- using Microsoft . PowerShell . EditorServices . Extensions . Services ;
7
- using Microsoft . PowerShell . EditorServices . Services ;
8
- using Microsoft . PowerShell . EditorServices . Services . TextDocument ;
9
- using Microsoft . PowerShell . EditorServices . Test . Shared ;
10
4
using System ;
11
5
using System . Collections . Generic ;
12
6
using System . Linq ;
13
7
using System . Management . Automation ;
8
+ using System . Threading ;
14
9
using System . Threading . Tasks ;
10
+ using Microsoft . Extensions . Logging . Abstractions ;
11
+ using Microsoft . PowerShell . EditorServices . Extensions ;
12
+ using Microsoft . PowerShell . EditorServices . Extensions . Services ;
13
+ using Microsoft . PowerShell . EditorServices . Services . Extension ;
14
+ using Microsoft . PowerShell . EditorServices . Services . PowerShell . Host ;
15
+ using Microsoft . PowerShell . EditorServices . Services . TextDocument ;
16
+ using Microsoft . PowerShell . EditorServices . Test . Shared ;
15
17
using Xunit ;
16
18
17
19
namespace Microsoft . PowerShell . EditorServices . Test . Extensions
18
20
{
19
- // TODO:
20
- // These tests require being able to instantiate a language server and use the service provider.
21
- // Re-enable them when we have mocked out more infrastructure for testing.
22
-
23
- /*
21
+ [ Trait ( "Category" , "Extensions" ) ]
24
22
public class ExtensionCommandTests : IDisposable
25
23
{
26
- private readonly PowerShellContextService _powershellContextService;
27
-
28
- private readonly IExtensionCommandService _extensionCommandService;
24
+ private readonly PsesInternalHost psesHost ;
29
25
30
- private readonly ExtensionService _extensionService ;
26
+ private readonly ExtensionCommandService extensionCommandService ;
31
27
32
28
public ExtensionCommandTests ( )
33
29
{
34
- _powershellContextService = PowerShellContextFactory.Create(NullLogger.Instance);
35
- _extensionCommandService = EditorObject.Instance.GetExtensionServiceProvider().ExtensionCommands;
30
+ psesHost = PsesHostFactory . Create ( NullLoggerFactory . Instance ) ;
31
+ ExtensionService extensionService = new (
32
+ languageServer : null ,
33
+ serviceProvider : null ,
34
+ editorOperations : null ,
35
+ executionService : psesHost ) ;
36
+ extensionService . InitializeAsync ( ) . Wait ( ) ;
37
+ extensionCommandService = new ( extensionService ) ;
38
+ }
39
+
40
+ public void Dispose ( )
41
+ {
42
+ psesHost . StopAsync ( ) . Wait ( ) ;
43
+ GC . SuppressFinalize ( this ) ;
36
44
}
37
45
38
- [Trait("Category", "Extensions")]
39
46
[ Fact ]
40
47
public async Task CanRegisterAndInvokeCommandWithCmdletName ( )
41
48
{
@@ -48,34 +55,30 @@ public async Task CanRegisterAndInvokeCommandWithCmdletName()
48
55
BufferRange . None ) ;
49
56
50
57
EditorCommand commandAdded = null ;
51
- _extensionCommandService.CommandAdded += (object sender, EditorCommand command) =>
52
- {
53
- commandAdded = command;
54
- };
55
-
56
- string commandName = "test.function";
57
- string commandDisplayName = "Function extension";
58
+ extensionCommandService . CommandAdded += ( object _ , EditorCommand command ) => commandAdded = command ;
58
59
60
+ const string commandName = "test.function" ;
61
+ const string commandDisplayName = "Function extension" ;
59
62
60
- await _powershellContextService.ExecuteScriptStringAsync(
61
- TestUtilities.NormalizeNewlines($@"
62
- function Invoke-Extension {{ $global:testValue = 5 }}
63
- Register-EditorCommand -Name {commandName} -DisplayName ""{commandDisplayName}"" -Function Invoke-Extension"));
63
+ await psesHost . ExecutePSCommandAsync (
64
+ new PSCommand ( ) . AddScript (
65
+ "function Invoke-Extension { $global:extensionValue = 5 }; " +
66
+ $ "Register-EditorCommand -Name { commandName } -DisplayName \" { commandDisplayName } \" -Function Invoke-Extension") ,
67
+ CancellationToken . None ) . ConfigureAwait ( true ) ;
64
68
65
69
Assert . NotNull ( commandAdded ) ;
66
70
Assert . Equal ( commandAdded . Name , commandName ) ;
67
71
Assert . Equal ( commandAdded . DisplayName , commandDisplayName ) ;
68
72
69
73
// Invoke the command
70
- await _extensionCommandService .InvokeCommandAsync(commandName, editorContext);
74
+ await extensionCommandService . InvokeCommandAsync ( commandName , editorContext ) . ConfigureAwait ( true ) ;
71
75
72
76
// Assert the expected value
73
77
PSCommand psCommand = new PSCommand ( ) . AddScript ( "$global:extensionValue" ) ;
74
- IEnumerable<int> results = await _powershellContextService.ExecuteCommandAsync <int>(psCommand);
78
+ IEnumerable < int > results = await psesHost . ExecutePSCommandAsync < int > ( psCommand , CancellationToken . None ) . ConfigureAwait ( true ) ;
75
79
Assert . Equal ( 5 , results . FirstOrDefault ( ) ) ;
76
80
}
77
81
78
- [Trait("Category", "Extensions")]
79
82
[ Fact ]
80
83
public async Task CanRegisterAndInvokeCommandWithScriptBlock ( )
81
84
{
@@ -87,62 +90,56 @@ public async Task CanRegisterAndInvokeCommandWithScriptBlock()
87
90
new BufferPosition ( line : 1 , column : 1 ) ,
88
91
BufferRange . None ) ;
89
92
90
-
91
93
EditorCommand commandAdded = null ;
92
- _extensionCommandService.CommandAdded += (object sender, EditorCommand command) =>
93
- {
94
- commandAdded = command;
95
- };
94
+ extensionCommandService . CommandAdded += ( object _ , EditorCommand command ) => commandAdded = command ;
96
95
96
+ const string commandName = "test.scriptblock" ;
97
+ const string commandDisplayName = "ScriptBlock extension" ;
97
98
98
- string commandName = "test.scriptblock";
99
- string commandDisplayName = "ScriptBlock extension";
100
-
101
- await _powershellContextService.ExecuteCommandAsync(new PSCommand()
102
- .AddCommand("Register-EditorCommand")
103
- .AddParameter("Name", commandName)
104
- .AddParameter("DisplayName", commandDisplayName)
105
- .AddParameter("ScriptBlock", ScriptBlock.Create("$global:extensionValue = 10")));
99
+ await psesHost . ExecutePSCommandAsync (
100
+ new PSCommand ( )
101
+ . AddCommand ( "Register-EditorCommand" )
102
+ . AddParameter ( "Name" , commandName )
103
+ . AddParameter ( "DisplayName" , commandDisplayName )
104
+ . AddParameter ( "ScriptBlock" , ScriptBlock . Create ( "$global:extensionValue = 10" ) ) ,
105
+ CancellationToken . None ) . ConfigureAwait ( true ) ;
106
106
107
107
Assert . NotNull ( commandAdded ) ;
108
108
Assert . Equal ( commandName , commandAdded . Name ) ;
109
109
Assert . Equal ( commandDisplayName , commandAdded . DisplayName ) ;
110
110
111
111
// Invoke the command
112
- await _extensionCommandService .InvokeCommandAsync("test.scriptblock", editorContext);
112
+ await extensionCommandService . InvokeCommandAsync ( "test.scriptblock" , editorContext ) . ConfigureAwait ( true ) ;
113
113
114
114
// Assert the expected value
115
115
PSCommand psCommand = new PSCommand ( ) . AddScript ( "$global:extensionValue" ) ;
116
- IEnumerable<int> results = await _powershellContextService.ExecuteCommandAsync <int>(psCommand);
116
+ IEnumerable < int > results = await psesHost . ExecutePSCommandAsync < int > ( psCommand , CancellationToken . None ) . ConfigureAwait ( true ) ;
117
117
Assert . Equal ( 10 , results . FirstOrDefault ( ) ) ;
118
118
}
119
119
120
- [Trait("Category", "Extensions")]
121
120
[ Fact ]
122
121
public async Task CanUpdateRegisteredCommand ( )
123
122
{
124
123
EditorCommand updatedCommand = null ;
125
- _extensionCommandService.CommandUpdated += (object sender, EditorCommand command) =>
126
- {
127
- updatedCommand = command;
128
- };
124
+ extensionCommandService . CommandUpdated += ( object _ , EditorCommand command ) => updatedCommand = command ;
129
125
130
- string commandName = "test.function";
131
- string commandDisplayName = "Updated function extension";
126
+ const string commandName = "test.function" ;
127
+ const string commandDisplayName = "Updated function extension" ;
132
128
133
129
// Register a command and then update it
134
- await _powershellContextService.ExecuteScriptStringAsync(TestUtilities.NormalizeNewlines(
135
- "function Invoke-Extension { Write-Output \"Extension output!\" }\n" +
136
- $"Register-EditorCommand -Name \"{commandName}\" -DisplayName \"Old function extension\" -Function \"Invoke-Extension\"\n" +
137
- $"Register-EditorCommand -Name \"{commandName}\" -DisplayName \"{commandDisplayName}\" -Function \"Invoke-Extension\""));
130
+ await psesHost . ExecutePSCommandAsync (
131
+ new PSCommand ( ) . AddScript (
132
+ "function Invoke-Extension { Write-Output \" Extension output!\" }; " +
133
+ $ "Register-EditorCommand -Name { commandName } -DisplayName \" Old function extension\" -Function Invoke-Extension; " +
134
+ $ "Register-EditorCommand -Name { commandName } -DisplayName \" { commandDisplayName } \" -Function Invoke-Extension") ,
135
+ CancellationToken . None ) . ConfigureAwait ( true ) ;
138
136
139
137
// Wait for the add and update events
140
138
Assert . NotNull ( updatedCommand ) ;
141
139
Assert . Equal ( commandName , updatedCommand . Name ) ;
142
140
Assert . Equal ( commandDisplayName , updatedCommand . DisplayName ) ;
143
141
}
144
142
145
- [Trait("Category", "Extensions")]
146
143
[ Fact ]
147
144
public async Task CanUnregisterCommand ( )
148
145
{
@@ -154,41 +151,33 @@ public async Task CanUnregisterCommand()
154
151
new BufferPosition ( line : 1 , column : 1 ) ,
155
152
BufferRange . None ) ;
156
153
157
- string commandName = "test.scriptblock";
158
- string commandDisplayName = "ScriptBlock extension";
154
+ const string commandName = "test.scriptblock" ;
155
+ const string commandDisplayName = "ScriptBlock extension" ;
159
156
160
157
EditorCommand removedCommand = null ;
161
- _extensionCommandService.CommandRemoved += (object sender, EditorCommand command) =>
162
- {
163
- removedCommand = command;
164
- };
158
+ extensionCommandService . CommandRemoved += ( object _ , EditorCommand command ) => removedCommand = command ;
165
159
166
160
// Add the command and wait for the add event
167
- await _powershellContextService.ExecuteCommandAsync(new PSCommand()
168
- .AddCommand("Register-EditorCommand")
169
- .AddParameter("Name", commandName)
170
- .AddParameter("DisplayName", commandDisplayName)
171
- .AddParameter("ScriptBlock", ScriptBlock.Create("Write-Output \"Extension output!\"")));
161
+ await psesHost . ExecutePSCommandAsync (
162
+ new PSCommand ( )
163
+ . AddCommand ( "Register-EditorCommand" )
164
+ . AddParameter ( "Name" , commandName )
165
+ . AddParameter ( "DisplayName" , commandDisplayName )
166
+ . AddParameter ( "ScriptBlock" , ScriptBlock . Create ( "Write-Output \" Extension output!\" " ) ) ,
167
+ CancellationToken . None ) . ConfigureAwait ( true ) ;
172
168
173
169
// Remove the command and wait for the remove event
174
- await _powershellContextService.ExecuteCommandAsync(new PSCommand()
175
- .AddCommand("Unregister-EditorCommand")
176
- .AddParameter("Name", commandName) );
170
+ await psesHost . ExecutePSCommandAsync (
171
+ new PSCommand ( ) . AddCommand ( "Unregister-EditorCommand" ) . AddParameter ( "Name" , commandName ) ,
172
+ CancellationToken . None ) . ConfigureAwait ( true ) ;
177
173
178
174
Assert . NotNull ( removedCommand ) ;
179
175
Assert . Equal ( commandName , removedCommand . Name ) ;
180
176
Assert . Equal ( commandDisplayName , removedCommand . DisplayName ) ;
181
177
182
178
// Ensure that the command has been unregistered
183
- await Assert.ThrowsAsync<KeyNotFoundException>(() =>
184
- _extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext));
185
- }
186
-
187
- public void Dispose()
188
- {
189
- _powershellContextService.Dispose();
179
+ await Assert . ThrowsAsync < KeyNotFoundException > (
180
+ ( ) => extensionCommandService . InvokeCommandAsync ( "test.scriptblock" , editorContext ) ) . ConfigureAwait ( true ) ;
190
181
}
191
182
}
192
- */
193
183
}
194
-
0 commit comments