Skip to content

Commit

Permalink
Fix handling of conflicting dynamic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminfuchs committed Dec 20, 2024
1 parent 62823f5 commit cb2b3e3
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1457,13 +1457,50 @@ function Get-MockDynamicParameter {

switch ($PSCmdlet.ParameterSetName) {
'Cmdlet' {
Get-DynamicParametersForCmdlet -CmdletName $CmdletName -Parameters $Parameters
$dynamicParams = Get-DynamicParametersForCmdlet -CmdletName $CmdletName -Parameters $Parameters
}

'Function' {
Get-DynamicParametersForMockedFunction -DynamicParamScriptBlock $DynamicParamScriptBlock -Parameters $Parameters -Cmdlet $Cmdlet
$dynamicParams = Get-DynamicParametersForMockedFunction -DynamicParamScriptBlock $DynamicParamScriptBlock -Parameters $Parameters -Cmdlet $Cmdlet
}
}

if ($null -eq $dynamicParams) {
return
}

Repair-ConflictingDynamicParameters -DynamicParams $dynamicParams
}

function Repair-ConflictingDynamicParameters {
[OutputType([System.Management.Automation.RuntimeDefinedParameterDictionary])]
param (
[Parameter(Mandatory = $true)]
[System.Management.Automation.RuntimeDefinedParameterDictionary]
$DynamicParams
)

$repairedDynamicParams = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$conflictingParams = Get-ConflictingParameterNames

foreach ($paramName in $DynamicParams.Keys) {
$dynamicParam = $DynamicParams[$paramName]

if ($conflictingParams -contains $paramName) {
$newName = "_$paramName"
$dynamicParam.Name = $newName

$aliasAttribute = New-Object System.Management.Automation.AliasAttribute -ArgumentList $paramName
$dynamicParam.Attributes.Add($aliasAttribute)

$repairedDynamicParams[$newName] = $dynamicParam
}
else {
$repairedDynamicParams[$paramName] = $dynamicParam
}
}

return $repairedDynamicParams
}

function Get-DynamicParametersForCmdlet {
Expand Down
69 changes: 69 additions & 0 deletions tst/functions/Mock.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3160,3 +3160,72 @@ Describe 'Mocking with nested Pester runs' {
Get-Command Get-ChildItem | Should -Not -Be 2
}
}

Describe 'Usage of Alias in DynamicParams' {
# https://github.com/pester/Pester/issues/1274

BeforeAll {
function New-DynamicAttr($ParamDictionary, $Name, $Alias = $null) {
$attr = New-Object -Type `
System.Management.Automation.ParameterAttribute
$attr.Mandatory = $false
$attr.ParameterSetName = '__AllParameterSets'
$attributeCollection = New-Object `
-Type System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attr)

if ($null -ne $Alias) {
$attr = New-Object -Type `
System.Management.Automation.AliasAttribute -ArgumentList @($Alias)
$attributeCollection.Add($attr)
}

$dynParam1 = New-Object -Type `
System.Management.Automation.RuntimeDefinedParameter($Name, [string],
$attributeCollection)

$ParamDictionary.Add($Name, $dynParam1)
}

function Test-DynamicParam {
[CmdletBinding()]
param(
[String]$Name
)

dynamicparam {
if ($Name.StartsWith("Hello")) {
$paramDictionary = New-Object `
-Type System.Management.Automation.RuntimeDefinedParameterDictionary
New-DynamicAttr -ParamDictionary $paramDictionary -Name "PSEdition"

return $paramDictionary
}
}

process {
if ($PSBoundParameters.PSEdition) {
Write-Host "PSEdition value: $($PSBoundParameters.PSEdition)"
}
}
}
}

Context 'Mocking with ParameterFilter' {
It 'Mocks Test-DynamicParam with PSEdition set to Desktop' {
Mock Test-DynamicParam { "World" } -ParameterFilter { $PSEdition -eq 'Desktop' }

Test-DynamicParam -Name "Hello" -PSEdition 'Desktop' | Should -Be 'World'
}
}

Context 'Validating Mock Invocation' {
It 'Invokes Test-DynamicParam with correct parameters' {
Mock Test-DynamicParam { "World" }

Test-DynamicParam -Name "Hello" -PSEdition 'Desktop' | Should -Be 'World'

Should -Invoke Test-DynamicParam -Exactly 1 -Scope It
}
}
}

0 comments on commit cb2b3e3

Please sign in to comment.