pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.11k stars 473 forks source link

Calling mock of Get-ChildItem with -File parameter fails on Linux #2484

Closed ChrisLGardner closed 5 months ago

ChrisLGardner commented 6 months ago

Checklist

What is the issue?

When mocking Get-ChildItem on Linux (or at least in the devcontainers I've tested it) when you then call that mock and use the -File switch you get "A parameter cannot be found that matches parameter name 'File'.".

Debugging into this the problem appears to be that this line isn't finding the dynamic parameters on Get-ChildItem for the filesystem. I've checked that the filesystem is definitely the active provider when the mock is called and when that line is ran, Line 1488 returns the dynamic parameters correctly.

Expected Behavior

Finds and calls the correct mock of Get-ChildItem when -File switch is used.

Steps To Reproduce

Running the following code in a devcontainer (tested in codespaces for this repo and another devcontainer for my main projects) or other Linux workspace:

$c = New-PesterContainer -ScriptBlock {
    Describe test {
        BeforeAll {
            Mock Get-ChildItem -ParameterFilter {$Path -eq 'C:\TestFolder' -and $File} -MockWith {"Mock 1"}

            function subject {
                [CmdletBinding()]param ( )
                Get-ChildItem -File -Path C:\TestFolder -EA stop -Recurse -Force
            }
        }
        It 'Can call gci with File' {
            { subject -ea stop } | Should -Not -Throw
            Should -Invoke Get-ChildItem -ParameterFilter { $File }
        }
    }
}
Invoke-Pester -Container $c

Describe your environment

Pester version : 5.5.0 /home/vscode/.local/share/powershell/Modules/Pester/5.5.0/Pester.psm1
PowerShell version : 7.4.1 OS version : Unix 5.10.102.1

Possible Solution?

No response

fflaten commented 5 months ago

Thanks for the detailed report! The dynamic parameters are affected by your current location and -Path. You're providing a Windows path on a Unix-system. That will fail even without mocking.

PS /workspaces/Pester/Samples> Get-ChildItem -File -Path C:\TestFolder -EA stop -Recurse -Force
Get-ChildItem: A parameter cannot be found that matches parameter name 'File'.

Remove -Path or use a Unix-path and it should work as expected.