Skip to content

Commit 8ccb756

Browse files
authored
Merge pull request #1243 from KevinRansom/fixpoundi
Fix pound i
2 parents a8d4e9a + 43432e0 commit 8ccb756

File tree

2 files changed

+87
-10
lines changed

2 files changed

+87
-10
lines changed

src/Microsoft.DotNet.Interactive.Tests/LanguageKernelPackageTests.cs

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,18 +425,86 @@ public async Task Pound_i_nuget_displays_list_of_added_sources(Language language
425425

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

428-
await kernel.SubmitCodeAsync(@"#i ""nuget:https://completelyFakerestoreSource""");
428+
await kernel.SubmitCodeAsync(@"
429+
#i ""nuget:https://completelyFakerestoreSource1""
430+
#i ""nuget:https://completelyFakerestoreSource2""
431+
");
432+
events.OfType<DisplayEvent>().Select(e => e.GetType()).Should().ContainInOrder(typeof(DisplayedValueProduced), typeof(DisplayedValueUpdated));
433+
}
429434

435+
[Theory]
436+
[InlineData(Language.CSharp)]
437+
[InlineData(Language.FSharp)]
438+
public async Task Pound_i_nuget_with_multi_submissions_combines_the_Text_Produced(Language language)
439+
{
440+
var kernel = CreateKernel(language);
441+
442+
await kernel.SubmitCodeAsync(@"
443+
#i ""nuget:https://completelyFakerestoreSourceCommand1.1""
444+
#i ""nuget:https://completelyFakerestoreSourceCommand1.2""
445+
");
446+
447+
var result = await kernel.SubmitCodeAsync(@"
448+
#i ""nuget:https://completelyFakerestoreSourceCommand2.1""
449+
");
450+
451+
var expectedList = new[]
452+
{
453+
"https://completelyFakerestoreSourceCommand1.1",
454+
"https://completelyFakerestoreSourceCommand1.2",
455+
"https://completelyFakerestoreSourceCommand2.1"
456+
};
457+
458+
using var events = result.KernelEvents.ToSubscribedList();
459+
460+
// For the DisplayedValueUpdated events strip out the restore sources
461+
// Verify that they match the expected values
430462
events.Should()
431-
.ContainSingle<DisplayedValueUpdated>()
432-
.Which
433-
.FormattedValues
463+
.ContainSingle<DisplayedValueProduced>()
464+
.Which.FormattedValues
434465
.Should()
435-
.ContainSingle(v => v.MimeType == "text/html")
436-
.Which
437-
.Value
466+
.ContainSingle(e => e.MimeType == HtmlFormatter.MimeType)
467+
.Which.Value
468+
.Should()
469+
.ContainAll(expectedList);
470+
}
471+
472+
[Theory]
473+
[InlineData(Language.CSharp)]
474+
[InlineData(Language.FSharp)]
475+
public async Task Pound_i_nuget_with_multi_submissions_combines_the_Text_Updates(Language language)
476+
{
477+
var kernel = CreateKernel(language);
478+
479+
await kernel.SubmitCodeAsync(@"
480+
#i ""nuget:https://completelyFakerestoreSourceCommand1.1""
481+
#i ""nuget:https://completelyFakerestoreSourceCommand1.2""
482+
");
483+
484+
var result = await kernel.SubmitCodeAsync(@"
485+
#i ""nuget:https://completelyFakerestoreSourceCommand2.1""
486+
#i ""nuget:https://completelyFakerestoreSourceCommand2.2""
487+
");
488+
489+
var expectedList = new[]
490+
{
491+
"https://completelyFakerestoreSourceCommand1.1",
492+
"https://completelyFakerestoreSourceCommand1.2",
493+
"https://completelyFakerestoreSourceCommand2.1",
494+
"https://completelyFakerestoreSourceCommand2.2"
495+
};
496+
497+
using var events = result.KernelEvents.ToSubscribedList();
498+
499+
// For the DisplayedValueUpdated events strip out the restore sources
500+
// Verify that they match the expected values
501+
events.OfType<DisplayedValueUpdated>()
502+
.Last().FormattedValues
503+
.Should()
504+
.ContainSingle(e => e.MimeType == HtmlFormatter.MimeType)
505+
.Which.Value
438506
.Should()
439-
.ContainAll("Restore sources", "https://completelyFakerestoreSource");
507+
.ContainAll(expectedList);
440508
}
441509

442510
[Theory]

src/Microsoft.DotNet.Interactive/KernelSupportsNugetExtensions.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static T UseNugetDirective<T>(this T kernel)
3636
return kernel;
3737
}
3838

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

54-
var displayed = new DisplayedValue("displayedValueRestoreSources" + context.GetHashCode().ToString(), HtmlFormatter.MimeType, context);
55-
displayed.Update(content);
55+
object displayed = null;
56+
if (!context.Command.Properties.TryGetValue(restoreSourcesPropertyName, out displayed))
57+
{
58+
displayed = context.Display(content, HtmlFormatter.MimeType);
59+
context.Command.Properties.Add(restoreSourcesPropertyName, displayed);
60+
}
61+
else
62+
{
63+
(displayed as DisplayedValue).Update(content);
64+
}
5665
}
5766
});
5867
return iDirective;

0 commit comments

Comments
 (0)