Skip to content

Commit

Permalink
Emit ProgressRecord in CLIXML minishell output
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jborean93 committed May 23, 2024
1 parent 74d8bdb commit 4bcecb2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1331,22 +1331,21 @@ public override void WriteProgress(long sourceId, ProgressRecord record)
{
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();
obj.Properties.Add(new PSNoteProperty("SourceId", sourceId));
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);
Expand Down
21 changes: 21 additions & 0 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down

0 comments on commit 4bcecb2

Please sign in to comment.