|
| 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