|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +Describe "Remote host method call tests" -Tag Feature { |
| 5 | + BeforeAll { |
| 6 | + if (-not ('RemoteHostCallsTest.TestHost' -as [type])) { |
| 7 | + Add-Type -TypeDefinition @' |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Collections.ObjectModel; |
| 11 | +using System.Globalization; |
| 12 | +using System.Management.Automation; |
| 13 | +using System.Management.Automation.Host; |
| 14 | +using System.Security; |
| 15 | +
|
| 16 | +namespace RemoteHostCallsTest |
| 17 | +{ |
| 18 | + public class TestHostUserInterface : PSHostUserInterface, IHostUISupportsMultipleChoiceSelection |
| 19 | + { |
| 20 | + public int[] ReceivedDefaultChoices; |
| 21 | + public Collection<int> ChoicesToReturn = new Collection<int>(); |
| 22 | +
|
| 23 | + public override PSHostRawUserInterface RawUI => null; |
| 24 | + public override string ReadLine() => throw new NotImplementedException(); |
| 25 | + public override SecureString ReadLineAsSecureString() => throw new NotImplementedException(); |
| 26 | + public override void Write(string value) { } |
| 27 | + public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) { } |
| 28 | + public override void WriteLine(string value) { } |
| 29 | + public override void WriteErrorLine(string value) { } |
| 30 | + public override void WriteDebugLine(string message) { } |
| 31 | + public override void WriteProgress(long sourceId, ProgressRecord record) { } |
| 32 | + public override void WriteVerboseLine(string message) { } |
| 33 | + public override void WriteWarningLine(string message) { } |
| 34 | +
|
| 35 | + public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions) |
| 36 | + => throw new NotImplementedException(); |
| 37 | +
|
| 38 | + public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName) |
| 39 | + => throw new NotImplementedException(); |
| 40 | +
|
| 41 | + public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options) |
| 42 | + => throw new NotImplementedException(); |
| 43 | +
|
| 44 | + public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice) |
| 45 | + => throw new NotImplementedException(); |
| 46 | +
|
| 47 | + public Collection<int> PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, IEnumerable<int> defaultChoices) |
| 48 | + { |
| 49 | + ReceivedDefaultChoices = defaultChoices == null ? null : new List<int>(defaultChoices).ToArray(); |
| 50 | + return ChoicesToReturn; |
| 51 | + } |
| 52 | + } |
| 53 | +
|
| 54 | + public class TestHost : PSHost |
| 55 | + { |
| 56 | + private readonly TestHostUserInterface _ui = new TestHostUserInterface(); |
| 57 | + private readonly Guid _instanceId = Guid.NewGuid(); |
| 58 | +
|
| 59 | + public override CultureInfo CurrentCulture => CultureInfo.CurrentCulture; |
| 60 | + public override CultureInfo CurrentUICulture => CultureInfo.CurrentUICulture; |
| 61 | + public override Guid InstanceId => _instanceId; |
| 62 | + public override string Name => "RemoteHostCallsTest"; |
| 63 | + public override PSHostUserInterface UI => _ui; |
| 64 | + public override Version Version => new Version(1, 0); |
| 65 | + public override void EnterNestedPrompt() => throw new NotImplementedException(); |
| 66 | + public override void ExitNestedPrompt() => throw new NotImplementedException(); |
| 67 | + public override void NotifyBeginApplication() { } |
| 68 | + public override void NotifyEndApplication() { } |
| 69 | + public override void SetShouldExit(int exitCode) { } |
| 70 | + } |
| 71 | +} |
| 72 | +'@ |
| 73 | + } |
| 74 | + |
| 75 | + $testHost = [RemoteHostCallsTest.TestHost]::new() |
| 76 | + |
| 77 | + # Connecting to the current process over the named pipe gives a remote |
| 78 | + # runspace whose host calls are marshalled back to our custom host. |
| 79 | + $connInfo = [System.Management.Automation.Runspaces.NamedPipeConnectionInfo]::new($PID) |
| 80 | + $runspace = [runspacefactory]::CreateRunspace($testHost, $connInfo) |
| 81 | + $runspace.Open() |
| 82 | + } |
| 83 | + |
| 84 | + AfterAll { |
| 85 | + if ($runspace) { |
| 86 | + $runspace.Dispose() |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + Context "IHostUISupportsMultipleChoiceSelection.PromptForChoice" { |
| 91 | + BeforeEach { |
| 92 | + $testHost.UI.ReceivedDefaultChoices = $null |
| 93 | + $testHost.UI.ChoicesToReturn.Clear() |
| 94 | + } |
| 95 | + |
| 96 | + It "Passes default choices as <Name>" -TestCases @( |
| 97 | + @{ Name = 'int[]'; DefaultChoices = '[int[]]@(0, 2)'; Expected = @(0, 2) } |
| 98 | + @{ Name = 'List[int]'; DefaultChoices = '[System.Collections.Generic.List[int]]@(0, 2)'; Expected = @(0, 2) } |
| 99 | + @{ Name = 'Collection[int]'; DefaultChoices = '[System.Collections.ObjectModel.Collection[int]]@(0, 2)'; Expected = @(0, 2) } |
| 100 | + @{ Name = 'empty int[]'; DefaultChoices = '[int[]]@()'; Expected = @() } |
| 101 | + @{ Name = 'null'; DefaultChoices = '$null'; Expected = $null } |
| 102 | + ) { |
| 103 | + param ($DefaultChoices, $Expected) |
| 104 | + |
| 105 | + $testHost.UI.ChoicesToReturn.Add(1) |
| 106 | + $testHost.UI.ChoicesToReturn.Add(2) |
| 107 | + |
| 108 | + $ps = [PowerShell]::Create() |
| 109 | + try { |
| 110 | + $ps.Runspace = $runspace |
| 111 | + $null = $ps.AddScript(@" |
| 112 | +`$choices = [System.Collections.ObjectModel.Collection[System.Management.Automation.Host.ChoiceDescription]]@('&a', '&b', '&c') |
| 113 | +`$Host.UI.PromptForChoice('caption', 'message', `$choices, $DefaultChoices) |
| 114 | +"@) |
| 115 | + $actual = $ps.Invoke() |
| 116 | + |
| 117 | + $ps.Streams.Error | Should -BeNullOrEmpty |
| 118 | + $actual | Should -Be @(1, 2) |
| 119 | + |
| 120 | + if ($null -eq $Expected) { |
| 121 | + $null -eq $testHost.UI.ReceivedDefaultChoices | Should -BeTrue |
| 122 | + } |
| 123 | + else { |
| 124 | + $null -eq $testHost.UI.ReceivedDefaultChoices | Should -BeFalse |
| 125 | + $testHost.UI.ReceivedDefaultChoices -join ',' | Should -BeExactly ($Expected -join ',') |
| 126 | + } |
| 127 | + } |
| 128 | + finally { |
| 129 | + $ps.Dispose() |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments