Skip to content

Commit 7c1a5cc

Browse files
committed
Enable PsesInternalHostTests (previously PowerShellContextTests)
1 parent bb17dbc commit 7c1a5cc

File tree

3 files changed

+164
-188
lines changed

3 files changed

+164
-188
lines changed

Diff for: src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

+14-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal class PsesInternalHost : PSHost, IHostSupportsInteractiveSession, IRuns
4141

4242
private readonly ILanguageServerFacade _languageServer;
4343

44-
private readonly HostStartupInfo _hostInfo;
44+
internal readonly HostStartupInfo _hostInfo;
4545

4646
private readonly BlockingConcurrentDeque<ISynchronousTask> _taskQueue;
4747

@@ -216,12 +216,7 @@ public async Task<bool> TryStartAsync(HostStartOptions startOptions, Cancellatio
216216

217217
if (startOptions.LoadProfiles)
218218
{
219-
await ExecuteDelegateAsync(
220-
"LoadProfiles",
221-
new PowerShellExecutionOptions { MustRunInForeground = true, ThrowOnError = false },
222-
(pwsh, _) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
223-
cancellationToken).ConfigureAwait(false);
224-
219+
await LoadHostProfilesAsync(cancellationToken).ConfigureAwait(false);
225220
_logger.LogInformation("Profiles loaded");
226221
}
227222

@@ -391,6 +386,15 @@ public void InvokePSDelegate(string representation, ExecutionOptions executionOp
391386
task.ExecuteAndGetResult(cancellationToken);
392387
}
393388

389+
internal Task LoadHostProfilesAsync(CancellationToken cancellationToken)
390+
{
391+
return ExecuteDelegateAsync(
392+
"LoadProfiles",
393+
new PowerShellExecutionOptions { MustRunInForeground = true, ThrowOnError = false },
394+
(pwsh, _) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
395+
cancellationToken);
396+
}
397+
394398
public Task SetInitialWorkingDirectoryAsync(string path, CancellationToken cancellationToken)
395399
{
396400
InitialWorkingDirectory = path;
@@ -703,7 +707,7 @@ private static PowerShell CreateNestedPowerShell(RunspaceInfo currentRunspace)
703707
return pwsh;
704708
}
705709

706-
private static PowerShell CreatePowerShellForRunspace(Runspace runspace)
710+
internal static PowerShell CreatePowerShellForRunspace(Runspace runspace)
707711
{
708712
var pwsh = PowerShell.Create();
709713
pwsh.Runspace = runspace;
@@ -751,7 +755,7 @@ private static PowerShell CreatePowerShellForRunspace(Runspace runspace)
751755
return (pwsh, engineIntrinsics);
752756
}
753757

754-
private Runspace CreateInitialRunspace(InitialSessionState initialSessionState)
758+
internal Runspace CreateInitialRunspace(InitialSessionState initialSessionState)
755759
{
756760
Runspace runspace = RunspaceFactory.CreateRunspace(PublicHost, initialSessionState);
757761

@@ -919,7 +923,7 @@ private Task PopOrReinitializeRunspaceAsync()
919923
CancellationToken.None);
920924
}
921925

