mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 12:45:47 +00:00
`Read-Host` calls into `$Host.UI.Prompt()`. However, this method is also used when the host prompts for mandatory parameters that aren't provided. The method expects to be called when given a `FieldDescription` and if the input starts with `!` it enters `CommandPromptMode`. In this mode, you can type `!?` to request help, for example. However this mode is not something you can use via `Read-Host` (only if calling `$Host.UI.Prompt()` directly passing in a well constructed `FieldDescription`). When using `Read-Host -Prompt`, the cmdlet creates a `FieldDescription` where the name is the prompt and the rest of the properties are empty. The fix is that if `Label` is empty, we can assume it's being called from `Read-Host` rather than being called to prompt for a mandatory parameter and thus not enter `CommandPromptMode`.
53 lines
1.6 KiB
PowerShell
53 lines
1.6 KiB
PowerShell
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
# Licensed under the MIT License.
|
|
Describe "Read-Host Test" -tag "CI" {
|
|
BeforeAll {
|
|
$th = New-TestHost
|
|
$rs = [runspacefactory]::Createrunspace($th)
|
|
$rs.open()
|
|
$ps = [powershell]::Create()
|
|
$ps.Runspace = $rs
|
|
$ps.Commands.Clear()
|
|
}
|
|
|
|
AfterEach {
|
|
$ps.Commands.Clear()
|
|
}
|
|
|
|
AfterAll {
|
|
$rs.Close()
|
|
$rs.Dispose()
|
|
$ps.Dispose()
|
|
}
|
|
|
|
It "Read-Host returns expected string" {
|
|
$result = $ps.AddCommand("Read-Host").Invoke()
|
|
$result | Should -Be $th.UI.ReadLineData
|
|
}
|
|
|
|
It "Read-Host sets the prompt correctly" {
|
|
$result = $ps.AddScript("Read-Host -prompt myprompt").Invoke()
|
|
$prompt = $th.ui.streams.prompt[0]
|
|
$prompt | Should -Not -BeNullOrEmpty
|
|
$prompt.split(":")[-1] | Should -Be myprompt
|
|
}
|
|
|
|
It "Read-Host returns a secure string when using -AsSecureString parameter" {
|
|
$result = $ps.AddScript("Read-Host -AsSecureString").Invoke() | select-object -first 1
|
|
$result | Should -BeOfType SecureString
|
|
[pscredential]::New("foo",$result).GetNetworkCredential().Password | Should -BeExactly TEST
|
|
}
|
|
|
|
It "Read-Host doesn't enter command prompt mode" {
|
|
$result = "!1" | pwsh -NoProfile -c "Read-host -Prompt 'foo'"
|
|
if ($IsWindows) {
|
|
# Windows write to console directly so can't capture prompt in stdout
|
|
$expected = @('!1','!1')
|
|
}
|
|
else {
|
|
$expected = @('foo: !1','!1')
|
|
}
|
|
$result | should -BeExactly $expected
|
|
}
|
|
}
|