Fix mocking commands with [OrderedDictionary] parameters on PowerShell 7#2886
Merged
Conversation
ProxyCommand.GetParamBlock serializes [System.Collections.Specialized.OrderedDictionary]
parameters using the [ordered] type accelerator on PowerShell 7+. That accelerator is
only valid as a cast on a hash literal ([ordered]@{}), not as a parameter type
constraint, so [scriptblock]::Create threw a ParseException while building the mock
bootstrap function before the test ran.
Add a Repair-OrderedType helper (mirroring Repair-EnumParameters) that rewrites the
generated param-block, replacing the [ordered] / [ordered[]] accelerator in
type-constraint position with the full System.Collections.Specialized.OrderedDictionary
type name. Windows PowerShell 5.1 already emits the full name, so it is unaffected.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Mocking a function that has a parameter typed
[System.Collections.Specialized.OrderedDictionary]threw aParseExceptionon PowerShell 7, even though it worked on Windows PowerShell 5.1.[System.Management.Automation.ProxyCommand]::GetParamBlock()— used to build the mock bootstrap function — serializes anOrderedDictionaryparameter using the[ordered]type accelerator on PowerShell 7+. That accelerator is only valid as a cast on a hash literal ([ordered]@{}), not as a parameter type constraint, so the[scriptblock]::Create(...)that finalises the bootstrap function threw:before the test even ran. Windows PowerShell 5.1 emits the full type name instead, which is why it was unaffected.
Fix
Add a
Repair-OrderedTypehelper (mirroring the existingRepair-EnumParameters) that rewrites the generated param-block, replacing the[ordered]/[ordered[]]accelerator in type-constraint position (the token immediately preceding${ParameterName}for parameters the metadata reports asOrderedDictionary) with the full[System.Collections.Specialized.OrderedDictionary]type name. It is called right afterRepair-EnumParameters. The match is bounded to the specific parameter variable so any legitimate[ordered]@{}cast is left untouched, and it early-returns when no[ordered]accelerator is present (e.g. on Windows PowerShell).Tests
Added a regression
Describeblock intst/functions/Mock.Tests.ps1covering a scalarOrderedDictionaryparameter, one mixed with other parameters, anOrderedDictionary[]array, and an end-to-end mock invoked through a wrapper withShould -Invoke. Confirmed these fail with the originalParseExceptionbefore the fix and pass after it, on PowerShell 7.Verification
./build.ps1succeeds../test.ps1 -File tst/functions/Mock.Tests.ps1 -SkipPTests→ 254 passed, 0 failed.Invoke-ScriptAnalyzer -Path ./src -Recurse -Settings ./.github/workflows/PSScriptAnalyzerSettings.psd1→ no new findings on the changed lines.Fix #2370