Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,11 @@ private void ProcessCachedGroupOnWide(WideViewHeaderInfo wvhi, List<PacketInfoDa
/// </summary>
static private int GetConsoleWindowWidth(int columnNumber)
{
if (InternalTestHooks.SetConsoleWidthToZero)
{
return DefaultConsoleWidth;
}

if (columnNumber == int.MaxValue)
{
if (_noConsole)
Expand All @@ -769,7 +774,9 @@ static private int GetConsoleWindowWidth(int columnNumber)
}
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
{
Expand Down
1 change: 1 addition & 0 deletions src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,7 @@ public static class InternalTestHooks
// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}