From 7cc8a9ad32f8f83c48250a1a818d6ae7d72a0f8a Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 26 Mar 2024 07:19:12 +1000 Subject: [PATCH] Emit ProgressRecord in CLIXML minishell output Instead of never emitting a ProgressRecord if stdout is redirected, continue to emit the CLIXML serialized record when pwsh was started with -OutputFormat xml regardless of redirection. This allows calling processes to receive the progress record from the spawned process when it captures the output. --- .../host/msh/ConsoleHostUserInterface.cs | 13 ++++++------ test/powershell/Host/ConsoleHost.Tests.ps1 | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 6e3359275e..25c1553006 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -1331,13 +1331,6 @@ namespace Microsoft.PowerShell { Dbg.Assert(record != null, "WriteProgress called with null ProgressRecord"); - if (Console.IsOutputRedirected) - { - // Do not write progress bar when the stdout is redirected. - return; - } - - // We allow only one thread at a time to update the progress state.) if (_parent.ErrorFormat == Serialization.DataFormat.XML) { PSObject obj = new PSObject(); @@ -1345,8 +1338,14 @@ namespace Microsoft.PowerShell obj.Properties.Add(new PSNoteProperty("Record", record)); _parent.ErrorSerializer.Serialize(obj, "progress"); } + else if (Console.IsOutputRedirected) + { + // Do not write progress bar when the stdout is redirected. + return; + } else { + // We allow only one thread at a time to update the progress state.) lock (_instanceLock) { HandleIncomingProgressRecord(sourceId, record); diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 9a54630290..ec0e80de5c 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -435,6 +435,27 @@ export $envVarName='$guid' $out = $out.Split([Environment]::NewLine)[0] [System.Management.Automation.Internal.StringDecorated]::new($out).ToString("PlainText") | Should -BeExactly "Exception: boom" } + + It "Progress is not emitted when stdout is redirected" { + $ps = [powershell]::Create() + $null = $ps.AddScript('$a = & ([Environment]::ProcessPath) -Command "Write-Progress -Activity progress"; $a') + $actual = $ps.Invoke() + + $ps.HadErrors | Should -BeFalse + $actual | Should -BeNullOrEmpty + $ps.Streams.Progress | Should -BeNullOrEmpty + } + + It "Progress is still emitted with redireciton with XML output" { + $ps = [powershell]::Create() + $null = $ps.AddScript('$a = & ([Environment]::ProcessPath) -OutputFormat xml -Command "Write-Progress -Activity progress"; $a') + $actual = $ps.Invoke() + + $ps.HadErrors | Should -BeFalse + $actual | Should -BeNullOrEmpty + $ps.Streams.Progress.Count | Should -Be 1 + $ps.Streams.Progress[0].Activity | Should -Be progress + } } Context "Redirected standard output" {