@@ -111,39 +111,6 @@ public async Task CanCancelExecutionWithMethod()
111111 Assert . True ( executeTask . IsCanceled ) ;
112112 }
113113
114- [ Fact ]
115- public async Task CanResolveAndLoadProfilesForHostId ( )
116- {
117- // Load the profiles for the test host name
118- await psesHost . LoadHostProfilesAsync ( CancellationToken . None ) . ConfigureAwait ( true ) ;
119-
120- // Ensure that the $PROFILE variable is a string with the value of CurrentUserCurrentHost.
121- IReadOnlyList < string > profileVariable = await psesHost . ExecutePSCommandAsync < string > (
122- new PSCommand ( ) . AddScript ( "$PROFILE" ) ,
123- CancellationToken . None ) . ConfigureAwait ( true ) ;
124-
125- Assert . Collection ( profileVariable ,
126- ( p ) => Assert . Equal ( PsesHostFactory . TestProfilePaths . CurrentUserCurrentHost , p ) ) ;
127-
128- // Ensure that all the profile paths are set in the correct note properties.
129- IReadOnlyList < string > profileProperties = await psesHost . ExecutePSCommandAsync < string > (
130- new PSCommand ( ) . AddScript ( "$PROFILE | Get-Member -Type NoteProperty" ) ,
131- CancellationToken . None ) . ConfigureAwait ( true ) ;
132-
133- Assert . Collection ( profileProperties ,
134- ( p ) => Assert . Equal ( $ "string AllUsersAllHosts={ PsesHostFactory . TestProfilePaths . AllUsersAllHosts } ", p , ignoreCase : true ) ,
135- ( p ) => Assert . Equal ( $ "string AllUsersCurrentHost={ PsesHostFactory . TestProfilePaths . AllUsersCurrentHost } ", p , ignoreCase : true ) ,
136- ( p ) => Assert . Equal ( $ "string CurrentUserAllHosts={ PsesHostFactory . TestProfilePaths . CurrentUserAllHosts } ", p , ignoreCase : true ) ,
137- ( p ) => Assert . Equal ( $ "string CurrentUserCurrentHost={ PsesHostFactory . TestProfilePaths . CurrentUserCurrentHost } ", p , ignoreCase : true ) ) ;
138-
139- // Ensure that the profile was loaded. The profile also checks that $PROFILE was defined.
140- IReadOnlyList < bool > profileLoaded = await psesHost . ExecutePSCommandAsync < bool > (
141- new PSCommand ( ) . AddScript ( "Assert-ProfileLoaded" ) ,
142- CancellationToken . None ) . ConfigureAwait ( true ) ;
143-
144- Assert . Collection ( profileLoaded , Assert . True ) ;
145- }
146-
147114 [ Fact ]
148115 public async Task CanHandleNoProfiles ( )
149116 {
@@ -202,6 +169,35 @@ public async Task CanHandleUndefinedPrompt()
202169 Assert . Equal ( PsesInternalHost . DefaultPrompt , prompt ) ;
203170 }
204171
172+ [ Fact ]
173+ public async Task CanRunOnIdleTask ( )
174+ {
175+ IReadOnlyList < PSObject > task = await psesHost . ExecutePSCommandAsync < PSObject > (
176+ new PSCommand ( ) . AddScript ( "$handled = $false; Register-EngineEvent -SourceIdentifier PowerShell.OnIdle -MaxTriggerCount 1 -Action { $global:handled = $true }" ) ,
177+ CancellationToken . None ) . ConfigureAwait ( true ) ;
178+
179+ IReadOnlyList < bool > handled = await psesHost . ExecutePSCommandAsync < bool > (
180+ new PSCommand ( ) . AddScript ( "$handled" ) ,
181+ CancellationToken . None ) . ConfigureAwait ( true ) ;
182+
183+ Assert . Collection ( handled , ( p ) => Assert . False ( p ) ) ;
184+
185+ await psesHost . ExecuteDelegateAsync (
186+ nameof ( psesHost . OnPowerShellIdle ) ,
187+ executionOptions : null ,
188+ ( _ , _ ) => psesHost . OnPowerShellIdle ( CancellationToken . None ) ,
189+ CancellationToken . None ) . ConfigureAwait ( true ) ;
190+
191+ // TODO: Why is this racy?
192+ Thread . Sleep ( 2000 ) ;
193+
194+ handled = await psesHost . ExecutePSCommandAsync < bool > (
195+ new PSCommand ( ) . AddScript ( "$handled" ) ,
196+ CancellationToken . None ) . ConfigureAwait ( true ) ;
197+
198+ Assert . Collection ( handled , ( p ) => Assert . True ( p ) ) ;
199+ }
200+
205201 [ Fact ]
206202 public async Task CanLoadPSReadLine ( )
207203 {
@@ -240,4 +236,72 @@ public async Task CanHandleBadInitialWorkingDirectory(string path)
240236 Assert . Collection ( getLocation , ( d ) => Assert . Equal ( cwd , d , ignoreCase : true ) ) ;
241237 }
242238 }
239+
240+ [ Trait ( "Category" , "PsesInternalHost" ) ]
241+ public class PsesInternalHostWithProfileTests : IDisposable
242+ {
243+ private readonly PsesInternalHost psesHost ;
244+
245+ public PsesInternalHostWithProfileTests ( ) => psesHost = PsesHostFactory . Create ( NullLoggerFactory . Instance , loadProfiles : true ) ;
246+
247+ public void Dispose ( )
248+ {
249+ #pragma warning disable VSTHRD002
250+ psesHost . StopAsync ( ) . Wait ( ) ;
251+ #pragma warning restore VSTHRD002
252+ GC . SuppressFinalize ( this ) ;
253+ }
254+
255+ [ Fact ]
256+ public async Task CanResolveAndLoadProfilesForHostId ( )
257+ {
258+ // Ensure that the $PROFILE variable is a string with the value of CurrentUserCurrentHost.
259+ IReadOnlyList < string > profileVariable = await psesHost . ExecutePSCommandAsync < string > (
260+ new PSCommand ( ) . AddScript ( "$PROFILE" ) ,
261+ CancellationToken . None ) . ConfigureAwait ( true ) ;
262+
263+ Assert . Collection ( profileVariable ,
264+ ( p ) => Assert . Equal ( PsesHostFactory . TestProfilePaths . CurrentUserCurrentHost , p ) ) ;
265+
266+ // Ensure that all the profile paths are set in the correct note properties.
267+ IReadOnlyList < string > profileProperties = await psesHost . ExecutePSCommandAsync < string > (
268+ new PSCommand ( ) . AddScript ( "$PROFILE | Get-Member -Type NoteProperty" ) ,
269+ CancellationToken . None ) . ConfigureAwait ( true ) ;
270+
271+ Assert . Collection ( profileProperties ,
272+ ( p ) => Assert . Equal ( $ "string AllUsersAllHosts={ PsesHostFactory . TestProfilePaths . AllUsersAllHosts } ", p , ignoreCase : true ) ,
273+ ( p ) => Assert . Equal ( $ "string AllUsersCurrentHost={ PsesHostFactory . TestProfilePaths . AllUsersCurrentHost } ", p , ignoreCase : true ) ,
274+ ( p ) => Assert . Equal ( $ "string CurrentUserAllHosts={ PsesHostFactory . TestProfilePaths . CurrentUserAllHosts } ", p , ignoreCase : true ) ,
275+ ( p ) => Assert . Equal ( $ "string CurrentUserCurrentHost={ PsesHostFactory . TestProfilePaths . CurrentUserCurrentHost } ", p , ignoreCase : true ) ) ;
276+
277+ // Ensure that the profile was loaded. The profile also checks that $PROFILE was defined.
278+ IReadOnlyList < bool > profileLoaded = await psesHost . ExecutePSCommandAsync < bool > (
279+ new PSCommand ( ) . AddScript ( "Assert-ProfileLoaded" ) ,
280+ CancellationToken . None ) . ConfigureAwait ( true ) ;
281+
282+ Assert . Collection ( profileLoaded , Assert . True ) ;
283+ }
284+
285+ // This test specifically relies on a handler registered in the test profile, and on the
286+ // test host loading the profiles during startup, that way the pipeline timing is
287+ // consistent.
288+ [ Fact ]
289+ public async Task CanRunOnIdleInProfileTask ( )
290+ {
291+ await psesHost . ExecuteDelegateAsync (
292+ nameof ( psesHost . OnPowerShellIdle ) ,
293+ executionOptions : null ,
294+ ( _ , _ ) => psesHost . OnPowerShellIdle ( CancellationToken . None ) ,
295+ CancellationToken . None ) . ConfigureAwait ( true ) ;
296+
297+ // TODO: Why is this racy?
298+ Thread . Sleep ( 2000 ) ;
299+
300+ IReadOnlyList < bool > handled = await psesHost . ExecutePSCommandAsync < bool > (
301+ new PSCommand ( ) . AddScript ( "$handledInProfile" ) ,
302+ CancellationToken . None ) . ConfigureAwait ( true ) ;
303+
304+ Assert . Collection ( handled , ( p ) => Assert . True ( p ) ) ;
305+ }
306+ }
243307}
0 commit comments