-
Notifications
You must be signed in to change notification settings - Fork 368
/
PowerShellKernelTests.cs
269 lines (228 loc) · 11.6 KB
/
PowerShellKernelTests.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// 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.Management.Automation;
using System.Threading.Tasks;
using FluentAssertions;
using System.Linq;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Tests;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.DotNet.Interactive.PowerShell.Tests
{
public class PowerShellKernelTests : LanguageKernelTestBase
{
private readonly string _allUsersCurrentHostProfilePath = Path.Combine(Path.GetDirectoryName(typeof(PSObject).Assembly.Location), "Microsoft.dotnet-interactive_profile.ps1");
public PowerShellKernelTests(ITestOutputHelper output) : base(output)
{
}
[Theory]
[InlineData(@"$x = New-Object -TypeName System.IO.FileInfo -ArgumentList c:\temp\some.txt", typeof(FileInfo))]
[InlineData("$x = \"hello!\"", typeof(string))]
public async Task TryGetVariable_unwraps_PowerShell_object(string code, Type expectedType)
{
using var kernel = new PowerShellKernel();
await kernel.SubmitCodeAsync(code);
kernel.TryGetValue("x", out object fi).Should().BeTrue();
fi.Should().BeOfType(expectedType);
}
[Fact]
public async Task PowerShell_progress_sends_updated_display_values()
{
var kernel = CreateKernel(Language.PowerShell);
var command = new SubmitCode(@"
for ($j = 0; $j -le 4; $j += 4 ) {
$p = $j * 25
Write-Progress -Id 1 -Activity 'Search in Progress' -Status ""$p% Complete"" -PercentComplete $p
Start-Sleep -Milliseconds 300
}
");
var result = await kernel.SendAsync(command);
var events = result.KernelEvents.ToSubscribedList();
Assert.Collection(events,
e => e.Should().BeOfType<CodeSubmissionReceived>(),
e => e.Should().BeOfType<CompleteCodeSubmissionReceived>(),
e => e.Should().BeOfType<DiagnosticsProduced>()
.Which.Diagnostics.Count.Should().Be(0),
e => e.Should().BeOfType<DisplayedValueProduced>().Which
.Value.Should().BeOfType<string>().Which
.Should().Match("* Search in Progress* 0% Complete* [ * ] *"),
e => e.Should().BeOfType<DisplayedValueUpdated>().Which
.Value.Should().BeOfType<string>().Which
.Should().Match("* Search in Progress* 100% Complete* [ooo*ooo] *"),
e => e.Should().BeOfType<DisplayedValueUpdated>().Which
.Value.Should().BeOfType<string>().Which
.Should().Be(string.Empty),
e => e.Should().BeOfType<CommandSucceeded>());
}
[Fact]
public async Task PowerShell_token_variables_work()
{
var kernel = CreateKernel(Language.PowerShell);
await kernel.SendAsync(new SubmitCode("echo /this/is/a/path"));
await kernel.SendAsync(new SubmitCode("$$; $^"));
KernelEvents.Should().SatisfyRespectively(
e => e.Should()
.BeOfType<CodeSubmissionReceived>()
.Which.Code
.Should().Be("echo /this/is/a/path"),
e => e.Should()
.BeOfType<CompleteCodeSubmissionReceived>()
.Which.Code
.Should().Be("echo /this/is/a/path"),
e => e.Should().BeOfType<DiagnosticsProduced>()
.Which.Diagnostics.Count.Should().Be(0),
e => e.Should()
.BeOfType<StandardOutputValueProduced>()
.Which
.FormattedValues
.Should()
.ContainSingle(f => f.Value == "/this/is/a/path" + Environment.NewLine),
e => e.Should().BeOfType<CommandSucceeded>(),
e => e.Should()
.BeOfType<CodeSubmissionReceived>()
.Which.Code
.Should().Be("$$; $^"),
e => e.Should()
.BeOfType<CompleteCodeSubmissionReceived>()
.Which.Code
.Should().Be("$$; $^"),
e => e.Should().BeOfType<DiagnosticsProduced>()
.Which.Diagnostics.Count.Should().Be(0),
e => e.Should()
.BeOfType<StandardOutputValueProduced>()
.Which
.FormattedValues
.Should()
.ContainSingle(f => f.Value == "/this/is/a/path" + Environment.NewLine),
e => e.Should()
.BeOfType<StandardOutputValueProduced>()
.Which
.FormattedValues
.Should()
.ContainSingle(f => f.Value == "echo" + Environment.NewLine),
e => e.Should().BeOfType<CommandSucceeded>());
}
[Fact]
public async Task PowerShell_get_history_should_work()
{
var kernel = CreateKernel(Language.PowerShell);
await kernel.SendAsync(new SubmitCode("Get-Verb > $null"));
await kernel.SendAsync(new SubmitCode("echo bar > $null"));
var result = await kernel.SendAsync(new SubmitCode("Get-History | % CommandLine"));
var outputs = result.KernelEvents
.ToSubscribedList()
.OfType<StandardOutputValueProduced>();
outputs.Should().SatisfyRespectively(
e => e.FormattedValues
.Should()
.ContainSingle(f => f.Value == "Get-Verb > $null" + Environment.NewLine),
e => e.FormattedValues
.Should()
.ContainSingle(f => f.Value == "echo bar > $null" + Environment.NewLine));
}
[Fact]
public async Task PowerShell_native_executable_output_is_collected()
{
var kernel = CreateKernel(Language.PowerShell);
var command = new SubmitCode("dotnet --help");
await kernel.SendAsync(command);
var outputs = KernelEvents.OfType<StandardOutputValueProduced>();
outputs.Should().HaveCountGreaterThan(1);
string.Join("",
outputs
.SelectMany(e => e.FormattedValues.Select(v => v.Value))
).ToLowerInvariant()
.Should()
.ContainAll("build-server", "restore");
}
[Fact]
public async Task GetCorrectProfilePaths()
{
using var kernel = new PowerShellKernel().UseProfiles();
// Set variables we will retrieve later.
await kernel.SubmitCodeAsync("$currentUserCurrentHost = $PROFILE.CurrentUserCurrentHost");
await kernel.SubmitCodeAsync("$allUsersCurrentHost = $PROFILE.AllUsersCurrentHost");
var (success, valueProduced) = await kernel.TryRequestValueAsync("currentUserCurrentHost");
success.Should().BeTrue();
valueProduced.Value.Should().BeOfType<string>();
string currentUserCurrentHost = valueProduced.Value.As<string>();
// Get $PROFILE default.
(success, valueProduced) = await kernel.TryRequestValueAsync("PROFILE");
success.Should().BeTrue();
valueProduced.Value.Should().BeOfType<string>();
string profileDefault = valueProduced.Value.As<string>();
// Check that $PROFILE is not null or empty and it is the same as
// $PROFILE.CurrentUserCurrentHost
profileDefault.Should().NotBeNullOrEmpty();
profileDefault.Should().Be(currentUserCurrentHost);
(success, valueProduced) = await kernel.TryRequestValueAsync("allUsersCurrentHost");
success.Should().BeTrue();
valueProduced.Value.Should().BeOfType<string>();
string allUsersCurrentHost = valueProduced.Value.As<string>();
// Check that $PROFILE.AllUsersCurrentHost is what we expect it is:
// $PSHOME + Microsoft.dotnet-interactive_profile.ps1
allUsersCurrentHost.Should().Be(_allUsersCurrentHostProfilePath);
}
[Fact]
public async Task VerifyAllUsersProfileRuns()
{
var randomVariableName = Path.GetRandomFileName().Split('.')[0];
File.WriteAllText(_allUsersCurrentHostProfilePath, $"$global:{randomVariableName} = $true");
try
{
using var kernel = new PowerShellKernel().UseProfiles();
// trigger first time setup.
await kernel.SubmitCodeAsync("Get-Date");
var (success, valueProduced) = await kernel.TryRequestValueAsync(randomVariableName);
success.Should().BeTrue();
valueProduced.Value.Should().BeOfType<bool>();
valueProduced.Value.As<bool>().Should().BeTrue();
}
finally
{
File.Delete(_allUsersCurrentHostProfilePath);
}
}
[Fact]
public async Task Powershell_customobject_is_formatted_for_outdisplay()
{
var kernel = CreateKernel(Language.PowerShell);
var result = await kernel.SendAsync(new SubmitCode("[pscustomobject]@{ prop1 = 'value1'; prop2 = 'value2'; prop3 = 'value3' } | Out-Display"));
var outputs = result.KernelEvents.ToSubscribedList();
string mimeType = "text/html";
string formattedHtml = $"<table><thead><tr><th><i>key</i></th><th>value</th></tr></thead><tbody><tr><td>prop1</td><td>value1</td></tr><tr><td>prop2</td><td>value2</td></tr><tr><td>prop3</td><td>value3</td></tr></tbody></table>";
FormattedValue fv = new FormattedValue(mimeType, formattedHtml);
outputs.Should().SatisfyRespectively(
e => e.Should().BeOfType<CodeSubmissionReceived>(),
e => e.Should().BeOfType<CompleteCodeSubmissionReceived>(),
e => e.Should().BeOfType<DiagnosticsProduced>().Which.Diagnostics.Count.Should().Be(0),
e => e.Should().BeOfType<DisplayedValueProduced>().Which.FormattedValues.ElementAt(0).Should().BeEquivalentToRespectingRuntimeTypes(fv),
e => e.Should().BeOfType<CommandSucceeded>()
);
}
[Fact]
public async Task RequestValueInfos_only_returns_user_defined_values()
{
using var kernel = CreateKernel(Language.PowerShell);
await kernel.SendAsync(new SubmitCode("$theAnswer = 42"));
var result = await kernel.SendAsync(new RequestValueInfos());
var events = result.KernelEvents.ToSubscribedList();
events
.Should()
.ContainSingle<ValueInfosProduced>()
.Which
.ValueInfos
.Should()
.ContainSingle()
.Which
.Name
.Should()
.Be("theAnswer");
}
}
}