922-
private bool TryLoadPSReadLine(PowerShell pwsh, EngineIntrinsics engineIntrinsics, out IReadLine psrlReadLine)
926+
internal bool TryLoadPSReadLine(PowerShell pwsh, EngineIntrinsics engineIntrinsics, out IReadLine psrlReadLine)
923927
{
924928
psrlReadLine = null;
925929
try

Diff for: test/PowerShellEditorServices.Test/Session/PowerShellContextTests.cs

-178
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using Microsoft.Extensions.Logging.Abstractions;
9+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
10+
using Xunit;
11+
12+
namespace Microsoft.PowerShell.EditorServices.Test.Console
13+
{
14+
using System.Management.Automation;
15+
using System.Management.Automation.Runspaces;
16+
17+
public class PsesInternalHostTests : IDisposable
18+
{
19+
private readonly PsesInternalHost psesHost;
20+
21+
public PsesInternalHostTests()
22+
{
23+
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
24+
}
25+
26+
public void Dispose()
27+
{
28+
psesHost.StopAsync().Wait();
29+
GC.SuppressFinalize(this);
30+
}
31+
32+
[Trait("Category", "PsesInternalHost")]
33+
[Fact]
34+
public async Task CanExecutePSCommand()
35+
{
36+
Assert.True(psesHost.IsRunning);
37+
var command = new PSCommand().AddScript("$a = \"foo\"; $a");
38+
var task = psesHost.ExecutePSCommandAsync<string>(command, CancellationToken.None);
39+
var result = await task.ConfigureAwait(true);
40+
Assert.Equal("foo", result[0]);
41+
}
42+
43+
[Trait("Category", "PsesInternalHost")]
44+
[Fact]
45+
public async Task CanQueueParallelPSCommands()
46+
{
47+
// Concurrently initiate 4 requests in the session.
48+
Task taskOne = psesHost.ExecutePSCommandAsync(
49+
new PSCommand().AddScript("$x = 100"),
50+
CancellationToken.None);
51+
52+
Task taskTwo = psesHost.ExecutePSCommandAsync(
53+
new PSCommand().AddScript("$x += 200"),
54+
CancellationToken.None);
55+
56+
Task taskThree = psesHost.ExecutePSCommandAsync(
57+
new PSCommand().AddScript("$x = $x / 100"),
58+
CancellationToken.None);
59+
60+
Task<IReadOnlyList<int>> resultTask = psesHost.ExecutePSCommandAsync<int>(
61+
new PSCommand().AddScript("$x"),
62+
CancellationToken.None);
63+
64+
// Wait for all of the executes to complete.
65+
await Task.WhenAll(taskOne, taskTwo, taskThree, resultTask).ConfigureAwait(true);
66+
67+
// Sanity checks
68+
Assert.Equal(RunspaceState.Opened, psesHost.Runspace.RunspaceStateInfo.State);
69+
70+
// 100 + 200 = 300, then divided by 100 is 3. We are ensuring that
71+
// the commands were executed in the sequence they were called.
72+
Assert.Equal(3, (await resultTask.ConfigureAwait(true))[0]);
73+
}
74+
75+
[Trait("Category", "PsesInternalHost")]
76+
[Fact]
77+
public async Task CanCancelExecutionWithToken()
78+
{
79+
_ = await Assert.ThrowsAsync<TaskCanceledException>(() =>
80+
{
81+
return psesHost.ExecutePSCommandAsync(
82+
new PSCommand().AddScript("Start-Sleep 10"),
83+
new CancellationTokenSource(1000).Token);
84+
}).ConfigureAwait(true);
85+
}
86+
87+
[Trait("Category", "PsesInternalHost")]
88+
[Fact]
89+
public async Task CanCancelExecutionWithMethod()
90+
{
91+
var executeTask = psesHost.ExecutePSCommandAsync(
92+
new PSCommand().AddScript("Start-Sleep 10"),
93+
CancellationToken.None);
94+
95+
// Wait until our task has started.
96+
Thread.Sleep(2000);
97+
psesHost.CancelCurrentTask();
98+
_ = await Assert.ThrowsAsync<TaskCanceledException>(() => executeTask).ConfigureAwait(true);
99+
Assert.True(executeTask.IsCanceled);
100+
}
101+
102+
103+
[Trait("Category", "PsesInternalHost")]
104+
[Fact]
105+
public async Task CanResolveAndLoadProfilesForHostId()
106+
{
107+
string[] expectedProfilePaths =
108+
new string[]
109+
{
110+
PsesHostFactory.TestProfilePaths.AllUsersAllHosts,
111+
PsesHostFactory.TestProfilePaths.AllUsersCurrentHost,
112+
PsesHostFactory.TestProfilePaths.CurrentUserAllHosts,
113+
PsesHostFactory.TestProfilePaths.CurrentUserCurrentHost
114+
};
115+
116+
// Load the profiles for the test host name
117+
await psesHost.LoadHostProfilesAsync(CancellationToken.None).ConfigureAwait(true);
118+
119+
// Ensure that all the paths are set in the correct variables
120+
// and that the current user's host profile got loaded
121+
PSCommand psCommand = new PSCommand().AddScript(
122+
"\"$($profile.AllUsersAllHosts) " +
123+
"$($profile.AllUsersCurrentHost) " +
124+
"$($profile.CurrentUserAllHosts) " +
125+
"$($profile.CurrentUserCurrentHost) " +
126+
"$(Assert-ProfileLoaded)\"");
127+
128+
var result = await psesHost.ExecutePSCommandAsync<string>(psCommand, CancellationToken.None).ConfigureAwait(true);
129+
130+
string expectedString =
131+
string.Format(
132+
"{0} True",
133+
string.Join(
134+
" ",
135+
expectedProfilePaths));
136+
137+
Assert.Equal(expectedString, result[0], ignoreCase: true);
138+
}
139+
140+
[Trait("Category", "PSReadLine")]
141+
[Fact]
142+
public void CanLoadPSReadLine()
143+
{
144+
Runspace runspace = psesHost.CreateInitialRunspace(psesHost._hostInfo.InitialSessionState);
145+
PowerShell pwsh = PsesInternalHost.CreatePowerShellForRunspace(runspace);
146+
var engineIntrinsics = (EngineIntrinsics)runspace.SessionStateProxy.GetVariable("ExecutionContext");
147+
Assert.True(psesHost.TryLoadPSReadLine(pwsh, engineIntrinsics, out _));
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)