From 2468aac01aa461bc536f7aec28098c6fdb4a3b83 Mon Sep 17 00:00:00 2001 From: CM Date: Thu, 14 Nov 2024 23:32:56 -0500 Subject: [PATCH 1/3] Fix Progress Preference handling, add tests/assert --- .../engine/SpecialVariables.cs | 2 + .../engine/parser/VariableAnalysis.cs | 8 ++++ .../Scripting/CommonParameters.Tests.ps1 | 38 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/src/System.Management.Automation/engine/SpecialVariables.cs b/src/System.Management.Automation/engine/SpecialVariables.cs index 420b52d4d22..7563f89a919 100644 --- a/src/System.Management.Automation/engine/SpecialVariables.cs +++ b/src/System.Management.Automation/engine/SpecialVariables.cs @@ -341,6 +341,7 @@ internal static class SpecialVariables SpecialVariables.WarningPreference, SpecialVariables.InformationPreference, SpecialVariables.ConfirmPreference, + SpecialVariables.ProgressPreference, }; internal static readonly Type[] PreferenceVariableTypes = @@ -352,6 +353,7 @@ internal static class SpecialVariables /* 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 diff --git a/src/System.Management.Automation/engine/parser/VariableAnalysis.cs b/src/System.Management.Automation/engine/parser/VariableAnalysis.cs index 8bf14d79aac..504947167b4 100644 --- a/src/System.Management.Automation/engine/parser/VariableAnalysis.cs +++ b/src/System.Management.Automation/engine/parser/VariableAnalysis.cs @@ -720,6 +720,14 @@ orderby details.LocalTupleIndex (scriptCmdlet ? SpecialVariables.PreferenceVariables.Length : 0), "analysis is incorrectly allocating number of locals when optimizations are disabled."); + Diagnostics.Assert(SpecialVariables.AutomaticVariables.Length == (int)AutomaticVariable.NumberOfAutomaticVariables + && SpecialVariables.AutomaticVariableTypes.Length == (int)AutomaticVariable.NumberOfAutomaticVariables, + "automatic variable enum length does not match both automatic variable and automatic variable types length."); + + Diagnostics.Assert(Enum.GetNames(typeof(PreferenceVariable)).Length == SpecialVariables.PreferenceVariables.Length + && Enum.GetNames(typeof(PreferenceVariable)).Length == SpecialVariables.PreferenceVariableTypes.Length, + "preference variable enum length does not match both preference variable and preference variable types length."); + var nameToIndexMap = new Dictionary(0, StringComparer.OrdinalIgnoreCase); for (int i = 0; i < orderedLocals.Length; ++i) { diff --git a/test/powershell/Language/Scripting/CommonParameters.Tests.ps1 b/test/powershell/Language/Scripting/CommonParameters.Tests.ps1 index 6f9d80e1677..af7388a418f 100644 --- a/test/powershell/Language/Scripting/CommonParameters.Tests.ps1 +++ b/test/powershell/Language/Scripting/CommonParameters.Tests.ps1 @@ -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 = [Powershell]::Create() + $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 = [Powershell]::Create() + $ps.AddScript(@' + function test-function { + [CmdletBinding()]param() + + switch($false) { default {} } + + 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 From a5627028bd85dd982089a550eb3a14d0cd50436f Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 2 Jun 2025 16:53:53 -0700 Subject: [PATCH 2/3] Move the assertions to a common code path and update test --- .../engine/parser/Compiler.cs | 8 +++++ .../engine/parser/VariableAnalysis.cs | 8 ----- .../Scripting/CommonParameters.Tests.ps1 | 32 +++++++++---------- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/System.Management.Automation/engine/parser/Compiler.cs b/src/System.Management.Automation/engine/parser/Compiler.cs index 30528f99588..d010234edec 100644 --- a/src/System.Management.Automation/engine/parser/Compiler.cs +++ b/src/System.Management.Automation/engine/parser/Compiler.cs @@ -833,6 +833,14 @@ internal class Compiler : ICustomAstVisitor2 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"); diff --git a/src/System.Management.Automation/engine/parser/VariableAnalysis.cs b/src/System.Management.Automation/engine/parser/VariableAnalysis.cs index 504947167b4..8bf14d79aac 100644 --- a/src/System.Management.Automation/engine/parser/VariableAnalysis.cs +++ b/src/System.Management.Automation/engine/parser/VariableAnalysis.cs @@ -720,14 +720,6 @@ orderby details.LocalTupleIndex (scriptCmdlet ? SpecialVariables.PreferenceVariables.Length : 0), "analysis is incorrectly allocating number of locals when optimizations are disabled."); - Diagnostics.Assert(SpecialVariables.AutomaticVariables.Length == (int)AutomaticVariable.NumberOfAutomaticVariables - && SpecialVariables.AutomaticVariableTypes.Length == (int)AutomaticVariable.NumberOfAutomaticVariables, - "automatic variable enum length does not match both automatic variable and automatic variable types length."); - - Diagnostics.Assert(Enum.GetNames(typeof(PreferenceVariable)).Length == SpecialVariables.PreferenceVariables.Length - && Enum.GetNames(typeof(PreferenceVariable)).Length == SpecialVariables.PreferenceVariableTypes.Length, - "preference variable enum length does not match both preference variable and preference variable types length."); - var nameToIndexMap = new Dictionary(0, StringComparer.OrdinalIgnoreCase); for (int i = 0; i < orderedLocals.Length; ++i) { diff --git a/test/powershell/Language/Scripting/CommonParameters.Tests.ps1 b/test/powershell/Language/Scripting/CommonParameters.Tests.ps1 index af7388a418f..4b6eda1ef95 100644 --- a/test/powershell/Language/Scripting/CommonParameters.Tests.ps1 +++ b/test/powershell/Language/Scripting/CommonParameters.Tests.ps1 @@ -149,14 +149,14 @@ Describe "Common parameters support for script cmdlets" -Tags "CI" { Context "ProgressAction" { It "Ignores progress actions on advanced script function with no variables" { - $ps = [Powershell]::Create() - $ps.AddScript(@' - function test-function { - [CmdletBinding()]param() - - Write-Progress "progress foo" - } - test-function -ProgressAction Ignore + $ps.AddScript( +@' +function test-function { + [CmdletBinding()]param() + + Write-Progress "progress foo" +} +test-function -ProgressAction Ignore '@).Invoke() $ps.Streams.Progress.Count | Should -Be 0 @@ -166,16 +166,16 @@ Describe "Common parameters support for script cmdlets" -Tags "CI" { } It "Ignores progress actions on advanced script function with variables" { - $ps = [Powershell]::Create() - $ps.AddScript(@' - function test-function { - [CmdletBinding()]param() + $ps.AddScript( +@' +function test-function { + [CmdletBinding()]param([string]$path) - switch($false) { default {} } + switch($false) { default { "echo $path" } } - Write-Progress "progress foo" - } - test-function -ProgressAction Ignore + Write-Progress "progress foo" +} +test-function -ProgressAction Ignore '@).Invoke() $ps.Streams.Progress.Count | Should -Be 0 From 9b76f07662f3328e17e1163a0f391738d3cb7ed4 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 2 Jun 2025 17:14:15 -0700 Subject: [PATCH 3/3] Fix markdown error reported in CI --- docs/community/working-group-definitions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/community/working-group-definitions.md b/docs/community/working-group-definitions.md index f3c01442de4..277fc37f789 100644 --- a/docs/community/working-group-definitions.md +++ b/docs/community/working-group-definitions.md @@ -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