Skip to content

Commit

Permalink
Merge pull request #1243 from KevinRansom/fixpoundi
Browse files Browse the repository at this point in the history
Fix pound i
  • Loading branch information
KevinRansom authored Apr 15, 2021
2 parents a8d4e9a + 43432e0 commit 8ccb756
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,18 +425,86 @@ public async Task Pound_i_nuget_displays_list_of_added_sources(Language language

using var events = kernel.KernelEvents.ToSubscribedList();

await kernel.SubmitCodeAsync(@"#i ""nuget:https://completelyFakerestoreSource""");
await kernel.SubmitCodeAsync(@"
#i ""nuget:https://completelyFakerestoreSource1""
#i ""nuget:https://completelyFakerestoreSource2""
");
events.OfType<DisplayEvent>().Select(e => e.GetType()).Should().ContainInOrder(typeof(DisplayedValueProduced), typeof(DisplayedValueUpdated));
}

[Theory]
[InlineData(Language.CSharp)]
[InlineData(Language.FSharp)]
public async Task Pound_i_nuget_with_multi_submissions_combines_the_Text_Produced(Language language)
{
var kernel = CreateKernel(language);

await kernel.SubmitCodeAsync(@"
#i ""nuget:https://completelyFakerestoreSourceCommand1.1""
#i ""nuget:https://completelyFakerestoreSourceCommand1.2""
");

var result = await kernel.SubmitCodeAsync(@"
#i ""nuget:https://completelyFakerestoreSourceCommand2.1""
");

var expectedList = new[]
{
"https://completelyFakerestoreSourceCommand1.1",
"https://completelyFakerestoreSourceCommand1.2",
"https://completelyFakerestoreSourceCommand2.1"
};

using var events = result.KernelEvents.ToSubscribedList();

// For the DisplayedValueUpdated events strip out the restore sources
// Verify that they match the expected values
events.Should()
.ContainSingle<DisplayedValueUpdated>()
.Which
.FormattedValues
.ContainSingle<DisplayedValueProduced>()
.Which.FormattedValues
.Should()
.ContainSingle(v => v.MimeType == "text/html")
.Which
.Value
.ContainSingle(e => e.MimeType == HtmlFormatter.MimeType)
.Which.Value
.Should()
.ContainAll(expectedList);
}

[Theory]
[InlineData(Language.CSharp)]
[InlineData(Language.FSharp)]
public async Task Pound_i_nuget_with_multi_submissions_combines_the_Text_Updates(Language language)
{
var kernel = CreateKernel(language);

await kernel.SubmitCodeAsync(@"
#i ""nuget:https://completelyFakerestoreSourceCommand1.1""
#i ""nuget:https://completelyFakerestoreSourceCommand1.2""
");

var result = await kernel.SubmitCodeAsync(@"
#i ""nuget:https://completelyFakerestoreSourceCommand2.1""
#i ""nuget:https://completelyFakerestoreSourceCommand2.2""
");

var expectedList = new[]
{
"https://completelyFakerestoreSourceCommand1.1",
"https://completelyFakerestoreSourceCommand1.2",
"https://completelyFakerestoreSourceCommand2.1",
"https://completelyFakerestoreSourceCommand2.2"
};

using var events = result.KernelEvents.ToSubscribedList();

// For the DisplayedValueUpdated events strip out the restore sources
// Verify that they match the expected values
events.OfType<DisplayedValueUpdated>()
.Last().FormattedValues
.Should()
.ContainSingle(e => e.MimeType == HtmlFormatter.MimeType)
.Which.Value
.Should()
.ContainAll("Restore sources", "https://completelyFakerestoreSource");
.ContainAll(expectedList);
}

[Theory]
Expand Down
13 changes: 11 additions & 2 deletions src/Microsoft.DotNet.Interactive/KernelSupportsNugetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static T UseNugetDirective<T>(this T kernel)
return kernel;
}

private static readonly string restoreSourcesPropertyName = "commandIHandler.RestoreSources";
private static Command i()
{
var iDirective = new Command("#i")
Expand All @@ -51,8 +52,16 @@ private static Command i()
strong("Restore sources"),
ul(kernel.RestoreSources.Select(s => li(span(s)))));

var displayed = new DisplayedValue("displayedValueRestoreSources" + context.GetHashCode().ToString(), HtmlFormatter.MimeType, context);
displayed.Update(content);
object displayed = null;
if (!context.Command.Properties.TryGetValue(restoreSourcesPropertyName, out displayed))
{
displayed = context.Display(content, HtmlFormatter.MimeType);
context.Command.Properties.Add(restoreSourcesPropertyName, displayed);
}
else
{
(displayed as DisplayedValue).Update(content);
}
}
});
return iDirective;
Expand Down

0 comments on commit 8ccb756

Please sign in to comment.