Suppress Write-Progress in ConsoleHost if output is redirected and fix tests (#14716)

This commit is contained in:
Steve Lee 2021-02-08 09:42:53 -08:00 committed by GitHub
parent 6ad5f65e18
commit 6d7fbed02b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 26 deletions

View file

@ -1304,33 +1304,34 @@ namespace Microsoft.PowerShell
/// </summary>
public override void WriteProgress(Int64 sourceId, ProgressRecord record)
{
if (record == null)
Dbg.Assert(record != null, "WriteProgress called with null ProgressRecord");
if (Console.IsOutputRedirected)
{
Dbg.Assert(false, "WriteProgress called with null ProgressRecord");
// Do not write progress bar when the stdout is redirected.
return;
}
bool matchPattern;
string currentOperation = HostUtilities.RemoveIdentifierInfoFromMessage(record.CurrentOperation, out matchPattern);
if (matchPattern)
{
record = new ProgressRecord(record) { CurrentOperation = currentOperation };
}
// We allow only one thread at a time to update the progress state.)
if (_parent.ErrorFormat == Serialization.DataFormat.XML)
{
PSObject obj = new PSObject();
obj.Properties.Add(new PSNoteProperty("SourceId", sourceId));
obj.Properties.Add(new PSNoteProperty("Record", record));
_parent.ErrorSerializer.Serialize(obj, "progress");
}
else
{
bool matchPattern;
string currentOperation = HostUtilities.RemoveIdentifierInfoFromMessage(record.CurrentOperation, out matchPattern);
if (matchPattern)
lock (_instanceLock)
{
record = new ProgressRecord(record) { CurrentOperation = currentOperation };
}
// We allow only one thread at a time to update the progress state.)
if (_parent.ErrorFormat == Serialization.DataFormat.XML)
{
PSObject obj = new PSObject();
obj.Properties.Add(new PSNoteProperty("SourceId", sourceId));
obj.Properties.Add(new PSNoteProperty("Record", record));
_parent.ErrorSerializer.Serialize(obj, "progress");
}
else
{
lock (_instanceLock)
{
HandleIncomingProgressRecord(sourceId, record);
}
HandleIncomingProgressRecord(sourceId, record);
}
}
}

View file

@ -302,6 +302,7 @@ namespace Microsoft.PowerShell
if (_content is not null)
{
Console.CursorVisible = false;
var currentPosition = _rawui.CursorPosition;
_rawui.CursorPosition = _location;

View file

@ -116,7 +116,7 @@ Describe "Verify Markdown Links" {
catch [Microsoft.PowerShell.Commands.HttpResponseException]
{
if ( $allowedFailures -notcontains $_.Exception.Response.StatusCode ) {
throw "Failed to complete request to `"$url`". $($_.Exception.Message)"
throw "Failed to complete request to `"$url`". $($_.Exception.Response.StatusCode) $($_.Exception.Message)"
}
}
}

View file

@ -473,8 +473,8 @@ Describe "Additional tests for Import-Module with WinCompat" -Tag "Feature" {
It "Verify that Warning is generated with default WarningAction" {
$LogPath = Join-Path $TestDrive (New-Guid).ToString()
& $pwsh -NoProfile -NonInteractive -c "[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('TestWindowsPowerShellPSHomeLocation', `'$basePath`');Import-Module $ModuleName" *> $LogPath
$LogPath | Should -FileContentMatch 'loaded in Windows PowerShell'
& $pwsh -NoProfile -NonInteractive -c "[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('TestWindowsPowerShellPSHomeLocation', `'$basePath`');Import-Module $ModuleName;Get-Error" *> $LogPath
$LogPath | Should -FileContentMatch 'loaded in Windows PowerShell' -Because (Get-Content $LogPath)
}
It "Verify that Error is Not generated with -ErrorAction Ignore" {

View file

@ -72,7 +72,7 @@ Describe "WinRM based remoting session abrupt disconnect" -Tags 'Feature','Requi
} -ErrorAction SilentlyContinue
# Session should be disconnected.
$session.State | Should -BeExactly 'Disconnected'
$session.State | Should -BeLikeExactly 'Disconnect*'
# A disconnected job should have been created for reconnect.
$script:job = Get-Job | Where-Object { $_.ChildJobs[0].Runspace.Id -eq $session.Runspace.Id }