-
Notifications
You must be signed in to change notification settings - Fork 368
/
LanguageKernelAssemblyReferenceTests.cs
167 lines (131 loc) · 5.31 KB
/
LanguageKernelAssemblyReferenceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 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 System;
using System.IO;
using System.Reflection;
using FluentAssertions;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.DotNet.Interactive.Tests;
#pragma warning disable 8509
public class LanguageKernelAssemblyReferenceTests : LanguageKernelTestBase
{
public LanguageKernelAssemblyReferenceTests(ITestOutputHelper output) : base(output)
{
}
[Theory]
[InlineData(Language.CSharp)]
[InlineData(Language.FSharp)]
public async Task it_can_load_assembly_references_using_r_directive_single_submission(Language language)
{
var kernel = CreateKernel(language);
// F# strings treat \ as an escape character. So do C# strings, except #r in C# is special, and doesn't. F# usually uses @ strings for paths @"c:\temp\...."
var dllPath = CreateDllInCurrentDirectory();
var source = language switch
{
Language.FSharp => $@"#r @""{dllPath.FullName}""
typeof<Hello>.Name",
Language.CSharp => $@"#r ""{dllPath.FullName}""
typeof(Hello).Name"
};
await SubmitCode(kernel, source);
KernelEvents.Should().NotContainErrors();
KernelEvents
.Should()
.ContainSingle<ReturnValueProduced>()
.Which
.Value
.Should()
.Be("Hello");
}
[Theory]
[InlineData(Language.CSharp)]
[InlineData(Language.FSharp)]
public async Task it_can_load_assembly_references_using_r_directive_separate_submissions(Language language)
{
var kernel = CreateKernel(language);
// F# strings treat \ as an escape character. So do C# strings, except #r in C# is special, and doesn't. F# usually uses @ strings for paths @"c:\temp\...."
var dllPath = CreateDllInCurrentDirectory();
var source = language switch
{
Language.FSharp => new[]
{
$"#r @\"{dllPath.FullName}\"",
"typeof<Hello>.Name"
},
Language.CSharp => new[]
{
$"#r \"{dllPath.FullName}\"",
"typeof(Hello).Name"
}
};
await SubmitCode(kernel, source);
KernelEvents.Should().NotContainErrors();
KernelEvents
.Should()
.ContainSingle<ReturnValueProduced>()
.Which
.Value
.Should()
.Be("Hello");
}
[Theory]
[InlineData(Language.CSharp)]
[InlineData(Language.FSharp)]
public async Task it_can_load_assembly_references_using_r_directive_with_relative_path(Language language)
{
var kernel = CreateKernel(language);
var dllName = CreateDllInCurrentDirectory();
var code = language switch
{
Language.CSharp => $"#r \"{dllName.Name}\"",
Language.FSharp => $"#r \"{dllName.Name}\""
};
var command = new SubmitCode(code);
await kernel.SendAsync(command);
KernelEvents.Should().NotContainErrors();
KernelEvents.Should()
.ContainSingle<CommandSucceeded>(c => c.Command == command);
}
[Theory]
[InlineData(Language.CSharp)]
// [InlineData(Language.FSharp)] Not supported in F#
public async Task it_can_load_assembly_references_using_r_directive_with_relative_path_after_user_code_changes_current_directory(Language language)
{
var currentDirectory = Directory.GetCurrentDirectory();
DisposeAfterTest(() => Directory.SetCurrentDirectory(currentDirectory));
var kernel = CreateKernel(language);
//Even when a user changes the current directory, loading from a relative path is not affected.
await kernel.SendAsync(new SubmitCode("System.IO.Directory.SetCurrentDirectory(\"..\")"));
var dllName = CreateDllInCurrentDirectory();
var code = language switch
{
Language.CSharp => $"#r \"{dllName.Name}\"\nnew Hello()",
Language.FSharp => $"#r \"{dllName.Name}\"\nnew Hello()"
};
var command = new SubmitCode(code);
await kernel.SendAsync(command);
KernelEvents.Should().NotContainErrors();
KernelEvents.Should()
.ContainSingle<CommandSucceeded>(c => c.Command == command);
}
private FileInfo CreateDllInCurrentDirectory()
{
var assemblyName = Guid.NewGuid().ToString("N");
var dllName = assemblyName + ".dll";
var systemRefLocation = typeof(object).GetTypeInfo().Assembly.Location;
var systemReference = MetadataReference.CreateFromFile(systemRefLocation);
CSharpCompilation.Create(assemblyName)
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddReferences(systemReference)
.AddSyntaxTrees(CSharpSyntaxTree.ParseText("public class Hello { }"))
.Emit(dllName);
return new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), dllName));
}
}