From 1bfa1c36a7a73cf0dadb3d6a1d9ffb71bcd43a67 Mon Sep 17 00:00:00 2001 From: Staffan Gustafsson Date: Sat, 31 Jul 2021 15:51:03 +0200 Subject: [PATCH] Forwarding progress stream changes from Foreach-Object -Parallel runspaces (#14271) --- .../engine/hostifaces/PSTask.cs | 10 ++++++++++ .../Foreach-Object-Parallel.Tests.ps1 | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/System.Management.Automation/engine/hostifaces/PSTask.cs b/src/System.Management.Automation/engine/hostifaces/PSTask.cs index 5f05d267fc..6e08733cc2 100644 --- a/src/System.Management.Automation/engine/hostifaces/PSTask.cs +++ b/src/System.Management.Automation/engine/hostifaces/PSTask.cs @@ -68,6 +68,7 @@ namespace System.Management.Automation.PSTasks _powershell.Streams.Warning.DataAdded += (sender, args) => HandleWarningData(); _powershell.Streams.Verbose.DataAdded += (sender, args) => HandleVerboseData(); _powershell.Streams.Debug.DataAdded += (sender, args) => HandleDebugData(); + _powershell.Streams.Progress.DataAdded += (sender, args) => HandleProgressData(); _powershell.Streams.Information.DataAdded += (sender, args) => HandleInformationData(); // State change handler @@ -132,6 +133,15 @@ namespace System.Management.Automation.PSTasks } } + private void HandleProgressData() + { + foreach (var item in _powershell.Streams.Progress.ReadAll()) + { + _dataStreamWriter.Add( + new PSStreamObject(PSStreamObjectType.Progress, item)); + } + } + #endregion #region Event handlers diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 index 46f85b80bd..994d71601a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Foreach-Object-Parallel.Tests.ps1 @@ -138,6 +138,19 @@ Describe 'ForEach-Object -Parallel Basic Tests' -Tags 'CI' { $actualDebug.Message | Should -BeExactly 'Debug!' } + It 'Verifies progress data streaming' { + + $ps = [Powershell]::Create([System.Management.Automation.RunspaceMode]::NewRunspace) + + $ps.Commands.AddScript("`$ProgressPreference='Continue'; 1..2 | ForEach-Object -Parallel { Write-Progress -Activity Progress -Status Running }") | Out-Null + $ps.Invoke() + $pr = $ps.Streams.Progress.ReadAll() + $pr.Count | Should -Be 2 + $pr[0].Activity | Should -Be Progress + $pr[0].StatusDescription | Should -Be Running + $ps.Dispose() + } + It 'Verifies information data streaming' { $actualInformation = 1..1 | ForEach-Object -Parallel { Write-Information "Information!" } 6>&1