Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -622,15 +622,18 @@ public override PSHostUserInterface UI
/// <summary>
/// See base class.
/// </summary>
public void PushRunspace(Runspace newRunspace)
public void PushRunspace(Runspace runspace)
{
if (_runspaceRef == null)
{
return;
}

RemoteRunspace remoteRunspace = newRunspace as RemoteRunspace;
Dbg.Assert(remoteRunspace != null, "Expected remoteRunspace != null");
if (runspace is not RemoteRunspace remoteRunspace)
{
throw new ArgumentException(ConsoleHostStrings.PushRunspaceNotRemote, nameof(runspace));
}

remoteRunspace.StateChanged += HandleRemoteRunspaceStateChanged;

// Unsubscribe the local session debugger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ public interface IHostSupportsInteractiveSession
/// <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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is API comment I'd explicitly point it should be RemoteRunspace.

@jborean93 jborean93 Sep 2, 2024

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RemoteRunspace is not a public class so I can't really reference it in the API docs.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be clearer, but I don't insist.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll leave it off unless others also prefer it in there. I'm unsure how the docs would actually be rendered if a non-public type was referenced here.

/// </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")]
Expand Down
15 changes: 15 additions & 0 deletions test/powershell/Host/HostUtilities.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}