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.
This commit is contained in:
Jordan Borean
2024-12-11 20:22:43 -05:00
committed by Patrick Meinecke
parent 36740ab4a2
commit 7cc8a9ad32
2 changed files with 27 additions and 7 deletions
@@ -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);
@@ -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" {