@@ -79,7 +79,7 @@ static PowerShellContextService()
79
79
{
80
80
MethodInfo setterInfo = typeof ( Runspace ) . GetProperty ( "ApartmentState" ) . GetSetMethod ( ) ;
81
81
Delegate setter = Delegate . CreateDelegate ( typeof ( Action < Runspace , ApartmentState > ) , firstArgument : null , method : setterInfo ) ;
82
- s_runspaceApartmentStateSetter = ( Action < Runspace , ApartmentState > ) setter ;
82
+ s_runspaceApartmentStateSetter = ( Action < Runspace , ApartmentState > ) setter ;
83
83
}
84
84
85
85
if ( VersionUtils . IsPS7OrGreater )
@@ -242,7 +242,7 @@ HostStartupInfo hostStartupInfo
242
242
/// <param name="hostUserInterface">The EditorServicesPSHostUserInterface to use for this instance.</param>
243
243
/// <param name="logger">An ILogger implementation to use for this instance.</param>
244
244
/// <returns></returns>
245
- public static Runspace CreateRunspace (
245
+ public static Runspace CreateTestRunspace (
246
246
HostStartupInfo hostDetails ,
247
247
PowerShellContextService powerShellContext ,
248
248
EditorServicesPSHostUserInterface hostUserInterface ,
@@ -262,21 +262,8 @@ public static Runspace CreateRunspace(
262
262
/// <param name="psHost">The PSHost that will be used for this Runspace.</param>
263
263
/// <param name="languageMode">The language mode inherited from the orginal PowerShell process. This will be used when creating runspaces so that we honor the same language mode.</param>
264
264
/// <returns></returns>
265
- public static Runspace CreateRunspace ( PSHost psHost , InitialSessionState initialSessionState , List < string > additionalModules = null )
265
+ public static Runspace CreateTestRunspace ( PSHost psHost , InitialSessionState initialSessionState , List < string > additionalModules = null )
266
266
{
267
- if ( Environment . GetEnvironmentVariable ( "PSES_TEST_USE_CREATE_DEFAULT" ) == "1" ) {
268
- initialSessionState = InitialSessionState . CreateDefault ( ) ;
269
- }
270
-
271
- // We set the process scope's execution policy (which is really the runspace's scope) to
272
- // Bypass so we can import our bundled modules. This is equivalent in scope to the CLI
273
- // argument `-Bypass`, which (for instance) the extension passes. Thus we emulate this
274
- // behavior for consistency such that unit tests can pass in a similar environment.
275
- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
276
- {
277
- initialSessionState . ExecutionPolicy = ExecutionPolicy . Bypass ;
278
- }
279
-
280
267
Runspace runspace = RunspaceFactory . CreateRunspace ( psHost , initialSessionState ) ;
281
268
282
269
// Windows PowerShell must be hosted in STA mode
@@ -534,6 +521,20 @@ bool ownsInitialRunspace
534
521
{
535
522
this . PromptContext = new LegacyReadLineContext ( this ) ;
536
523
}
524
+ }
525
+
526
+ /// <summary>
527
+ /// Imports the PowerShellEditorServices.Commands module into
528
+ /// the runspace. This method will be moved somewhere else soon.
529
+ /// </summary>
530
+ /// <returns></returns>
531
+ public Task ImportCommandsModuleAsync ( )
532
+ {
533
+ this . logger . LogTrace ( $ "Importing PowershellEditorServices commands from { s_commandsModulePath } ") ;
534
+
535
+ PSCommand importCommand = new PSCommand ( )
536
+ . AddCommand ( "Import-Module" )
537
+ . AddArgument ( s_commandsModulePath ) ;
537
538
538
539
if ( VersionUtils . IsWindows && initialRunspace . InitialSessionState . LanguageMode == PSLanguageMode . FullLanguage )
539
540
{
@@ -2262,7 +2263,7 @@ private static string GetStringForPSCommand(PSCommand psCommand)
2262
2263
return stringBuilder . ToString ( ) ;
2263
2264
}
2264
2265
2265
- private void SetExecutionPolicy ( )
2266
+ internal void RestoreExecutionPolicy ( )
2266
2267
{
2267
2268
this . logger . LogTrace ( "Setting execution policy..." ) ;
2268
2269
@@ -2288,14 +2289,14 @@ private void SetExecutionPolicy()
2288
2289
// set to expected values, so we must sift through those.
2289
2290
2290
2291
ExecutionPolicy policyToSet = ExecutionPolicy . Bypass ;
2291
- var currentUserPolicy = ( ExecutionPolicy ) policies [ policies . Count - 2 ] . Members [ "ExecutionPolicy" ] . Value ;
2292
+ var currentUserPolicy = ( ExecutionPolicy ) policies [ policies . Count - 2 ] . Members [ "ExecutionPolicy" ] . Value ;
2292
2293
if ( currentUserPolicy != ExecutionPolicy . Undefined )
2293
2294
{
2294
2295
policyToSet = currentUserPolicy ;
2295
2296
}
2296
2297
else
2297
2298
{
2298
- var localMachinePolicy = ( ExecutionPolicy ) policies [ policies . Count - 1 ] . Members [ "ExecutionPolicy" ] . Value ;
2299
+ var localMachinePolicy = ( ExecutionPolicy ) policies [ policies . Count - 1 ] . Members [ "ExecutionPolicy" ] . Value ;
2299
2300
if ( localMachinePolicy != ExecutionPolicy . Undefined )
2300
2301
{
2301
2302
policyToSet = localMachinePolicy ;
@@ -2467,7 +2468,7 @@ private void HandleRunspaceStateChanged(object sender, RunspaceStateEventArgs ar
2467
2468
case RunspaceState . Closed :
2468
2469
case RunspaceState . Broken :
2469
2470
// If the runspace closes or fails, pop the runspace
2470
- ( ( IHostSupportsInteractiveSession ) this ) . PopRunspace ( ) ;
2471
+ ( ( IHostSupportsInteractiveSession ) this ) . PopRunspace ( ) ;
2471
2472
break ;
2472
2473
}
2473
2474
}
@@ -2479,7 +2480,7 @@ private static IEnumerable<string> GetLoadableProfilePaths(ProfilePathInfo profi
2479
2480
yield break ;
2480
2481
}
2481
2482
2482
- foreach ( string path in new [ ] { profilePaths . AllUsersAllHosts , profilePaths . AllUsersCurrentHost , profilePaths . CurrentUserAllHosts , profilePaths . CurrentUserCurrentHost } )
2483
+ foreach ( string path in new [ ] { profilePaths . AllUsersAllHosts , profilePaths . AllUsersCurrentHost , profilePaths . CurrentUserAllHosts , profilePaths . CurrentUserCurrentHost } )
2483
2484
{
2484
2485
if ( path != null && File . Exists ( path ) )
2485
2486
{
@@ -2511,12 +2512,12 @@ private void StartCommandLoopOnRunspaceAvailable()
2511
2512
void availabilityChangedHandler ( object runspace , RunspaceAvailabilityEventArgs eventArgs )
2512
2513
{
2513
2514
if ( eventArgs . RunspaceAvailability != RunspaceAvailability . Available ||
2514
- this . versionSpecificOperations . IsDebuggerStopped ( this . PromptNest , ( Runspace ) runspace ) )
2515
+ this . versionSpecificOperations . IsDebuggerStopped ( this . PromptNest , ( Runspace ) runspace ) )
2515
2516
{
2516
2517
return ;
2517
2518
}
2518
2519
2519
- ( ( Runspace ) runspace ) . AvailabilityChanged -= availabilityChangedHandler ;
2520
+ ( ( Runspace ) runspace ) . AvailabilityChanged -= availabilityChangedHandler ;
2520
2521
Interlocked . Exchange ( ref this . isCommandLoopRestarterSet , 0 ) ;
2521
2522
this . ConsoleReader ? . StartCommandLoop ( ) ;
2522
2523
}
0 commit comments