Skip to content

Add the host support to the PowerShell kernel #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 3, 2020
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
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Linq;
using System.Threading.Tasks;
using Clockwise;
Expand Down Expand Up @@ -306,8 +307,8 @@ public async Task sends_ExecuteReply_message_when_submission_contains_only_a_dir

[Theory]
[InlineData("input()", "", "input-value")]
[InlineData("input(\"User:\")", "User:", "user name")]
public async Task sends_InputRequest_message_when_submission_requests_user_input(string code, string prompt, string expectedDisplayValue)
[InlineData("input(\"User: \")", "User: ", "user name")]
public async Task sends_InputRequest_message_when_submission_requests_user_input_in_csharp(string code, string prompt, string expectedDisplayValue)
{
var scheduler = CreateScheduler();
var request = ZeroMQMessage.Create(new ExecuteRequest(code));
Expand All @@ -323,6 +324,27 @@ public async Task sends_InputRequest_message_when_submission_requests_user_input
.Contain(dp => dp.Data["text/plain"] as string == expectedDisplayValue);
}

[Theory]
[InlineData("Read-Host", "", "input-value")]
[InlineData("Read-Host -Prompt User", "User: ", "user name")]
public async Task sends_InputRequest_message_when_submission_requests_user_input_in_powershell(string code, string prompt, string expectedDisplayValue)
{
SetKernelLanguage(Language.PowerShell);

var scheduler = CreateScheduler();
var request = ZeroMQMessage.Create(new ExecuteRequest(code));
var context = new JupyterRequestContext(JupyterMessageSender, request);
await scheduler.Schedule(context);

await context.Done().Timeout(20.Seconds());

JupyterMessageSender.RequestMessages.Should().Contain(r => r.Prompt == prompt && r.Password == false);
JupyterMessageSender.PubSubMessages
.OfType<Stream>()
.Should()
.Contain(s => s.Name == Stream.StandardOutput && s.Text == (expectedDisplayValue + Environment.NewLine));
}

[Fact]
public async Task password_input_should_not_appear_in_diagnostic_logs()
{
Expand All @@ -342,7 +364,7 @@ public async Task password_input_should_not_appear_in_diagnostic_logs()
[Theory]
[InlineData("password()", "")]
[InlineData("password(\"Type your password:\")", "Type your password:")]
public async Task sends_InputRequest_message_when_submission_requests_user_password(string code, string prompt)
public async Task sends_InputRequest_message_when_submission_requests_user_password_in_csharp(string code, string prompt)
{
var scheduler = CreateScheduler();
var request = ZeroMQMessage.Create(new ExecuteRequest(code));
Expand All @@ -358,6 +380,27 @@ public async Task sends_InputRequest_message_when_submission_requests_user_passw
.Contain(dp => dp.Data["text/html"] as string == $"{typeof(PasswordString).FullName}");
}

[Theory]
[InlineData("Read-Host -AsSecureString", "")]
[InlineData("Read-Host -Prompt 'Type your password' -AsSecureString", "Type your password: ")]
public async Task sends_InputRequest_message_when_submission_requests_user_password_in_powershell(string code, string prompt)
{
SetKernelLanguage(Language.PowerShell);

var scheduler = CreateScheduler();
var request = ZeroMQMessage.Create(new ExecuteRequest(code));
var context = new JupyterRequestContext(JupyterMessageSender, request);
await scheduler.Schedule(context);

await context.Done().Timeout(20.Seconds());

JupyterMessageSender.RequestMessages.Should().Contain(r => r.Prompt == prompt && r.Password == true);
JupyterMessageSender.PubSubMessages
.OfType<Stream>()
.Should()
.Contain(s => s.Name == Stream.StandardOutput && s.Text == $"System.Security.SecureString{Environment.NewLine}");
}

[Fact]
public async Task Shows_not_supported_exception_when_stdin_not_allowed_and_input_is_requested()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Clockwise;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.FSharp;
using Microsoft.DotNet.Interactive.PowerShell;
using Microsoft.DotNet.Interactive.Jupyter.Protocol;
using Microsoft.DotNet.Interactive.Tests;
using Pocket;
Expand All @@ -18,6 +19,7 @@ public abstract class JupyterRequestHandlerTestBase<T> : IDisposable
private readonly CompositeDisposable _disposables = new CompositeDisposable();
private readonly CSharpKernel _cSharpKernel;
private readonly FSharpKernel _fSharpKernel;
private readonly PowerShellKernel _psKernel;
private readonly CompositeKernel _compositeKernel;
private readonly JupyterFrontendEnvironment _frontendEnvironment;

Expand All @@ -41,10 +43,14 @@ protected JupyterRequestHandlerTestBase(ITestOutputHelper output)
.UseDefaultNamespaces()
.UseMathAndLaTeX();

_psKernel = new PowerShellKernel()
.UseJupyterHelpers();

_compositeKernel = new CompositeKernel
{
_cSharpKernel,
_fSharpKernel
_fSharpKernel,
_psKernel
}
.UseDefaultMagicCommands();

Expand All @@ -70,6 +76,9 @@ protected void SetKernelLanguage(Language language)
case Language.FSharp:
_compositeKernel.DefaultKernelName = _fSharpKernel.Name;
break;
case Language.PowerShell:
_compositeKernel.DefaultKernelName = _psKernel.Name;
break;
default:
throw new ArgumentOutOfRangeException(nameof(language), language, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public string Send(InputRequest message)

return message.Prompt switch
{
"User:" => "user name",
"Password:" => "secret",
"User: " => "user name",
_ => "input-value"
};
}
Expand Down
8 changes: 8 additions & 0 deletions Microsoft.DotNet.Interactive.Jupyter/KernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.PowerShell;
using Microsoft.DotNet.Interactive.Formatting;
using static Microsoft.DotNet.Interactive.Kernel;
using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags;
Expand Down Expand Up @@ -43,7 +44,14 @@ public static CSharpKernel UseJupyterHelpers(
");

kernel.DeferCommand(command);
return kernel;
}

public static PowerShellKernel UseJupyterHelpers(
this PowerShellKernel kernel)
{
kernel.ReadInput = Kernel.input;
kernel.ReadPassword = Kernel.password;
return kernel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Clockwise" Version="1.0.261-beta" />
<PackageReference Include="Markdig" Version="0.17.1" />
<PackageReference Include="Markdig.Signed" Version="0.18.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.0" />
<PackageReference Include="NetMQ" Version="4.0.0.207" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
Expand All @@ -19,12 +19,13 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.5.1" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.6.0" />
<PackageReference Include="System.Reactive" Version="4.3.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Microsoft.DotNet.Interactive.CSharp\Microsoft.DotNet.Interactive.CSharp.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.FSharp\Microsoft.DotNet.Interactive.FSharp.fsproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.PowerShell\Microsoft.DotNet.Interactive.PowerShell.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive\Microsoft.DotNet.Interactive.csproj" />
</ItemGroup>
</Project>
17 changes: 0 additions & 17 deletions Microsoft.DotNet.Interactive.PowerShell/Commands/CommandUtils.cs

This file was deleted.

This file was deleted.

Loading