mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add remote runspace check for PushRunspace
Add explicit exception when attempting to call PushRunspace on a runspace that is not remote. Instead of failing with a hard to understand NullReferenceException an ArgumentException is raised with information about what went wrong.
This commit is contained in:
committed by
Patrick Meinecke
parent
b18eacaa83
commit
82341b0e3b
@@ -622,14 +622,19 @@ namespace Microsoft.PowerShell
|
||||
/// <summary>
|
||||
/// See base class.
|
||||
/// </summary>
|
||||
public void PushRunspace(Runspace newRunspace)
|
||||
public void PushRunspace(Runspace runspace)
|
||||
{
|
||||
if (_runspaceRef == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RemoteRunspace remoteRunspace = newRunspace as RemoteRunspace;
|
||||
RemoteRunspace remoteRunspace = runspace as RemoteRunspace;
|
||||
if (remoteRunspace is null)
|
||||
{
|
||||
throw new ArgumentException(ConsoleHostStrings.PushRunspaceNotRemote, nameof(runspace));
|
||||
}
|
||||
|
||||
Dbg.Assert(remoteRunspace != null, "Expected remoteRunspace != null");
|
||||
remoteRunspace.StateChanged += HandleRemoteRunspaceStateChanged;
|
||||
|
||||
|
||||
@@ -182,4 +182,7 @@ The current session does not support debugging; execution will continue.
|
||||
<data name="RunAsAdministrator" xml:space="preserve">
|
||||
<value>Run as Administrator</value>
|
||||
</data>
|
||||
<data name="PushRunspaceNotRemote" xml:space="preserve">
|
||||
<value>PushRunspace can only push a remote runspace.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
||||
@@ -296,6 +296,11 @@ namespace System.Management.Automation.Host
|
||||
/// <summary>
|
||||
/// Called by the engine to notify the host that a runspace push has been requested.
|
||||
/// </summary>
|
||||
/// <param name="runspace">
|
||||
/// The runspace to push. This runspace must be a remote runspace and
|
||||
/// not a locally created runspace.
|
||||
/// </param>
|
||||
/// <exception cref="ArgumentException">The specified runspace is not a remote runspace.</exception>
|
||||
/// <seealso cref="System.Management.Automation.Host.IHostSupportsInteractiveSession.PushRunspace"/>
|
||||
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Runspace")]
|
||||
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "runspace")]
|
||||
|
||||
@@ -81,3 +81,18 @@ Describe 'PromptForCredential' -Tags "CI" {
|
||||
$out.UserName | Should -BeExactly 'myDomain\myUser'
|
||||
}
|
||||
}
|
||||
|
||||
Describe 'PushRunspaceLocalFailure' -Tags 'CI' {
|
||||
It 'Should throw an exception when pushing a local runspace' {
|
||||
$runspace = [RunspaceFactory]::CreateRunspace()
|
||||
try {
|
||||
$runspace.Open()
|
||||
$exc = { $Host.PushRunspace($runspace) } | Should -Throw -PassThru
|
||||
$exc.Exception.InnerException | Should -BeOfType ([System.ArgumentException])
|
||||
[string]$exc | Should -BeLike "*PushRunspace can only push a remote runspace. (Parameter 'runspace')*"
|
||||
}
|
||||
finally {
|
||||
$runspace.Dispose()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user