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

Fix Should -HaveParamter -DefaultValue #2398

Merged
merged 3 commits into from Nov 9, 2023
Merged

Fix Should -HaveParamter -DefaultValue #2398

merged 3 commits into from Nov 9, 2023

Conversation

nohwnd
Copy link
Member

@nohwnd nohwnd commented Oct 16, 2023

PR Checklist

  • PR has meaningful title
  • Summary describes changes
  • PR is ready to be merged
    • If not, use the arrow next to Create Pull Request to mark it as a draft. PR can be marked Ready for review when it's ready.
  • Tests are added/update (if required)
  • Documentation is updated/added (if required)

Fix #2388

@nohwnd
Copy link
Member Author

nohwnd commented Oct 16, 2023

Note to self:

currently I see this, which suggests we "to string" the value, which makes sense since the parameter is a string. maybe we can look at our own invocation and see if the user provided the value as literal. Or change the DefaultValue to object.

Running tests.
[-] Should -HaveParameter.passes when object parameter has default parameter value $null 3ms (3ms|0ms)
 Expected command Test-Parameter to have a parameter objParam, of type [System.Object] and the default value to be <empty>, but the default value was '$null'.
 at Get-Command Test-Parameter | Should -HaveParameter 'objParam' -Type 'object' -DefaultValue $null, S:\p\pester\tst\functions\assertions\HaveParameter.Tests.ps1:353
 at <ScriptBlock>, S:\p\pester\tst\functions\assertions\HaveParameter.Tests.ps1:353
[-] Should -HaveParameter.fails when object parameter expects default parameter value $null, but there is none 9ms (9ms|0ms)
 Expected an exception with message like 'aaaa' to be thrown, but the message was 'Expected command Test-Parameter to have a parameter objParam, of type [System.Object] and the default value to be <empty>, but the default value was not specified.'. from S:\p\pester\src\functions\assertions\Should.ps1:266 char:13
     +             throw $errorRecord
     +             ~~~~~~~~~~~~~~~~~~
 at { Get-Command Test-Parameter | Should -HaveParameter 'objParam' -Type 'object' -DefaultValue $null } | Should -Throw "aaaa", S:\p\pester\tst\functions\assertions\HaveParameter.Tests.ps1:361
 at <ScriptBlock>, S:\p\pester\tst\functions\assertions\HaveParameter.Tests.ps1:361
[-] Should -HaveParameter.passes when bool parameter has default parameter value $true 4ms (3ms|0ms)
 Expected command Test-Parameter to have a parameter boolParam, of type [System.Boolean] and the default value to be 'True', but the default value was '$true'.
 at Get-Command Test-Parameter | Should -HaveParameter 'boolParam' -Type 'bool' -DefaultValue $true, S:\p\pester\tst\functions\assertions\HaveParameter.Tests.ps1:377
 at <ScriptBlock>, S:\p\pester\tst\functions\assertions\HaveParameter.Tests.ps1:377
[-] Should -HaveParameter.passes when bool parameter has default parameter value $false 4ms (4ms|1ms)
 Expected command Test-Parameter to have a parameter boolParam, of type [System.Boolean] and the default value to be 'False', but the default value was '$false'.
 at Get-Command Test-Parameter | Should -HaveParameter 'boolParam' -Type 'bool' -DefaultValue $false, S:\p\pester\tst\functions\assertions\HaveParameter.Tests.ps1:385
 at <ScriptBlock>, S:\p\pester\tst\functions\assertions\HaveParameter.Tests.ps1:385

@nohwnd
Copy link
Member Author

nohwnd commented Oct 31, 2023

There is some hidden potential in this, but I don't think it is worth the breaking change. I've reverted to the previous behavior where we cannot explicitly determine whether or not the default value was specified at all or if it was $null.

We also cannot check for the exact literal of the default type, to distinguish '(Get-Date)' string and (Get-Date) expression.

BUT the first feature does not have enough potential to warrant the breaking change, because there is not much difference between providing null default, and not providing one at all.

And the second one can be overcome by #1888 and still is an edge case.

@nohwnd nohwnd marked this pull request as ready for review October 31, 2023 08:44
@nohwnd nohwnd requested a review from fflaten October 31, 2023 08:44
@nohwnd
Copy link
Member Author

nohwnd commented Nov 9, 2023

@fflaten merged, let me know if you had any concerns :)

@nohwnd nohwnd merged commit acc66a9 into main Nov 9, 2023
14 checks passed
@nohwnd nohwnd deleted the has-parameter-fixes branch November 9, 2023 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should -HaveParameter -DefaultValue does not work for boolean types or integer value of 0 (regression)
1 participant