-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable
PsesInternalHostTests
(previously PowerShellContextTests
)
- Loading branch information
1 parent
bb17dbc
commit 7c1a5cc
Showing
3 changed files
with
164 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 0 additions & 178 deletions
178
test/PowerShellEditorServices.Test/Session/PowerShellContextTests.cs
This file was deleted.
Oops, something went wrong.
150 changes: 150 additions & 0 deletions
150
test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
// 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.Host; | ||
using Xunit; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Test.Console | ||
{ | ||
using System.Management.Automation; | ||
using System.Management.Automation.Runspaces; | ||
|
||
public class PsesInternalHostTests : IDisposable | ||
{ | ||
private readonly PsesInternalHost psesHost; | ||
|
||
public PsesInternalHostTests() | ||
{ | ||
psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
psesHost.StopAsync().Wait(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
[Trait("Category", "PsesInternalHost")] | ||
[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]); | ||
} | ||
|
||
[Trait("Category", "PsesInternalHost")] | ||
[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]); | ||
} | ||
|
||
[Trait("Category", "PsesInternalHost")] | ||
[Fact] | ||
public async Task CanCancelExecutionWithToken() | ||
{ | ||
_ = await Assert.ThrowsAsync<TaskCanceledException>(() => | ||
{ | ||
return psesHost.ExecutePSCommandAsync( | ||
new PSCommand().AddScript("Start-Sleep 10"), | ||
new CancellationTokenSource(1000).Token); | ||
}).ConfigureAwait(true); | ||
} | ||
|
||
[Trait("Category", "PsesInternalHost")] | ||
[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); | ||
} | ||
|
||
|
||
[Trait("Category", "PsesInternalHost")] | ||
[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); | ||
} | ||
|
||
[Trait("Category", "PSReadLine")] | ||
[Fact] | ||
public void CanLoadPSReadLine() | ||
{ | ||
Runspace runspace = psesHost.CreateInitialRunspace(psesHost._hostInfo.InitialSessionState); | ||
PowerShell pwsh = PsesInternalHost.CreatePowerShellForRunspace(runspace); | ||
var engineIntrinsics = (EngineIntrinsics)runspace.SessionStateProxy.GetVariable("ExecutionContext"); | ||
Assert.True(psesHost.TryLoadPSReadLine(pwsh, engineIntrinsics, out _)); | ||
} | ||
} | ||
} |