Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsequitur committed Feb 7, 2020
1 parent 8cc68a1 commit 334e5a5
Show file tree
Hide file tree
Showing 16 changed files with 292 additions and 121 deletions.
138 changes: 81 additions & 57 deletions Microsoft.DotNet.Interactive.Tests/KernelExtensionTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,66 @@ namespace Microsoft.DotNet.Interactive.Tests
{
internal static class KernelExtensionTestHelper
{
internal static async Task<FileInfo> CreateExtensionNupkgInDirectory(
private static readonly string _microsoftDotNetInteractiveDllPath = typeof(IKernelExtension).Assembly.Location;

internal static async Task<FileInfo> CreateExtensionNupkg(
DirectoryInfo projectDir,
string body,
DirectoryInfo outputDir,
[CallerMemberName] string testName = null)
string code,
string packageName,
string packageVersion)
{
var extensionName = AlignExtensionNameWithDirectoryName(projectDir, testName);
projectDir.Populate(
("_._", ""),
ExtensionCs(code),
("Extension.csproj", $@"
<Project Sdk=""Microsoft.NET.Sdk"">
await CreateProjectAndBuild(
projectDir,
body,
extensionName);
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IncludeBuildOutput>false</IncludeBuildOutput>
<IsPackable>true</IsPackable>
<PackageId>{packageName}</PackageId>
<PackageVersion>{packageVersion}</PackageVersion>
</PropertyGroup>
var extensionDll = projectDir
.GetDirectories("bin", SearchOption.AllDirectories)
.Single()
.GetFiles($"{extensionName}.nupkg", SearchOption.AllDirectories)
.Single();
<ItemGroup>
<None Include=""_._"" Pack=""true"" PackagePath=""lib/netstandard2.0"" />
<None Include=""_._"" Pack=""true"" PackagePath=""lib/netcoreapp3.1"" />
<None Include=""$(OutputPath)/Extension.dll"" Pack=""true"" PackagePath=""interactive-extensions/dotnet"" />
</ItemGroup>
if (!outputDir.Exists)
{
outputDir.Create();
}
<ItemGroup>
<Reference Include=""Microsoft.DotNet.Interactive"">
<HintPath>{_microsoftDotNetInteractiveDllPath}</HintPath>
</Reference>
</ItemGroup>
</Project>
var finalExtensionDll = new FileInfo(Path.Combine(outputDir.FullName, extensionDll.Name));
File.Move(extensionDll.FullName, finalExtensionDll.FullName);
"));

var dotnet = new Dotnet(projectDir);

var pack = await dotnet.Pack(projectDir.FullName);

pack.ThrowOnFailure();

return finalExtensionDll;
return projectDir
.GetFiles("*.nupkg", SearchOption.AllDirectories)
.Single();
}

internal static async Task<FileInfo> CreateExtensionDllInDirectory(
internal static async Task<FileInfo> CreateExtensionAssembly(
DirectoryInfo projectDir,
string body,
DirectoryInfo outputDir,
string code,
DirectoryInfo copyDllTo = null,
[CallerMemberName] string testName = null)
{
var extensionName = AlignExtensionNameWithDirectoryName(projectDir, testName);

await CreateProjectAndBuild(
await CreateExtensionProjectAndBuild(
projectDir,
body,
code,
extensionName);

var extensionDll = projectDir
Expand All @@ -61,15 +80,19 @@ await CreateProjectAndBuild(
.GetFiles($"{extensionName}.dll", SearchOption.AllDirectories)
.Single();

if (!outputDir.Exists)
if (copyDllTo != null)
{
outputDir.Create();
if (!copyDllTo.Exists)
{
copyDllTo.Create();
}

var finalExtensionDll = new FileInfo(Path.Combine(copyDllTo.FullName, extensionDll.Name));
File.Move(extensionDll.FullName, finalExtensionDll.FullName);
extensionDll = finalExtensionDll;
}

var finalExtensionDll = new FileInfo(Path.Combine(outputDir.FullName, extensionDll.Name));
File.Move(extensionDll.FullName, finalExtensionDll.FullName);

return finalExtensionDll;
return extensionDll;
}

private static string AlignExtensionNameWithDirectoryName(DirectoryInfo extensionDir, string testName)
Expand All @@ -78,29 +101,13 @@ private static string AlignExtensionNameWithDirectoryName(DirectoryInfo extensio
return match.Success ? $"{testName}{match.Groups["counter"].Value}" : testName;
}

private static async Task CreateProjectAndBuild(
DirectoryInfo projectDir,
string body,
string extensionName = null)
private static async Task CreateExtensionProjectAndBuild(
DirectoryInfo projectDir,
string code,
string extensionName)
{
var microsoftDotNetInteractiveDllPath = typeof(IKernelExtension).Assembly.Location;

projectDir.Populate(
("Extension.cs", $@"
using System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
public class TestKernelExtension : IKernelExtension
{{
public async Task OnLoadAsync(IKernel kernel)
{{
{body}
}}
}}
"),
ExtensionCs(code),
("TestExtension.csproj", $@"
<Project Sdk=""Microsoft.NET.Sdk"">
Expand All @@ -109,20 +116,37 @@ public async Task OnLoadAsync(IKernel kernel)
<AssemblyName>{extensionName}</AssemblyName>
</PropertyGroup>
<ItemGroup>
<ItemGroup>
<Reference Include=""Microsoft.DotNet.Interactive"">
<HintPath>{microsoftDotNetInteractiveDllPath}</HintPath>
<HintPath>{_microsoftDotNetInteractiveDllPath}</HintPath>
</Reference>
</ItemGroup>
</Project>
"));

var buildResult = await new Dotnet(projectDir).Build();

buildResult.ThrowOnFailure();
}

private static (string, string) ExtensionCs(string code)
{
return ("Extension.cs", $@"
using System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
public class TestKernelExtension : IKernelExtension
{{
public async Task OnLoadAsync(IKernel kernel)
{{
{code}
}}
}}
");
}

}
}
9 changes: 4 additions & 5 deletions Microsoft.DotNet.Interactive.Tests/KernelExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Extensions;
using Xunit;

namespace Microsoft.DotNet.Interactive.Tests
{
public class KernelExtensionsTests
{
[Fact(Timeout = 45000)]
[Fact]
public void VisitSubkernels_does_not_recurse_by_default()
{
var visited = new List<string>();
Expand All @@ -36,7 +35,7 @@ public void VisitSubkernels_does_not_recurse_by_default()
visited.Should().BeEquivalentTo("child");
}

[Fact(Timeout = 45000)]
[Fact]
public void VisitSubkernels_can_recurse_child_composite_kernels()
{
var visited = new List<string>();
Expand All @@ -60,7 +59,7 @@ public void VisitSubkernels_can_recurse_child_composite_kernels()
visited.Should().BeEquivalentTo("child", "grandchild");
}

[Fact(Timeout = 45000)]
[Fact]
public async Task VisitSubkernelsAsync_does_not_recurse_by_default()
{
var visited = new List<string>();
Expand Down Expand Up @@ -88,7 +87,7 @@ await parent.VisitSubkernelsAsync(kernel =>
visited.Should().BeEquivalentTo("child");
}

[Fact(Timeout = 45000)]
[Fact]
public async Task VisitSubkernelsAsync_can_recurse_child_composite_kernels()
{
var visited = new List<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Commands;
Expand All @@ -28,7 +27,7 @@ public async Task It_loads_extensions_in_specified_directory_via_a_command()

var dllDir = projectDir.CreateSubdirectory("extension");

var extensionDll = await CreateExtensionDllInDirectory(
var extensionDll = await CreateExtensionAssembly(
projectDir,
@"await kernel.SendAsync(new SubmitCode(""display(\""csharp extension installed\"");""));",
dllDir);
Expand Down Expand Up @@ -56,7 +55,7 @@ public async Task It_throws_when_extension_throws_during_load()

var dllDir = projectDir.CreateSubdirectory("extension");

await CreateExtensionDllInDirectory(
await CreateExtensionAssembly(
projectDir,
"throw new Exception();",
dllDir);
Expand All @@ -79,33 +78,26 @@ public async Task It_loads_extensions_found_in_nuget_packages()
{
var projectDir = DirectoryUtility.CreateDirectory();

var packageName = "MyExtensionPackage";
var packageVersion = "2.0.0";
var packageName = "MyTestExtension";
var packageVersion = "2.0.0-" + Guid.NewGuid().ToString("N");
var guid = Guid.NewGuid().ToString();

var nugetPackageDirectory = new DirectoryInfo(
Path.Combine(
projectDir.FullName,
packageName,
packageVersion));

var extensionsDir =
new DirectoryInfo(
Path.Combine(
nugetPackageDirectory.FullName,
"interactive-extensions", "dotnet"));

var extensionDll = await CreateExtensionNupkgInDirectory(
projectDir,
@"await kernel.SendAsync(new SubmitCode(""using System.Reflection;""));",
extensionsDir);
var nupkg = await CreateExtensionNupkg(
projectDir,
$"await kernel.SendAsync(new SubmitCode(\"\\\"{guid}\\\"\"));",
packageName,
packageVersion);

var kernel = CreateKernel(Language.CSharp);

await kernel.SubmitCodeAsync($"#r \"nuget:{packageName},{packageVersion}\"");
await kernel.SubmitCodeAsync($@"
#r ""nuget:RestoreSources={nupkg.Directory.FullName}""
#r ""nuget:{packageName},{packageVersion}"" ");

KernelEvents.Should()
.ContainSingle<DisplayedValueUpdated>(e =>
e.Value.ToString() == $"Loaded kernel extension TestKernelExtension from assembly {extensionDll.FullName}");
.ContainSingle<ReturnValueProduced>(
e =>
e.Value.ToString().Contains(guid));
}
}
}
2 changes: 0 additions & 2 deletions Microsoft.DotNet.Interactive/Commands/KernelCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,5 @@ public virtual async Task InvokeAsync(KernelInvocationContext context)

await Handler(this, context);
}

internal IKernel HandlingKernel { get; set; }
}
}
8 changes: 1 addition & 7 deletions Microsoft.DotNet.Interactive/CompositeKernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,7 @@ await LoadExtensionsFromDirectoryAsync(
protected override void SetHandlingKernel(IKernelCommand command, KernelInvocationContext context)
{
var kernel = GetHandlingKernel(command, context);

if (command is KernelCommandBase commandBase &&
commandBase.HandlingKernel == null)
{
commandBase.HandlingKernel = kernel;
}


if (context.HandlingKernel == null)
{
context.HandlingKernel = kernel;
Expand Down
9 changes: 8 additions & 1 deletion Microsoft.DotNet.Interactive/PackageRestoreContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ public async Task<PackageRestoreResult> Restore()
WriteProjectFile();

var dotnet = new Dotnet(Directory);
var result = await dotnet.Execute("msbuild -restore /t:WriteNugetAssemblyPaths");

var commandLine = "msbuild -restore /t:WriteNugetAssemblyPaths";

#if DEBUG
commandLine += " /bl";
#endif

var result = await dotnet.Execute(commandLine);

if (result.ExitCode != 0)
{
Expand Down
1 change: 0 additions & 1 deletion Microsoft.DotNet.Interactive/PackageRestoreResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Utility;

namespace Microsoft.DotNet.Interactive
Expand Down
Loading

0 comments on commit 334e5a5

Please sign in to comment.