Fix ConciseView where error message is wider than window width and doesn't have whitespace (#11880)

This commit is contained in:
Steve Lee
2020-02-20 08:38:28 +05:00
committed by GitHub
parent 8e68397228
commit 34f9b43514
2 changed files with 27 additions and 9 deletions
@@ -1151,12 +1151,10 @@ namespace System.Management.Automation.Runspaces
# replace newlines in message so it lines up correct
$message = $message.Replace($newline, ' ').Replace(""`t"", ' ')
try {
$windowWidth = [Console]::WindowWidth
}
catch {
# fails if there is no console
$windowWidth = 120
$windowWidth = 120
if ($Host.UI.RawUI -ne $null) {
$windowWidth = $Host.UI.RawUI.WindowSize.Width
}
if ($windowWidth -gt 0 -and ($message.Length - $prefixVTLength) -gt $windowWidth) {
@@ -1168,9 +1166,17 @@ namespace System.Management.Automation.Runspaces
while (($remainingMessage.Length + $prefixLength) -gt $windowWidth) {
$subMessage = $prefix + $remainingMessage
$substring = Get-TruncatedString -string $subMessage -length ($windowWidth + $prefixVtLength)
$null = $sb.Append($substring)
$null = $sb.Append($newline)
$remainingMessage = $remainingMessage.Substring($substring.Length - $prefix.Length).Trim()
if ($substring.Length - $prefix.Length -gt 0)
{
$null = $sb.Append($substring)
$null = $sb.Append($newline)
$remainingMessage = $remainingMessage.Substring($substring.Length - $prefix.Length).Trim()
}
else
{
break
}
}
$null = $sb.Append($prefix + $remainingMessage.Trim())
$message = $sb.ToString()
@@ -87,6 +87,18 @@ Describe 'Tests for $ErrorView' -Tag CI {
# validate line number is shown
$e | Should -BeLike '* 2 *'
}
It "Long exception message gets rendered" {
$msg = "1234567890"
while ($msg.Length -le $Host.UI.RawUI.WindowSize.Width)
{
$msg += $msg
}
$e = { throw "$msg" } | Should -Throw $msg -PassThru | Out-String
$e | Should -BeLike "*$msg*"
}
}
Context 'NormalView tests' {