Fix multi-line input w/ redirected stdin (#2090)
The input loop reading from stdin did not handle multi-line input correctly because it was adding a newline character where none was expected. The fix was to not include the final newline character to accept input, just like Console.ReadLine or PSReadline would.
This commit is contained in:
parent
22aac12c3e
commit
cf18010ad5
|
@ -1557,6 +1557,7 @@ namespace Microsoft.PowerShell
|
|||
if (initialContent != null)
|
||||
{
|
||||
sb.Append(initialContent);
|
||||
sb.Append('\n');
|
||||
}
|
||||
|
||||
while (true)
|
||||
|
@ -1579,7 +1580,11 @@ namespace Microsoft.PowerShell
|
|||
if (!NoPrompt) Console.Out.Write('\n');
|
||||
Console.In.Read();
|
||||
}
|
||||
sb.Append('\n');
|
||||
break;
|
||||
}
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -1591,12 +1596,6 @@ namespace Microsoft.PowerShell
|
|||
{
|
||||
sb.Append(c);
|
||||
}
|
||||
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
|
|
|
@ -263,8 +263,23 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
|
|||
$si = NewProcessStartInfo "-noprofile -" -RedirectStdIn
|
||||
$process = RunPowerShell $si
|
||||
$process.StandardInput.Write("1+1`n")
|
||||
$process.StandardOutput.ReadLine() | Should Be "2"
|
||||
|
||||
# Multi-line input
|
||||
$process.StandardInput.Write("if (1)`n{`n 42`n}`n`n")
|
||||
$process.StandardOutput.ReadLine() | Should Be "42"
|
||||
$process.StandardInput.Write(@"
|
||||
function foo
|
||||
{
|
||||
'in foo'
|
||||
}
|
||||
|
||||
foo
|
||||
|
||||
"@)
|
||||
$process.StandardOutput.ReadLine() | Should Be "in foo"
|
||||
|
||||
$process.StandardInput.Close()
|
||||
$process.StandardOutput.ReadToEnd() | Should Be "2${nl}"
|
||||
EnsureChildHasExited $process
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue