diff --git a/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.httpRequest_api_is_not_changed.approved.txt b/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.httpRequest_api_is_not_changed.approved.txt index 7f5da8229c..0b9b6cc348 100644 --- a/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.httpRequest_api_is_not_changed.approved.txt +++ b/src/Microsoft.DotNet.Interactive.ApiCompatibility.Tests/ApiCompatibilityTests.httpRequest_api_is_not_changed.approved.txt @@ -1,4 +1,6 @@ Microsoft.DotNet.Interactive.Http + public class ClearValues : Microsoft.DotNet.Interactive.Commands.KernelCommand, System.IEquatable + .ctor(System.String targetKernelName = null) public class EmptyHttpResponse .ctor() public class HttpContent @@ -7,8 +9,9 @@ Microsoft.DotNet.Interactive.Http public System.String ContentType { get;} public System.Collections.Generic.Dictionary Headers { get;} public System.String Raw { get;} - public class HttpKernel : Microsoft.DotNet.Interactive.Kernel, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, System.IDisposable + public class HttpKernel : Microsoft.DotNet.Interactive.Kernel, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, Microsoft.DotNet.Interactive.IKernelCommandHandler, System.IDisposable .ctor(System.String name = null, System.Net.Http.HttpClient client = null, System.Int32 responseDelayThresholdInMilliseconds = 1000, System.Int32 contentByteLengthThreshold = 500000) + public System.Threading.Tasks.Task HandleAsync(ClearValues command, Microsoft.DotNet.Interactive.KernelInvocationContext context) public class HttpKernelExtension public static System.Void Load(Microsoft.DotNet.Interactive.Kernel kernel, System.Net.Http.HttpClient httpClient = null, System.Int32 responseDelayThresholdInMilliseconds = 1000, System.Int32 contentByteLengthThreshold = 500000) .ctor() diff --git a/src/Microsoft.DotNet.Interactive.Http.Tests/HttpKernelTests.cs b/src/Microsoft.DotNet.Interactive.Http.Tests/HttpKernelTests.cs index e51d7776b5..c5325c04bb 100644 --- a/src/Microsoft.DotNet.Interactive.Http.Tests/HttpKernelTests.cs +++ b/src/Microsoft.DotNet.Interactive.Http.Tests/HttpKernelTests.cs @@ -2231,4 +2231,26 @@ public async Task traceparent_header_has_a_new_top_level_value_for_each_request( // the traceparent format looks like this: 00-d00689882649007396cd32ab75c2611c-59402ab4fd5c55f7-00 traceparent1.Single()[0..36].Should().NotBe(traceparent2.Single()[0..36]); } + + [Fact] + public async Task Variables_can_be_cleared() + { + using var kernel = new HttpKernel().UseValueSharing(); + + var result = await kernel.SendAsync(new SendValue("my_host", "my.host.com")); + result.Events.Should().NotContainErrors(); + + result = await kernel.SendAsync(new ClearValues()); + result.Events.Should().NotContainErrors(); + + result = await kernel.SendAsync(new RequestValueInfos()); + + result.Events.Should().NotContainErrors(); + + result.Events.Should().ContainSingle() + .Which + .ValueInfos + .Should() + .BeEmpty(); + } } diff --git a/src/Microsoft.DotNet.Interactive.Http/ClearValues.cs b/src/Microsoft.DotNet.Interactive.Http/ClearValues.cs new file mode 100644 index 0000000000..abfd2e08e8 --- /dev/null +++ b/src/Microsoft.DotNet.Interactive.Http/ClearValues.cs @@ -0,0 +1,13 @@ +// 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 Microsoft.DotNet.Interactive.Commands; + +namespace Microsoft.DotNet.Interactive.Http; + +public class ClearValues : KernelCommand +{ + public ClearValues(string? targetKernelName = null) : base(targetKernelName) + { + } +} \ No newline at end of file diff --git a/src/Microsoft.DotNet.Interactive.Http/HttpKernel.cs b/src/Microsoft.DotNet.Interactive.Http/HttpKernel.cs index 07f6f0e0bf..3bfc09b4ab 100644 --- a/src/Microsoft.DotNet.Interactive.Http/HttpKernel.cs +++ b/src/Microsoft.DotNet.Interactive.Http/HttpKernel.cs @@ -10,11 +10,8 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; -using System.Globalization; using System.Linq; using System.Net.Http; -using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; @@ -24,6 +21,7 @@ namespace Microsoft.DotNet.Interactive.Http; public class HttpKernel : Kernel, + IKernelCommandHandler, IKernelCommandHandler, IKernelCommandHandler, IKernelCommandHandler, @@ -58,6 +56,12 @@ This Kernel is able to execute http requests and display the results. RegisterForDisposal(_client); } + public Task HandleAsync(ClearValues command, KernelInvocationContext context) + { + _variables.Clear(); + return Task.CompletedTask; + } + Task IKernelCommandHandler.HandleAsync(RequestValue command, KernelInvocationContext context) { if (_variables.TryGetValue(command.Name, out var value)) diff --git a/src/Microsoft.DotNet.Interactive.Tests/Connection/SerializationTests.Command_contract_has_not_been_broken.approved.ClearValues.json b/src/Microsoft.DotNet.Interactive.Tests/Connection/SerializationTests.Command_contract_has_not_been_broken.approved.ClearValues.json new file mode 100644 index 0000000000..d64b59df52 --- /dev/null +++ b/src/Microsoft.DotNet.Interactive.Tests/Connection/SerializationTests.Command_contract_has_not_been_broken.approved.ClearValues.json @@ -0,0 +1,13 @@ +{ + "token": "the-token", + "commandType": "ClearValues", + "command": { + "targetKernelName": null, + "originUri": null, + "destinationUri": null + }, + "routingSlip": [ + "kernel://somelocation/kernelName?tag=arrived", + "kernel://somelocation/kernelName" + ] +} \ No newline at end of file diff --git a/src/Microsoft.DotNet.Interactive.Tests/Connection/SerializationTests.cs b/src/Microsoft.DotNet.Interactive.Tests/Connection/SerializationTests.cs index 76b6ad512a..5ffd5ad1b7 100644 --- a/src/Microsoft.DotNet.Interactive.Tests/Connection/SerializationTests.cs +++ b/src/Microsoft.DotNet.Interactive.Tests/Connection/SerializationTests.cs @@ -14,8 +14,12 @@ using Microsoft.CodeAnalysis; using Microsoft.DotNet.Interactive.Commands; using Microsoft.DotNet.Interactive.Connection; +using Microsoft.DotNet.Interactive.CSharp; using Microsoft.DotNet.Interactive.Events; using Microsoft.DotNet.Interactive.Formatting; +using Microsoft.DotNet.Interactive.FSharp; +using Microsoft.DotNet.Interactive.Http; +using Microsoft.DotNet.Interactive.PowerShell; using Microsoft.DotNet.Interactive.Tests.Utility; using Microsoft.DotNet.Interactive.ValueSharing; using Pocket; @@ -120,11 +124,20 @@ public void Event_contract_has_not_been_broken(KernelEvent @event) [Fact] public void All_command_types_are_tested_for_round_trip_serialization() { - var interactiveCommands = typeof(Kernel) - .Assembly - .ExportedTypes - .Concrete() - .DerivedFrom(typeof(KernelCommand)); + var commandTypes = new[] + { + typeof(Kernel), + typeof(CSharpKernel), + typeof(FSharpKernel), + typeof(HttpKernel), + typeof(PowerShellKernel), + } + .Select(t => t.Assembly) + .SelectMany(a => a.ExportedTypes); + + var interactiveCommands = commandTypes + .Concrete() + .DerivedFrom(typeof(KernelCommand)); Commands() .Select(e => e[0].GetType()) @@ -163,6 +176,8 @@ public static IEnumerable Commands() IEnumerable commands() { + yield return new ClearValues(); + yield return new DisplayError("oops!"); yield return new DisplayValue( diff --git a/src/Microsoft.DotNet.Interactive.Tests/Microsoft.DotNet.Interactive.Tests.csproj b/src/Microsoft.DotNet.Interactive.Tests/Microsoft.DotNet.Interactive.Tests.csproj index 33db4626e3..ccbecaf63e 100644 --- a/src/Microsoft.DotNet.Interactive.Tests/Microsoft.DotNet.Interactive.Tests.csproj +++ b/src/Microsoft.DotNet.Interactive.Tests/Microsoft.DotNet.Interactive.Tests.csproj @@ -75,6 +75,7 @@ +