Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit ProgressRecord in CLIXML minishell output #21373

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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