-
-
Notifications
You must be signed in to change notification settings - Fork 476
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
"Should -HaveParameter 'param' -Not -Mandatory" passes tests if parameter is not present #2105
Comments
Thanks for reporting this, @lipkau wanna fix this? |
So only While if I both understand it and find it a bit confusing. Would you have expected the behavior your describe if the code was |
I agree the syntax is confusing. Only now that you spelled it out I realized what the problem is. I guess there is nothing that can be fixed now, and we just need to remember to do it like that for Should that is based on distinct functions. Should-HaveParameter, and Should-NotHaveParameter (this one would not have the -Mandatory switch at all). |
Just adding -NotMandatory would fix the problem. It just doesn't make sense to test for a parameter to not exist and at the same time test whether it is mandatory or not or if it has an alias. And you wouldn't test for a function not having an alias, you would test for it having the alias you want so i guess only Mandatory/NotMandatory would be needed to cover all logical use cases |
I agree. We're unable block |
Would it work saying |
Atm. no, but it's a valid suggestion. We do inherit a |
Supporting this would enable the following structure for -ForEach tests: It 'Has parameter <_.Name>' -TestCases @(
@{ Name = 'Foo'; Mandatory = $true }
@{ Name = 'Bar'; }
) {
$command | Should -HaveParameter $Name -Mandatory:([bool]$Mandatory) -Type $Type
} Where I want to make sure |
There are a lot of nots and not negate in this code so it's a brain melter. I think instead of this (current): if ($Mandatory) {
$testMandatory = $attributes | & Where-Object { $_ -is [System.Management.Automation.ParameterAttribute] -and $_.Mandatory }
$filters += "which is$(if ($Negate) {" not"}) mandatory"
if (-not $Negate -and -not $testMandatory) {
$buts += "it wasn't mandatory"
}
elseif ($Negate -and $testMandatory) {
$buts += "it was mandatory"
}
} It should be this: if ($PSBoundParameters.Keys -contains "Mandatory") {
$testMandatory = $attributes | & Where-Object { $_ -is [System.Management.Automation.ParameterAttribute] -and $_.Mandatory }
$filters += "which is$(if ($Negate -or -not $Mandatory) {" not"}) mandatory"
if (-not ($Negate -or -not $Mandatory) -and -not $testMandatory) {
$buts += "it wasn't mandatory"
}
elseif (($Negate -or -not $Mandatory) -and $testMandatory) {
$buts += "it was mandatory"
}
} Can it be simplified a bit? 🤔 Or just add another if: if (-not $Mandatory -and $PSBoundParameters.Keys -contains "Mandatory") {
$testMandatory = $attributes | & Where-Object { $_ -is [System.Management.Automation.ParameterAttribute] -and $_.Mandatory }
$filters += "which is$(if (-not $Negate) {" not"}) mandatory"
if ($Negate -and -not $testMandatory) {
$buts += "it wasn't mandatory"
}
elseif (-not $Negate -and $testMandatory) {
$buts += "it was mandatory"
}
} |
That already works, but I'd recommend being explicit with
Note to future assignee: |
The second case does not work from what I've tried without the changes I mentioned. Since the current Pester code just has an if that checks if I thank you for the recommendation, but please re-iterate what you mean should work. |
Yeah, you're absolutely right. Looked at the wrong sample when I answered. |
General summary of the issue
"Should -HaveParameter 'param' -Not -Mandatory" passes tests if parameter is not present
Describe your environment
Pester version : 5.3.1 C:\Users___\Documents\PowerShell\Modules\pester\5.3.1\Pester.psm1
PowerShell version : 7.2.0
OS version : Microsoft Windows NT 10.0.19043.0
Steps to reproduce
Expected Behavior
would expect the test to fail.
I can see how a parameter which is not declared is actually not mandatory. But i would still expect the 'Should -HaveParameter' to first check the actual presence of the parameter.
Current Behavior
test succeeds
Possible Solution? (optional)
use two tests
The text was updated successfully, but these errors were encountered: