public async Task When_SubmitCode_command_adds_packages_to_csharp_kernel_then_the_submission_is_not_passed_to_csharpScript() { var cSharpKernel = new CSharpKernel(); using var events = cSharpKernel.KernelEvents.ToSubscribedList(); var kernel = new CompositeKernel { cSharpKernel.UseNugetDirective() }; var command = new SubmitCode("#r \"nuget:Microsoft.ML, 1.3.1\" \nvar a = new List<int>();"); await kernel.SendAsync(command); events .OfType <CodeSubmissionReceived>() .Should() .NotContain(e => e.Code.Contains("#r")); }
public async Task javascript_kernel_forwards_commands_to_frontend() { var client = new TestClient(); using var kernel = new CompositeKernel { new JavaScriptKernel(client) }; kernel.FindKernel(JavaScriptKernel.DefaultKernelName).RegisterCommandType <CustomCommand>(); using var events = kernel.KernelEvents.ToSubscribedList(); var command = new CustomCommand(JavaScriptKernel.DefaultKernelName); await kernel.SendAsync(command, CancellationToken.None); client.ForwardedCommands.Should().Contain(command); }
public async Task When_an_unrecognized_directive_is_encountered_an_error_is_produced() { using var kernel = new CompositeKernel { new CSharpKernel() }; using var events = kernel.KernelEvents.ToSubscribedList(); await kernel.SubmitCodeAsync("#!oops"); events .Should() .ContainSingle <CommandFailed>() .Which .Message .Should() .Contain("Unrecognized command or argument '#!oops'"); }
public async Task When_SubmitCode_command_adds_packages_to_csharp_kernel_then_CodeSubmissionReceived_is_published() { var kernel = new CompositeKernel { new CSharpKernel().UseNugetDirective() }; var command = new SubmitCode("#r \"nuget:Microsoft.Extensions.Logging, 2.2.0\" \nMicrosoft.Extensions.Logging.ILogger logger = null;"); var result = await kernel.SendAsync(command); using var events = result.KernelEvents.ToSubscribedList(); events.OfType <CodeSubmissionReceived>() .First() .Code .Should() .Be(command.Code); }
private static async Task RunProfiledOperation( Operation operation, CompositeKernel kernel) { switch (operation) { case Operation.SubmitCode: var submitCode = CreateSubmitCode(kernel); await kernel.SendAsync(submitCode); break; case Operation.RequestCompletion: var requestCompletion = CreateRequestCompletion(kernel); await kernel.SendAsync(requestCompletion); break; } }
public async Task it_registers_formatters() { using var kernel = new CompositeKernel(); var kernelExtension = new SandDanceKernelExtension(); await kernelExtension.OnLoadAsync(kernel); var data = new[] { new { Type = "orange", Price = 1.2 }, new { Type = "apple", Price = 1.3 }, new { Type = "grape", Price = 1.4 } }; var formatted = data.ExploreWithSandDance().ToDisplayString(HtmlFormatter.MimeType); formatted.Should().Contain("configureRequireFromExtension('SandDance','1.0.0')(['SandDance/sanddanceapi'], (sandDance) => {"); }
public async Task When_SubmitCode_command_adds_packages_to_csharp_kernel_then_PackageAdded_event_is_raised() { var kernel = new CompositeKernel { new CSharpKernel().UseNugetDirective() }; var command = new SubmitCode("#r \"nuget:Microsoft.Extensions.Logging, 2.2.0\" \nMicrosoft.Extensions.Logging.ILogger logger = null;"); var result = await kernel.SendAsync(command); using var events = result.KernelEvents.ToSubscribedList(); events .First() .Should() .Match(e => e is DisplayedValueProduced && ((DisplayedValueProduced)e).Value.ToString().Contains("Installing")); events .Should() .Contain(e => e is DisplayedValueUpdated); events .Should() .ContainSingle(e => e is NuGetPackageAdded); events.OfType <NuGetPackageAdded>() .Single() .PackageReference .Should() .BeEquivalentTo(new NugetPackageReference("Microsoft.Extensions.Logging", "2.2.0")); events .Should() .ContainSingle(e => e is CommandHandled && e.As <CommandHandled>().Command is AddNugetPackage); events .Should() .ContainSingle(e => e is CommandHandled && e.As <CommandHandled>().Command is SubmitCode); }
public async Task can_address_remote_composite_kernel_using_named_pipe() { using var localCompositeKernel = new CompositeKernel { new FSharpKernel() }.UseKernelClientConnection(new ConnectNamedPipe()); localCompositeKernel.DefaultKernelName = "fsharp"; var pipeName = Guid.NewGuid().ToString(); var remoteDefaultKernelInvoked = false; using var remoteCompositeKernel = new CompositeKernel { new FakeKernel("csharp") { Handle = (command, context) => { remoteDefaultKernelInvoked = true; return Task.CompletedTask; } }, new FakeKernel("powershell") }; remoteCompositeKernel.DefaultKernelName = "csharp"; StartServer(remoteCompositeKernel, pipeName); using var events = localCompositeKernel.KernelEvents.ToSubscribedList(); var connectToRemoteKernel = new SubmitCode($"#!connect named-pipe --kernel-name newKernelName --pipe-name {pipeName}"); var codeSubmissionForRemoteKernel = new SubmitCode(@" #!newKernelName var x = 1 + 1; x"); await localCompositeKernel.SendAsync(connectToRemoteKernel); await localCompositeKernel.SendAsync(codeSubmissionForRemoteKernel); remoteDefaultKernelInvoked.Should() .BeTrue(); }
public async Task cell_with_nuget_and_code_continues_executions_on_right_kernel() { var kernel = new CompositeKernel { new CSharpKernel() .UseDefaultFormatting() .UseNugetDirective() .UseKernelHelpers() .UseWho() .LogEventsToPocketLogger(), new FSharpKernel() .UseDefaultFormatting() .UseKernelHelpers() .UseWho() .UseDefaultNamespaces() .LogEventsToPocketLogger() } .UseDefaultMagicCommands(); kernel.DefaultKernelName = "csharp"; var events = kernel.KernelEvents.ToSubscribedList(); DisposeAfterTest(events); DisposeAfterTest(kernel); var command = new SubmitCode(@"#r ""nuget:Octokit, 0.32.0"" #r ""nuget:NodaTime, 2.4.6"" using Octokit; using NodaTime; using NodaTime.Extensions; using XPlot.Plotly;"); await kernel.SendAsync(command, CancellationToken.None); events.Should().NotContainErrors(); events .Should() .ContainSingle <CommandHandled>(ch => ch.Command == command); }
private static IKernel CreateKernel( string defaultKernelName, FrontendEnvironmentBase frontendEnvironment) { var compositeKernel = new CompositeKernel(); compositeKernel.UseFrontedEnvironment(context => frontendEnvironment); compositeKernel.Add( new CSharpKernel() .UseDefaultFormatting() .UseNugetDirective() .UseKernelHelpers() .UseJupyterHelpers() .UseWho() .UseXplot() .UseMathAndLaTeX()); compositeKernel.Add( new FSharpKernel() .UseDefaultFormatting() .UseKernelHelpers() .UseWho() .UseDefaultNamespaces() .UseXplot() .UseMathAndLaTeX()); compositeKernel.Add( new PowerShellKernel() .UseXplot() , new[] { "#!pwsh" }); var kernel = compositeKernel .UseDefaultMagicCommands() .UseLog() .UseAbout(); kernel.DefaultKernelName = defaultKernelName; kernel.Name = ".NET"; return(kernel); }
public async Task Handling_kernel_can_be_specified_using_kernel_name_as_a_directive_as_a_proxy_named_pipe2() { var fSharpKernel = new FSharpKernel(); using var localKernel = new CompositeKernel { fSharpKernel }.UseConnection(new ConnectNamedPipe()); localKernel.DefaultKernelName = fSharpKernel.Name; var pipeName = Guid.NewGuid().ToString(); using var remoteKernel = new CSharpKernel(); StartServer(remoteKernel, pipeName); using var events = localKernel.KernelEvents.ToSubscribedList(); var proxyCommand = new SubmitCode($"#!connect named-pipe --kernel-name test --pipe-name {pipeName}"); await localKernel.SendAsync(proxyCommand); var proxyCommand2 = new SubmitCode(@" #!test var x = 1 + 1; x"); await localKernel.SendAsync(proxyCommand2); var proxyCommand3 = new SubmitCode(@" #!test var y = x + x; y"); await localKernel.SendAsync(proxyCommand3); events.Should() .ContainSingle <CommandSucceeded>(e => e.Command == proxyCommand2); events.Should() .ContainSingle <CommandSucceeded>(e => e.Command == proxyCommand3); }
public async Task inspect_with_default_settings_calls_inspector_and_produces_output() { using var kernel = new CompositeKernel() { new CSharpKernel() }; await new InspectExtension().OnLoadAsync(kernel); var submission = @" #!inspect public class A { public string P1 { get; set; } } "; var result = await kernel.SendAsync(new SubmitCode(submission, "csharp")); result.KernelEvents .ToSubscribedList() .Should() .NotContainErrors(); var formattedValues = result.KernelEvents .ToSubscribedList() .Should() .ContainSingle <DisplayedValueProduced>() .Which .FormattedValues .Should() .ContainSingle() .Which .Value .Should() .ContainAll( "Tabbed view ", "[assembly: CompilationRelaxations(8)]", "[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]", "private auto ansi ", "instance string get_P1 () cil managed", "ctor()", "get_P1()"); }
protected override Task <IDisposable> ConnectHostAsync(CompositeKernel remoteKernel, string pipeName) { var serverStream = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous); var kernelCommandAndEventPipeStreamReceiver = new KernelCommandAndEventPipeStreamReceiver(serverStream); var kernelCommandAndEventPipeStreamSender = new KernelCommandAndEventPipeStreamSender(serverStream); var host = new KernelHost(remoteKernel, kernelCommandAndEventPipeStreamSender, new MultiplexingKernelCommandAndEventReceiver(kernelCommandAndEventPipeStreamReceiver)); Task.Run(() => { // required as waiting connection on named pipe server will block serverStream.WaitForConnection(); var _ = host.ConnectAsync(); }); RegisterForDisposal(host); return(Task.FromResult <IDisposable>(host)); }
public async Task widget_code_generation_is_not_broken() { using var kernel = new CompositeKernel(); var kernelExtension = new NteractKernelExtension(); await kernelExtension.OnLoadAsync(kernel); var data = new[] { new { Type = "orange", Price = 1.2 }, new { Type = "apple", Price = 1.3 }, new { Type = "grape", Price = 1.4 } }; var html = data.ExploreWithNteract().ToDisplayString(HtmlFormatter.MimeType); this.Assent(html.FixedGuid()); }
public async Task it_registers_formatters() { using var kernel = new CompositeKernel(); var kernelExtension = new NteractKernelExtension(); await kernelExtension.OnLoadAsync(kernel); var data = new[] { new { Type = "orange", Price = 1.2 }, new { Type = "apple", Price = 1.3 }, new { Type = "grape", Price = 1.4 } }; var formatted = data.ToTabularJsonString().ToDisplayString(HtmlFormatter.MimeType); formatted.Should().Contain("configureRequireFromExtension('nteract','1.0.0')(['nteract/index'], (nteract) => {"); }
public async Task Multiple_connections_can_be_created_using_the_same_connection_type() { using var compositeKernel = new CompositeKernel(); compositeKernel.UseKernelClientConnection( new ConnectFakeKernel("fake", "Connects the fake kernel") { CreateKernel = (options, context) => Task.FromResult <Kernel>(new FakeKernel()) }); await compositeKernel.SubmitCodeAsync("#!connect fake --kernel-name fake1"); await compositeKernel.SubmitCodeAsync("#!connect fake --kernel-name fake2"); compositeKernel .Should() .ContainSingle(k => k.Name == "fake1") .And .ContainSingle(k => k.Name == "fake2"); }
public void when_a_user_defines_kernel_connection_type_it_is_available_as_subcommand_of_connect() { using var compositeKernel = new CompositeKernel { new CSharpKernel().UseDefaultMagicCommands() }; compositeKernel.AddConnectionDirective( new Command("Data", "Connects to a data kernel") ); compositeKernel.Directives .Should() .ContainSingle(c => c.Name == "#!connect") .Which .Children .OfType <ICommand>() .Should() .ContainSingle(c => c.Name == "Data"); }
public void ChooseKernelDirective_parserResults_are_available_in_the_split_commands() { using var kernel = new CompositeKernel { new CSharpKernel() }; var code = @" #!csharp var a = 12; a.Display();"; var commands = kernel.SubmissionParser.SplitSubmission(new SubmitCode(code)); commands.Should().ContainSingle <SubmitCode>() .Which .KernelChooserParseResult .CommandResult.Command.Name .Should() .Be("#!csharp"); }
private static SubmitCode CreateSubmitCode(CompositeKernel kernel) { switch (kernel.DefaultKernelName) { case "csharp": return(new SubmitCode(@" Console.Write(""value one""); Console.Write(""value two""); Console.Write(""value three"");", "csharp")); case "fsharp": return(new SubmitCode(@"open System Console.Write(""value one"") Console.Write(""value two"") Console.Write(""value three"")", "fsharp")); default: throw new ArgumentOutOfRangeException($"kernel {kernel.DefaultKernelName} not supported"); } }
public void Directives_may_not_have_aliases_that_begin_with_(string value) { using var kernel = new CompositeKernel(); var command = new Command("#!this-is-fine"); command.AddAlias($"{value}hello"); kernel .Invoking(k => { kernel.AddDirective(command); }) .Should() .Throw <ArgumentException>() .Which .Message .Should() .Be($"Invalid directive name \"{value}hello\". Directives must begin with \"#\"."); }
private static IKernel CreateKernel(string defaultKernelName) { var kernel = new CompositeKernel { new CSharpKernel() .UseDefaultRendering() .UseNugetDirective(new NativeAssemblyLoadHelper()) .UseKernelHelpers() .UseXplot(), new FSharpKernel() .UseDefaultRendering() } .UseDefaultMagicCommands() .UseExtendDirective(); kernel.DefaultKernelName = defaultKernelName; kernel.Name = ".NET"; return(kernel); }
public async Task it_can_loads_script_from_uri_and_specify_context() { using var kernel = new CompositeKernel(); var kernelExtension = new NteractKernelExtension(); await kernelExtension.OnLoadAsync(kernel); kernel.UseNteractDataExplorer("https://a.cdn.url/script.js", "2.2.2"); var data = new[] { new { Type = "orange", Price = 1.2 }, new { Type = "apple", Price = 1.3 }, new { Type = "grape", Price = 1.4 } }; var formatted = data.ExploreWithNteract().ToDisplayString(HtmlFormatter.MimeType); formatted.Should().Contain("'context': '2.2.2'"); }
public async Task it_can_load_script_from_the_extension() { using var kernel = new CompositeKernel(); var kernelExtension = new NteractKernelExtension(); await kernelExtension.OnLoadAsync(kernel); var data = new[] { new { Type = "orange", Price = 1.2 }, new { Type = "apple", Price = 1.3 }, new { Type = "grape", Price = 1.4 } }; var formatted = data.ExploreWithNteract().ToDisplayString(HtmlFormatter.MimeType); formatted.Should().Contain("configureRequireFromExtension"); }
public async Task Handling_kernel_can_be_specified_by_setting_the_kernel_name_in_the_command() { var receivedOnFakeRepl = new List <IKernelCommand>(); var kernel = new CompositeKernel { new CSharpKernel(), new FakeKernel("fake") { Handle = context => { receivedOnFakeRepl.Add(context.Command); return(Task.CompletedTask); } } }; await kernel.SendAsync( new SubmitCode( @"var x = 123;", "csharp")); await kernel.SendAsync( new SubmitCode( @"hello!", "fake")); await kernel.SendAsync( new SubmitCode( @"x", "csharp")); receivedOnFakeRepl .Should() .ContainSingle(c => c is SubmitCode) .Which .As <SubmitCode>() .Code .Should() .Be("hello!"); }
private static async Task <CompositeKernel> CreateKernel() { var csharpKernel = new CSharpKernel().UseNugetDirective(); await csharpKernel.SubmitCodeAsync(@" #r ""nuget:microsoft.sqltoolsservice,3.0.0-release.163"" "); var kernel = new CompositeKernel { new KqlDiscoverabilityKernel(), csharpKernel, new KeyValueStoreKernel() }; kernel.DefaultKernelName = csharpKernel.Name; var kqlKernelExtension = new KqlKernelExtension(); await kqlKernelExtension.OnLoadAsync(kernel); return(kernel); }
public async Task sql_kernel_does_not_execute_query() { using var kernel = new CompositeKernel { new SqlDiscoverabilityKernel() }; using var events = kernel.KernelEvents.ToSubscribedList(); var query = "select * from sys.databases"; await kernel.SendAsync(new SubmitCode($"#!sql\n\n{query}")); var displayValue = events.Should() .ContainSingle <DisplayedValueProduced>() .Which; var message = displayValue.Value.ToString(); message.Should() .NotContain(query); }
public async Task can_specify_cacheBuster() { using var kernel = new CompositeKernel(); var kernelExtension = new NteractKernelExtension(); await kernelExtension.OnLoadAsync(kernel); kernel.UseNteractDataExplorer("https://a.cdn.url/script.js", cacheBuster: "XYZ"); var data = new[] { new { Type = "orange", Price = 1.2 }, new { Type = "apple", Price = 1.3 }, new { Type = "grape", Price = 1.4 } }; var formatted = data.ExploreWithNteract().ToDisplayString(HtmlFormatter.MimeType); formatted.Should().Contain("'urlArgs': 'cacheBuster=XYZ'"); }
public async Task Events_published_by_child_kernel_are_visible_in_parent_kernel() { var subKernel = new CSharpKernel(); using var compositeKernel = new CompositeKernel { subKernel }; var events = compositeKernel.KernelEvents.ToSubscribedList(); await subKernel.SendAsync(new SubmitCode("var x = 1;")); events .Select(e => e.GetType()) .Should() .ContainInOrder( typeof(CodeSubmissionReceived), typeof(CompleteCodeSubmissionReceived), typeof(CommandSucceeded)); }
private async Task <CompositeKernel> CreateKernel() { var csharpKernel = new CSharpKernel().UseNugetDirective(); await csharpKernel.SubmitCodeAsync(@$ " #r " "nuget:microsoft.sqltoolsservice,3.0.0-release.53" " "); // TODO: remove SQLKernel it is used to test current patch var kernel = new CompositeKernel { new SQLKernel(), csharpKernel, new KeyValueStoreKernel() }; kernel.DefaultKernelName = csharpKernel.Name; kernel.UseKernelClientConnection(new MsSqlKernelConnection()); return(kernel); }
public async Task it_can_loads_script_from_uri() { using var kernel = new CompositeKernel(); var kernelExtension = new SandDanceKernelExtension(); SandDanceExplorerExtensions.Settings.UseUri("https://a.cdn.url/script.js"); await kernelExtension.OnLoadAsync(kernel); var data = new[] { new { Type = "orange", Price = 1.2 }, new { Type = "apple", Price = 1.3 }, new { Type = "grape", Price = 1.4 } }; var formatted = data.ExploreWithSandDance().ToDisplayString(HtmlFormatter.MimeType); formatted.Should().Contain("require.config("); }