Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock fails to process dynamic parameter called PSEdition #1274

Open
renehernandez opened this issue Mar 25, 2019 · 4 comments · May be fixed by #2592
Open

Mock fails to process dynamic parameter called PSEdition #1274

renehernandez opened this issue Mar 25, 2019 · 4 comments · May be fixed by #2592

Comments

@renehernandez
Copy link
Contributor

renehernandez commented Mar 25, 2019

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 parameter

The following function and test highlight the issue:

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)"
        }
    }
}

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-MockCalled Test-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

6. Context

This is related to #1007 and #1128

@renehernandez
Copy link
Contributor Author

@nohwnd Can you add this issue to the Better Mock milestone?

@nohwnd nohwnd added this to the Better Mock milestone Apr 3, 2019
@renehernandez renehernandez self-assigned this Apr 11, 2019
@fflaten
Copy link
Collaborator

fflaten commented Aug 10, 2022

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.

@nohwnd
Copy link
Member

nohwnd commented Aug 11, 2022

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.

@fflaten fflaten self-assigned this Apr 4, 2023
@fflaten fflaten removed their assignment Apr 28, 2024
@benjaminfuchs benjaminfuchs linked a pull request Dec 20, 2024 that will close this issue
5 tasks
@benjaminfuchs
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants