Default to DefaultConsoleWidth when DotNet says WindowWidth is 0 (#7465)

Default to DefaultConsoleWidth when DotNet says WindowWidth is 0

This resolves an issue in an environment like VSTS when pwsh is spawned and DotNet is not able to determine the Console Width.
* Port changes from #6883 by @kalgiz
This commit is contained in:
Travis Plunk 2018-08-07 15:27:42 -07:00 committed by GitHub
parent 1523c218a6
commit d29349bb4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 1 deletions

View file

@ -761,6 +761,11 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
/// </summary>
static private int GetConsoleWindowWidth(int columnNumber)
{
if (InternalTestHooks.SetConsoleWidthToZero)
{
return DefaultConsoleWidth;
}
if (columnNumber == int.MaxValue)
{
if (_noConsole)
@ -769,7 +774,9 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
}
try
{
return Console.WindowWidth;
// if Console width is set to 0, the default width is returned so that the output string is not null.
// This can happen in environments where TERM is not set.
return (Console.WindowWidth != 0) ? Console.WindowWidth : DefaultConsoleWidth;
}
catch
{

View file

@ -1387,6 +1387,7 @@ namespace System.Management.Automation.Internal
// Simulate 'System.Diagnostics.Stopwatch.IsHighResolution is false' to test Get-Uptime throw
internal static bool StopwatchIsNotHighResolution;
internal static bool DisableGACLoading;
internal static bool SetConsoleWidthToZero;
// A location to test PSEdition compatibility functionality for Windows PowerShell modules with
// since we can't manipulate the System32 directory in a test

View file

@ -800,4 +800,18 @@ A Name B
$output = $obj | Format-Table -Wrap | Out-String
$output.Replace("`r","").Replace(" ",".").Replace("`n","^") | Should -BeExactly $expectedTable.Replace("`r","").Replace(" ",".").Replace("`n","^")
}
It "Should not return null when the Console width is equal to 0" {
[system.management.automation.internal.internaltesthooks]::SetTestHook('SetConsoleWidthToZero', $true)
try
{
# Fill the console window with the string, so that it reaches its max width.
# Check if the max width is equal to default value (120), to test test hook set.
$testObject = @{ test = '1' * 200}
Format-table -inputobject $testObject | Out-String -Stream | ForEach-Object{$_.length} | Sort-Object -Bottom 1 | Should -Be 120
}
finally {
[system.management.automation.internal.internaltesthooks]::SetTestHook('SetConsoleWidthToZero', $false)
}
}
}