-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathPsesInternalHostTests.cs
162 lines (138 loc) · 6.2 KB
/
PsesInternalHostTests.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Console;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Xunit;
namespace Microsoft.PowerShell.EditorServices.Test.Console
{
using System.Management.Automation;
using System.Management.Automation.Runspaces;
[Trait("Category", "PsesInternalHost")]
public class PsesInternalHostTests : IDisposable
{
private readonly PsesInternalHost psesHost;
public PsesInternalHostTests()
{
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
}
public void Dispose()
{
psesHost.StopAsync().Wait();
GC.SuppressFinalize(this);
}
[Fact]
public async Task CanExecutePSCommand()
{
Assert.True(psesHost.IsRunning);
var command = new PSCommand().AddScript("$a = \"foo\"; $a");
var task = psesHost.ExecutePSCommandAsync<string>(command, CancellationToken.None);
var result = await task.ConfigureAwait(true);
Assert.Equal("foo", result[0]);
}
[Fact] // https://github.com/PowerShell/vscode-powershell/issues/3677
public async Task CanHandleThrow()
{
await psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("throw"),
CancellationToken.None,
new PowerShellExecutionOptions { ThrowOnError = false }).ConfigureAwait(true);
}
[Fact]
public async Task CanQueueParallelPSCommands()
{
// Concurrently initiate 4 requests in the session.
Task taskOne = psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("$x = 100"),
CancellationToken.None);
Task taskTwo = psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("$x += 200"),
CancellationToken.None);
Task taskThree = psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("$x = $x / 100"),
CancellationToken.None);
Task<IReadOnlyList<int>> resultTask = psesHost.ExecutePSCommandAsync<int>(
new PSCommand().AddScript("$x"),
CancellationToken.None);
// Wait for all of the executes to complete.
await Task.WhenAll(taskOne, taskTwo, taskThree, resultTask).ConfigureAwait(true);
// Sanity checks
Assert.Equal(RunspaceState.Opened, psesHost.Runspace.RunspaceStateInfo.State);
// 100 + 200 = 300, then divided by 100 is 3. We are ensuring that
// the commands were executed in the sequence they were called.
Assert.Equal(3, (await resultTask.ConfigureAwait(true))[0]);
}
[Fact]
public async Task CanCancelExecutionWithToken()
{
await Assert.ThrowsAsync<TaskCanceledException>(() =>
{
return psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("Start-Sleep 10"),
new CancellationTokenSource(1000).Token);
}).ConfigureAwait(true);
}
[Fact]
public async Task CanCancelExecutionWithMethod()
{
var executeTask = psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("Start-Sleep 10"),
CancellationToken.None);
// Wait until our task has started.
Thread.Sleep(2000);
psesHost.CancelCurrentTask();
await Assert.ThrowsAsync<TaskCanceledException>(() => executeTask).ConfigureAwait(true);
Assert.True(executeTask.IsCanceled);
}
[Fact]
public async Task CanResolveAndLoadProfilesForHostId()
{
string[] expectedProfilePaths =
new string[]
{
PsesHostFactory.TestProfilePaths.AllUsersAllHosts,
PsesHostFactory.TestProfilePaths.AllUsersCurrentHost,
PsesHostFactory.TestProfilePaths.CurrentUserAllHosts,
PsesHostFactory.TestProfilePaths.CurrentUserCurrentHost
};
// Load the profiles for the test host name
await psesHost.LoadHostProfilesAsync(CancellationToken.None).ConfigureAwait(true);
// Ensure that all the paths are set in the correct variables
// and that the current user's host profile got loaded
PSCommand psCommand = new PSCommand().AddScript(
"\"$($profile.AllUsersAllHosts) " +
"$($profile.AllUsersCurrentHost) " +
"$($profile.CurrentUserAllHosts) " +
"$($profile.CurrentUserCurrentHost) " +
"$(Assert-ProfileLoaded)\"");
var result = await psesHost.ExecutePSCommandAsync<string>(psCommand, CancellationToken.None).ConfigureAwait(true);
string expectedString =
string.Format(
"{0} True",
string.Join(
" ",
expectedProfilePaths));
Assert.Equal(expectedString, result[0], ignoreCase: true);
}
[Fact]
public async Task CanLoadPSReadLine()
{
// NOTE: This is slightly more complicated than one would expect because we explicitly
// need it to run on the pipeline thread otherwise Windows complains about the the
// thread's appartment state not matching.
Assert.True(await psesHost.ExecuteDelegateAsync(
nameof(psesHost.TryLoadPSReadLine),
executionOptions: null,
(pwsh, _) => psesHost.TryLoadPSReadLine(
pwsh,
(EngineIntrinsics)pwsh.Runspace.SessionStateProxy.GetVariable("ExecutionContext"),
out IReadLine readLine),
CancellationToken.None).ConfigureAwait(true));
}
}
}