Fix the common parameter -ProgressAction for advanced functions (#24591)

Co-authored-by: Dongbo Wang <dongbow@microsoft.com>
This commit is contained in:
CM
2025-06-03 09:43:38 -07:00
committed by GitHub
co-authored by Dongbo Wang
parent 4e035461b6
commit b502098e3a
4 changed files with 49 additions and 1 deletions
+1 -1
View File
@@ -30,7 +30,7 @@ The PowerShell developer experience includes the **development of modules** (in
as well as the experience of **hosting PowerShell and its APIs** in other applications and language runtimes.
Special consideration should be given to topics like **backwards compatibility** with Windows PowerShell
(e.g. with **PowerShell Standard**) and **integration with related developer tools**
(e.g. .NET CLI or the PowerShell extension for VS Code).
(e.g. .NET CLI or the PowerShell extension for Visual Studio Code).
### Members
@@ -341,6 +341,7 @@ namespace System.Management.Automation
SpecialVariables.WarningPreference,
SpecialVariables.InformationPreference,
SpecialVariables.ConfirmPreference,
SpecialVariables.ProgressPreference,
};
internal static readonly Type[] PreferenceVariableTypes =
@@ -352,6 +353,7 @@ namespace System.Management.Automation
/* WarningPreference */ typeof(ActionPreference),
/* InformationPreference */ typeof(ActionPreference),
/* ConfirmPreference */ typeof(ConfirmImpact),
/* ProgressPreference */ typeof(ActionPreference),
};
// The following variables are created in every session w/ AllScope. We avoid creating local slots when we
@@ -833,6 +833,14 @@ namespace System.Management.Automation.Language
static Compiler()
{
Diagnostics.Assert(SpecialVariables.AutomaticVariables.Length == (int)AutomaticVariable.NumberOfAutomaticVariables
&& SpecialVariables.AutomaticVariableTypes.Length == (int)AutomaticVariable.NumberOfAutomaticVariables,
"The 'AutomaticVariable' enum length does not match both 'AutomaticVariables' and 'AutomaticVariableTypes' length.");
Diagnostics.Assert(Enum.GetNames(typeof(PreferenceVariable)).Length == SpecialVariables.PreferenceVariables.Length
&& Enum.GetNames(typeof(PreferenceVariable)).Length == SpecialVariables.PreferenceVariableTypes.Length,
"The 'PreferenceVariable' enum length does not match both 'PreferenceVariables' and 'PreferenceVariableTypes' length.");
s_functionContext = Expression.Parameter(typeof(FunctionContext), "funcContext");
s_executionContextParameter = Expression.Variable(typeof(ExecutionContext), "context");
@@ -147,6 +147,44 @@ Describe "Common parameters support for script cmdlets" -Tags "CI" {
}
}
Context "ProgressAction" {
It "Ignores progress actions on advanced script function with no variables" {
$ps.AddScript(
@'
function test-function {
[CmdletBinding()]param()
Write-Progress "progress foo"
}
test-function -ProgressAction Ignore
'@).Invoke()
$ps.Streams.Progress.Count | Should -Be 0
$ps.Streams.Error | ForEach-Object {
Write-Error -ErrorRecord $_ -ErrorAction Stop
}
}
It "Ignores progress actions on advanced script function with variables" {
$ps.AddScript(
@'
function test-function {
[CmdletBinding()]param([string]$path)
switch($false) { default { "echo $path" } }
Write-Progress "progress foo"
}
test-function -ProgressAction Ignore
'@).Invoke()
$ps.Streams.Progress.Count | Should -Be 0
$ps.Streams.Error | ForEach-Object {
Write-Error -ErrorRecord $_ -ErrorAction Stop
}
}
}
Context "SupportShouldprocess" {
$script = '
function get-foo