You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PSEdition, which is a conflicting parameter name, is not being properly handled while mocking if it is used for a dynamic parameter
The following function and test highlight the issue:
functionNew-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)
}
functionTest-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)"
}
}
}
Describe 'Usage of Alias in DynamicParams' {
Context 'Usage of ParameterFilters in Mock' {
It 'using conflicting PSEdition parameter name' {
Mock Test-DynamicParam { "World" } -ParameterFilter {$PSEdition-eq'Desktop'}
Test-DynamicParam-Name "Hello"-PSEdition 'Desktop'| Should -Be 'World'
}
}
Context 'Usage of ParameterFilters in Assert-MockCalled' {
It 'using conflicting PSEdition parameter name' {
Mock Test-DynamicParam { "World" }
Test-DynamicParam-Name "Hello"-PSEdition 'Desktop'| Should -Be 'World'Assert-MockCalledTest-DynamicParam-ParameterFilter {$PSEdition-eq'Desktop'}
}
}
}
2. Describe Your Environment
Pester version : 4.7.3 C:\Program Files\PowerShell\Modules\Pester\4.7.3\Pester.psd1
PowerShell version : 6.1.3
OS version : Microsoft Windows NT 10.0.17134.0
3. Expected Behavior
It should pass
4.Current Behavior
It fails
5. Possible Solution
Add corresponding implementation for dynamic parameter for what was done for regular parameters
Still an issue in 5.3.3 and conflict still allowed in PowerShell 7.
Note to implementer:
Current behavior for static parameters is in Repair-ConflictingParameters.
Need to repeat this, converting conflictingParam to _conflictingParam with original name as alias, in Get-DynamicParamBlock (with AST-parsing) and Get-DynamicParametersForCmdlet.
Even though the description above seems scary, most of the work has been done before for other parameters, and in the functions named above you'd find an almost complete implementation, it just needs to be extended for PSEdition.
I gave this issue a try by adding Repair-ConflictingDynamicParameters to handle conflicts like PSEdition. Since the issue is a bit old, I had to update the tests and wasn’t 100% sure if the implementation note is still fully valid. Linked PR (#2592) at least fixes the mentioned tests - feedback is welcome!
1. General summary of the issue
PSEdition
, which is a conflicting parameter name, is not being properly handled while mocking if it is used for a dynamic parameterThe following function and test highlight the issue:
2. Describe Your Environment
3. Expected Behavior
It should pass
4.Current Behavior
It fails
5. Possible Solution
Add corresponding implementation for dynamic parameter for what was done for regular parameters
6. Context
This is related to #1007 and #1128
The text was updated successfully, but these errors were encountered: