Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -761,14 +761,25 @@ 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)
{
return DefaultConsoleWidth;
}

try
{
// if Console width is set to 0, the default width is returned so that the output string is not null.
// Fix added because console width on VSTS is equal to 0.
if (Console.WindowWidth == 0)
{
return DefaultConsoleWidth;
}
return Console.WindowWidth;
}
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 @@ -1509,6 +1509,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;

/// <summary>This member is used for internal test purposes.</summary>
public static void SetTestHook(string property, object value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,4 +717,19 @@ abc bcd
$output = $obj | Format-Table | 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 -str | ForEach-Object {$_.length} | Sort-Object | Select-Object -Last 1 | Should -Be 120
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-str should be -Stream

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kalgiz Can you make these changes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adityapatwardhan can you make that change in this PR so we can still take this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format-Table -inputobject $testObject | Should -Not -BeNullOrEmpty
}
finally {
[system.management.automation.internal.internaltesthooks]::SetTestHook('SetConsoleWidthToZero', $false)
}
}
}