11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
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 ;
104using System ;
115using System . Collections . Generic ;
126using System . Linq ;
137using System . Management . Automation ;
8+ using System . Threading ;
149using 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 ;
1517using Xunit ;
1618
1719namespace Microsoft . PowerShell . EditorServices . Test . Extensions
1820{
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" ) ]
2422 public class ExtensionCommandTests : IDisposable
2523 {
26- private readonly PowerShellContextService _powershellContextService;
27-
28- private readonly IExtensionCommandService _extensionCommandService;
24+ private readonly PsesInternalHost psesHost ;
2925
30- private readonly ExtensionService _extensionService ;
26+ private readonly ExtensionCommandService extensionCommandService ;
3127
3228 public ExtensionCommandTests ( )
3329 {
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 ) ;
3644 }
3745
38- [Trait("Category", "Extensions")]
3946 [ Fact ]
4047 public async Task CanRegisterAndInvokeCommandWithCmdletName ( )
4148 {
@@ -48,34 +55,30 @@ public async Task CanRegisterAndInvokeCommandWithCmdletName()
4855 BufferRange . None ) ;
4956
5057 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 ;
5859
60+ const string commandName = "test.function" ;
61+ const string commandDisplayName = "Function extension" ;
5962
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 ) ;
6468
6569 Assert . NotNull ( commandAdded ) ;
6670 Assert . Equal ( commandAdded . Name , commandName ) ;
6771 Assert . Equal ( commandAdded . DisplayName , commandDisplayName ) ;
6872
6973 // Invoke the command
70- await _extensionCommandService .InvokeCommandAsync(commandName, editorContext);
74+ await extensionCommandService . InvokeCommandAsync ( commandName , editorContext ) . ConfigureAwait ( true ) ;
7175
7276 // Assert the expected value
7377 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 ) ;
7579 Assert . Equal ( 5 , results . FirstOrDefault ( ) ) ;
7680 }
7781
78- [Trait("Category", "Extensions")]
7982 [ Fact ]
8083 public async Task CanRegisterAndInvokeCommandWithScriptBlock ( )
8184 {
@@ -87,62 +90,56 @@ public async Task CanRegisterAndInvokeCommandWithScriptBlock()
8790 new BufferPosition ( line : 1 , column : 1 ) ,
8891 BufferRange . None ) ;
8992
90-
9193 EditorCommand commandAdded = null ;
92- _extensionCommandService.CommandAdded += (object sender, EditorCommand command) =>
93- {
94- commandAdded = command;
95- };
94+ extensionCommandService . CommandAdded += ( object _ , EditorCommand command ) => commandAdded = command ;
9695
96+ const string commandName = "test.scriptblock" ;
97+ const string commandDisplayName = "ScriptBlock extension" ;
9798
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 ) ;
106106
107107 Assert . NotNull ( commandAdded ) ;
108108 Assert . Equal ( commandName , commandAdded . Name ) ;
109109 Assert . Equal ( commandDisplayName , commandAdded . DisplayName ) ;
110110
111111 // Invoke the command
112- await _extensionCommandService .InvokeCommandAsync("test.scriptblock", editorContext);
112+ await extensionCommandService . InvokeCommandAsync ( "test.scriptblock" , editorContext ) . ConfigureAwait ( true ) ;
113113
114114 // Assert the expected value
115115 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 ) ;
117117 Assert . Equal ( 10 , results . FirstOrDefault ( ) ) ;
118118 }
119119
120- [Trait("Category", "Extensions")]
121120 [ Fact ]
122121 public async Task CanUpdateRegisteredCommand ( )
123122 {
124123 EditorCommand updatedCommand = null ;
125- _extensionCommandService.CommandUpdated += (object sender, EditorCommand command) =>
126- {
127- updatedCommand = command;
128- };
124+ extensionCommandService . CommandUpdated += ( object _ , EditorCommand command ) => updatedCommand = command ;
129125
130- string commandName = "test.function";
131- string commandDisplayName = "Updated function extension";
126+ const string commandName = "test.function" ;
127+ const string commandDisplayName = "Updated function extension" ;
132128
133129 // 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 ) ;
138136
139137 // Wait for the add and update events
140138 Assert . NotNull ( updatedCommand ) ;
141139 Assert . Equal ( commandName , updatedCommand . Name ) ;
142140 Assert . Equal ( commandDisplayName , updatedCommand . DisplayName ) ;
143141 }
144142
145- [Trait("Category", "Extensions")]
146143 [ Fact ]
147144 public async Task CanUnregisterCommand ( )
148145 {
@@ -154,41 +151,33 @@ public async Task CanUnregisterCommand()
154151 new BufferPosition ( line : 1 , column : 1 ) ,
155152 BufferRange . None ) ;
156153
157- string commandName = "test.scriptblock";
158- string commandDisplayName = "ScriptBlock extension";
154+ const string commandName = "test.scriptblock" ;
155+ const string commandDisplayName = "ScriptBlock extension" ;
159156
160157 EditorCommand removedCommand = null ;
161- _extensionCommandService.CommandRemoved += (object sender, EditorCommand command) =>
162- {
163- removedCommand = command;
164- };
158+ extensionCommandService . CommandRemoved += ( object _ , EditorCommand command ) => removedCommand = command ;
165159
166160 // 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 ) ;
172168
173169 // 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 ) ;
177173
178174 Assert . NotNull ( removedCommand ) ;
179175 Assert . Equal ( commandName , removedCommand . Name ) ;
180176 Assert . Equal ( commandDisplayName , removedCommand . DisplayName ) ;
181177
182178 // 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 ) ;
190181 }
191182 }
192- */
193183}
194-
0 commit comments