Fix mock bootstrap proxy for cmdlets with no DefaultParameterSetName#2887
Merged
Conversation
…o default set When a cmdlet has no DefaultParameterSetName and its dynamic parameters introduce multiple named parameter sets (e.g. Get-PackageSource with NuGet/PowerShellGet), calling the mock with no arguments fails with 'Parameter set cannot be resolved'. Fix: when $metadata.DefaultParameterSetName is null/empty for a cmdlet, inject DefaultParameterSetName='__AllParameterSets' into the generated [CmdletBinding()] of the bootstrap proxy so PowerShell can resolve the parameter set. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
fflaten
reviewed
Jul 17, 2026
Co-authored-by: Frode Flaten <3436158+fflaten@users.noreply.github.com>
…me is injected The suggestion from @fflaten moved the comma guard to the top of the Cmdlet block, but when DefaultParameterSetName='__AllParameterSets' is also injected the comma between DefaultParameterSetName and PositionalBinding=$false was missing, causing a ParseException at scriptblock creation time. Add the comma immediately after the DefaultParameterSetName insertion so both attributes are correctly separated. 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.
Fix #1531
Mocking a cmdlet with multiple non-mandatory parameter sets and no
DefaultParameterSetName(e.g.Get-PackageSourcewith itsNuGetandPowerShellGetdynamic sets) failed at call time with:When
Create-MockHookbuilds the bootstrap proxy it always injectsPositionalBinding=$falseinto the generated[CmdletBinding()]. For a cmdlet whose dynamic parameters add several named sets with no default, PowerShell has no set to fall back on when the proxy is called with no arguments, hence the ambiguity. The real cmdlet handles this in C# and is fine, the PS-level proxy is not.Fix: when
$metadata.DefaultParameterSetNameis null or empty, injectDefaultParameterSetName='__AllParameterSets'into the proxy's[CmdletBinding()]. Cmdlets that already have a default set are untouched, the existing value is preserved and the injection is skipped.Added a regression test in
Mock.Tests.ps1that mocksGet-PackageSource(ships with PS7) and calls it with no arguments, skipped when the cmdlet is not available.