-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathPowerShellContextTests.cs
179 lines (147 loc) · 6.79 KB
/
PowerShellContextTests.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.PowerShellContext;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using Microsoft.PowerShell.EditorServices.Utility;
using Xunit;
namespace Microsoft.PowerShell.EditorServices.Test.Console
{
public class PowerShellContextTests : IDisposable
{
// Borrowed from `VersionUtils` which can't be used here due to an initialization problem.
private static bool IsWindows { get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
private PowerShellContextService powerShellContext;
private AsyncQueue<SessionStateChangedEventArgs> stateChangeQueue;
private static readonly string s_debugTestFilePath =
TestUtilities.NormalizePath("../../../../PowerShellEditorServices.Test.Shared/Debugging/DebugTest.ps1");
public PowerShellContextTests()
{
this.powerShellContext = PowerShellContextFactory.Create(NullLogger.Instance);
this.powerShellContext.SessionStateChanged += OnSessionStateChanged;
this.stateChangeQueue = new AsyncQueue<SessionStateChangedEventArgs>();
}
public void Dispose()
{
this.powerShellContext.Close();
this.powerShellContext = null;
}
[Trait("Category", "PowerShellContext")]
[Fact]
public async Task CanExecutePSCommand()
{
PSCommand psCommand = new PSCommand();
psCommand.AddScript("$a = \"foo\"; $a");
var executeTask =
this.powerShellContext.ExecuteCommandAsync<string>(psCommand);
await this.AssertStateChange(PowerShellContextState.Running);
await this.AssertStateChange(PowerShellContextState.Ready);
var result = await executeTask;
Assert.Equal("foo", result.First());
}
[Trait("Category", "PowerShellContext")]
[Fact]
public async Task CanQueueParallelRunspaceRequests()
{
// Concurrently initiate 4 requests in the session
Task taskOne = this.powerShellContext.ExecuteScriptStringAsync("$x = 100");
Task<RunspaceHandle> handleTask = this.powerShellContext.GetRunspaceHandleAsync();
Task taskTwo = this.powerShellContext.ExecuteScriptStringAsync("$x += 200");
Task taskThree = this.powerShellContext.ExecuteScriptStringAsync("$x = $x / 100");
PSCommand psCommand = new PSCommand();
psCommand.AddScript("$x");
Task<IEnumerable<int>> resultTask = this.powerShellContext.ExecuteCommandAsync<int>(psCommand);
// Wait for the requested runspace handle and then dispose it
RunspaceHandle handle = await handleTask;
handle.Dispose();
// Wait for all of the executes to complete
await Task.WhenAll(taskOne, taskTwo, taskThree, resultTask);
// At this point, the remaining command executions should execute and complete
int result = resultTask.Result.FirstOrDefault();
// 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, result);
}
[Trait("Category", "PowerShellContext")]
[Fact]
public async Task CanAbortExecution()
{
var executeTask =
Task.Run(
async () =>
{
var unusedTask = this.powerShellContext.ExecuteScriptWithArgsAsync(s_debugTestFilePath);
await Task.Delay(50);
this.powerShellContext.AbortExecution();
});
await this.AssertStateChange(PowerShellContextState.Running);
await this.AssertStateChange(PowerShellContextState.Aborting);
await this.AssertStateChange(PowerShellContextState.Ready);
await executeTask;
}
[Trait("Category", "PowerShellContext")]
[Fact]
public async Task CanResolveAndLoadProfilesForHostId()
{
string[] expectedProfilePaths =
new string[]
{
PowerShellContextFactory.TestProfilePaths.AllUsersAllHosts,
PowerShellContextFactory.TestProfilePaths.AllUsersCurrentHost,
PowerShellContextFactory.TestProfilePaths.CurrentUserAllHosts,
PowerShellContextFactory.TestProfilePaths.CurrentUserCurrentHost
};
// Load the profiles for the test host name
await this.powerShellContext.LoadHostProfilesAsync();
// 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();
psCommand.AddScript(
"\"$($profile.AllUsersAllHosts) " +
"$($profile.AllUsersCurrentHost) " +
"$($profile.CurrentUserAllHosts) " +
"$($profile.CurrentUserCurrentHost) " +
"$(Assert-ProfileLoaded)\"");
var result =
await this.powerShellContext.ExecuteCommandAsync<string>(
psCommand);
string expectedString =
string.Format(
"{0} True",
string.Join(
" ",
expectedProfilePaths));
Assert.Equal(expectedString, result.FirstOrDefault(), true);
}
[Trait("Category", "PSReadLine")]
[SkippableFact]
public async Task CanGetPSReadLineProxy()
{
Skip.If(IsWindows, "This test doesn't work on Windows for some reason.");
Assert.True(PSReadLinePromptContext.TryGetPSReadLineProxy(
NullLogger.Instance,
PowerShellContextFactory.initialRunspace,
out PSReadLineProxy proxy));
}
#region Helper Methods
private async Task AssertStateChange(PowerShellContextState expectedState)
{
SessionStateChangedEventArgs newState =
await this.stateChangeQueue.DequeueAsync();
Assert.Equal(expectedState, newState.NewSessionState);
}
private void OnSessionStateChanged(object sender, SessionStateChangedEventArgs e)
{
this.stateChangeQueue.EnqueueAsync(e).Wait();
}
#endregion
}
}