Skip to content

Commit 6d7fbed

Browse files
authored
Suppress Write-Progress in ConsoleHost if output is redirected and fix tests (#14716)
1 parent 6ad5f65 commit 6d7fbed

5 files changed

Lines changed: 28 additions & 26 deletions

File tree

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,33 +1304,34 @@ public override void WriteWarningLine(string message)
13041304
/// </summary>
13051305
public override void WriteProgress(Int64 sourceId, ProgressRecord record)
13061306
{
1307-
if (record == null)
1307+
Dbg.Assert(record != null, "WriteProgress called with null ProgressRecord");
1308+
1309+
if (Console.IsOutputRedirected)
13081310
{
1309-
Dbg.Assert(false, "WriteProgress called with null ProgressRecord");
1311+
// Do not write progress bar when the stdout is redirected.
1312+
return;
13101313
}
1311-
else
1314+
1315+
bool matchPattern;
1316+
string currentOperation = HostUtilities.RemoveIdentifierInfoFromMessage(record.CurrentOperation, out matchPattern);
1317+
if (matchPattern)
13121318
{
1313-
bool matchPattern;
1314-
string currentOperation = HostUtilities.RemoveIdentifierInfoFromMessage(record.CurrentOperation, out matchPattern);
1315-
if (matchPattern)
1316-
{
1317-
record = new ProgressRecord(record) { CurrentOperation = currentOperation };
1318-
}
1319+
record = new ProgressRecord(record) { CurrentOperation = currentOperation };
1320+
}
13191321

1320-
// We allow only one thread at a time to update the progress state.)
1321-
if (_parent.ErrorFormat == Serialization.DataFormat.XML)
1322-
{
1323-
PSObject obj = new PSObject();
1324-
obj.Properties.Add(new PSNoteProperty("SourceId", sourceId));
1325-
obj.Properties.Add(new PSNoteProperty("Record", record));
1326-
_parent.ErrorSerializer.Serialize(obj, "progress");
1327-
}
1328-
else
1322+
// We allow only one thread at a time to update the progress state.)
1323+
if (_parent.ErrorFormat == Serialization.DataFormat.XML)
1324+
{
1325+
PSObject obj = new PSObject();
1326+
obj.Properties.Add(new PSNoteProperty("SourceId", sourceId));
1327+
obj.Properties.Add(new PSNoteProperty("Record", record));
1328+
_parent.ErrorSerializer.Serialize(obj, "progress");
1329+
}
1330+
else
1331+
{
1332+
lock (_instanceLock)
13291333
{
1330-
lock (_instanceLock)
1331-
{
1332-
HandleIncomingProgressRecord(sourceId, record);
1333-
}
1334+
HandleIncomingProgressRecord(sourceId, record);
13341335
}
13351336
}
13361337
}

src/Microsoft.PowerShell.ConsoleHost/host/msh/ProgressPane.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ private void WriteContent()
302302
if (_content is not null)
303303
{
304304
Console.CursorVisible = false;
305+
305306
var currentPosition = _rawui.CursorPosition;
306307
_rawui.CursorPosition = _location;
307308

test/common/markdown/markdown-link.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Describe "Verify Markdown Links" {
116116
catch [Microsoft.PowerShell.Commands.HttpResponseException]
117117
{
118118
if ( $allowedFailures -notcontains $_.Exception.Response.StatusCode ) {
119-
throw "Failed to complete request to `"$url`". $($_.Exception.Message)"
119+
throw "Failed to complete request to `"$url`". $($_.Exception.Response.StatusCode) $($_.Exception.Message)"
120120
}
121121
}
122122
}

test/powershell/Modules/Microsoft.PowerShell.Core/CompatiblePSEditions.Module.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,8 @@ Describe "Additional tests for Import-Module with WinCompat" -Tag "Feature" {
473473

474474
It "Verify that Warning is generated with default WarningAction" {
475475
$LogPath = Join-Path $TestDrive (New-Guid).ToString()
476-
& $pwsh -NoProfile -NonInteractive -c "[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('TestWindowsPowerShellPSHomeLocation', `'$basePath`');Import-Module $ModuleName" *> $LogPath
477-
$LogPath | Should -FileContentMatch 'loaded in Windows PowerShell'
476+
& $pwsh -NoProfile -NonInteractive -c "[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('TestWindowsPowerShellPSHomeLocation', `'$basePath`');Import-Module $ModuleName;Get-Error" *> $LogPath
477+
$LogPath | Should -FileContentMatch 'loaded in Windows PowerShell' -Because (Get-Content $LogPath)
478478
}
479479

480480
It "Verify that Error is Not generated with -ErrorAction Ignore" {

test/powershell/engine/Remoting/RemoteSession.Disconnect.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Describe "WinRM based remoting session abrupt disconnect" -Tags 'Feature','Requi
7272
} -ErrorAction SilentlyContinue
7373

7474
# Session should be disconnected.
75-
$session.State | Should -BeExactly 'Disconnected'
75+
$session.State | Should -BeLikeExactly 'Disconnect*'
7676

7777
# A disconnected job should have been created for reconnect.
7878
$script:job = Get-Job | Where-Object { $_.ChildJobs[0].Runspace.Id -eq $session.Runspace.Id }

0 commit comments

Comments
 (0)