From 9836df35466fe859352ef55ccfe2e6ba69259915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Thu, 20 Feb 2025 22:04:17 +0100 Subject: [PATCH 1/3] Add Should-HaveParameter --- src/Module.ps1 | 3 ++ src/Pester.psd1 | 3 ++ .../assert/Parameter/Should-HaveParameter.ps1 | 39 +++++++++++++++++++ .../Parameter/Should-NotHaveParameter.ps1 | 33 ++++++++++++++++ .../Parameter/Should-HaveParameter.Tests.ps1 | 15 +++++++ .../Should-NotHaveParameter.Tests.ps1 | 15 +++++++ 6 files changed, 108 insertions(+) create mode 100644 src/functions/assert/Parameter/Should-HaveParameter.ps1 create mode 100644 src/functions/assert/Parameter/Should-NotHaveParameter.ps1 create mode 100644 tst/functions/assert/Parameter/Should-HaveParameter.Tests.ps1 create mode 100644 tst/functions/assert/Parameter/Should-NotHaveParameter.Tests.ps1 diff --git a/src/Module.ps1 b/src/Module.ps1 index 34d86885f..23c48b92e 100644 --- a/src/Module.ps1 +++ b/src/Module.ps1 @@ -92,6 +92,9 @@ $script:SafeCommands['Set-DynamicParameterVariable'] = $ExecutionContext.Session 'Should-BeBefore' 'Should-BeAfter' + 'Should-HaveParameter' + 'Should-NotHaveParameter' + # export 'Export-NUnitReport' 'ConvertTo-NUnitReport' diff --git a/src/Pester.psd1 b/src/Pester.psd1 index 7d574845a..2288a45ba 100644 --- a/src/Pester.psd1 +++ b/src/Pester.psd1 @@ -114,6 +114,9 @@ 'Should-BeBefore' 'Should-BeAfter' + 'Should-HaveParameter' + 'Should-NotHaveParameter' + # helpers 'New-MockObject' 'New-Fixture' diff --git a/src/functions/assert/Parameter/Should-HaveParameter.ps1 b/src/functions/assert/Parameter/Should-HaveParameter.ps1 new file mode 100644 index 000000000..fb5ec3ecd --- /dev/null +++ b/src/functions/assert/Parameter/Should-HaveParameter.ps1 @@ -0,0 +1,39 @@ +function Should-HaveParameter { + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')] + param( + [String] $ParameterName, + $Type, + [String] $DefaultValue, + [Switch] $Mandatory, + [String] $InParameterSet, + [Switch] $HasArgumentCompleter, + [String[]] $Alias, + [Parameter(ValueFromPipeline = $true)] + $Actual, + [String] $Because + ) + <# + .SYNOPSIS + Asserts that a command has the expected parameter. + + .EXAMPLE + Get-Command "Invoke-WebRequest" | Should -HaveParameter Uri -Mandatory + + This test passes, because it expected the parameter URI to exist and to + be mandatory. + .NOTES + The attribute [ArgumentCompleter] was added with PSv5. Previously this + assertion will not be able to use the -HasArgumentCompleter parameter + if the attribute does not exist. + #> + + $collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput + $Actual = $collectedInput.Actual + + $PSBoundParameters["ActualValue"] = $Actual + $PSBoundParameters.Remove("Actual") + + $testResult = Should-HaveParameterAssertion @PSBoundParameters + + Test-AssertionResult $testResult +} diff --git a/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 b/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 new file mode 100644 index 000000000..ac1a1c172 --- /dev/null +++ b/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 @@ -0,0 +1,33 @@ +function Should-NotHaveParameter { + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')] + param( + [String] $ParameterName, + $Actual, + [String] $Because + ) + <# + .SYNOPSIS + Asserts that a command has does not have the parameter. + + .EXAMPLE + Get-Command "Invoke-WebRequest" | Should -NotHaveParameter Uri + + This test fails, because it expected the parameter URI to not exist. + + .NOTES + The attribute [ArgumentCompleter] was added with PSv5. Previously this + assertion will not be able to use the -HasArgumentCompleter parameter + if the attribute does not exist. + #> + + $collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput + $Actual = $collectedInput.Actual + + $PSBoundParameters["ActualValue"] = $Actual + $PSBoundParameters.Remove("Actual") + $PSBoundParameters["Negate"] = $true + + $testResult = Should-HaveParameterAssertion @PSBoundParameters + + Test-AssertionResult $testResult +} diff --git a/tst/functions/assert/Parameter/Should-HaveParameter.Tests.ps1 b/tst/functions/assert/Parameter/Should-HaveParameter.Tests.ps1 new file mode 100644 index 000000000..1ca0d9dd1 --- /dev/null +++ b/tst/functions/assert/Parameter/Should-HaveParameter.Tests.ps1 @@ -0,0 +1,15 @@ +Set-StrictMode -Version Latest + +Describe "Should-HaveParameter" { + It "Passes when function has a parameter" { + function f ($a) { } + + Get-Command f | Should-HaveParameter a + } + + It "Fails when function does not have a parameter" { + function f () { } + + { Get-Command f | Should-HaveParameter a } | Verify-Throw + } +} diff --git a/tst/functions/assert/Parameter/Should-NotHaveParameter.Tests.ps1 b/tst/functions/assert/Parameter/Should-NotHaveParameter.Tests.ps1 new file mode 100644 index 000000000..718680b75 --- /dev/null +++ b/tst/functions/assert/Parameter/Should-NotHaveParameter.Tests.ps1 @@ -0,0 +1,15 @@ +Set-StrictMode -Version Latest + +Describe "Should-HaveParameter" { + It "Passes when function does not have a parameter" { + function f () { } + + Get-Command f | Should-NotHaveParameter a + } + + It "Fails when function has a parameter" { + function f ($a) { } + + { Get-Command f | Should-NotHaveParameter a } | Verify-Throw + } +} From 89b8c3a7c9ed117a1a618911cda03d6c6f57be2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Thu, 20 Feb 2025 22:20:40 +0100 Subject: [PATCH 2/3] Help --- .../assert/Parameter/Should-HaveParameter.ps1 | 66 +++++++++++++++---- .../Parameter/Should-NotHaveParameter.ps1 | 27 ++++++-- 2 files changed, 73 insertions(+), 20 deletions(-) diff --git a/src/functions/assert/Parameter/Should-HaveParameter.ps1 b/src/functions/assert/Parameter/Should-HaveParameter.ps1 index fb5ec3ecd..25e5a1cf9 100644 --- a/src/functions/assert/Parameter/Should-HaveParameter.ps1 +++ b/src/functions/assert/Parameter/Should-HaveParameter.ps1 @@ -1,4 +1,56 @@ function Should-HaveParameter { + <# + .SYNOPSIS + Asserts that a command has the expected parameter. + + .PARAMETER ParameterName + The name of the parameter to check. E.g. Uri + + .PARAMETER Type + The type of the parameter to check. E.g. [string] + + .PARAMETER DefaultValue + The default value of the parameter to check. E.g. "https://example.com" + + .PARAMETER Mandatory + Whether the parameter is mandatory or not. + + .PARAMETER InParameterSet + The parameter set that the parameter belongs to. + + .PARAMETER HasArgumentCompleter + Whether the parameter has an argument completer or not. + + .PARAMETER Alias + The alias of the parameter to check. + + .PARAMETER Actual + The actual command to check. E.g. Get-Command "Invoke-WebRequest" + + .PARAMETER Because + The reason why the input should be the expected value. + + .EXAMPLE + ```powershell + Get-Command "Invoke-WebRequest" | Should -HaveParameter Uri -Mandatory + ``` + + This test passes, because it expected the parameter URI to exist and to + be mandatory. + + + .NOTES + The attribute [ArgumentCompleter] was added with PSv5. Previously this + assertion will not be able to use the -HasArgumentCompleter parameter + if the attribute does not exist. + + .LINK + https://pester.dev/docs/commands/Should-HaveParameter + + .LINK + https://pester.dev/docs/assertions + #> + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')] param( [String] $ParameterName, @@ -12,20 +64,6 @@ function Should-HaveParameter { $Actual, [String] $Because ) - <# - .SYNOPSIS - Asserts that a command has the expected parameter. - - .EXAMPLE - Get-Command "Invoke-WebRequest" | Should -HaveParameter Uri -Mandatory - - This test passes, because it expected the parameter URI to exist and to - be mandatory. - .NOTES - The attribute [ArgumentCompleter] was added with PSv5. Previously this - assertion will not be able to use the -HasArgumentCompleter parameter - if the attribute does not exist. - #> $collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput $Actual = $collectedInput.Actual diff --git a/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 b/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 index ac1a1c172..dcb2febec 100644 --- a/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 +++ b/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 @@ -1,14 +1,15 @@ function Should-NotHaveParameter { - [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')] - param( - [String] $ParameterName, - $Actual, - [String] $Because - ) <# .SYNOPSIS Asserts that a command has does not have the parameter. + .PARAMETER ParameterName + The name of the parameter to check. E.g. Uri + .PARAMETER Actual + The actual command to check. E.g. Get-Command "Invoke-WebRequest" + .PARAMETER Because + The reason why the input should be the expected value. + .EXAMPLE Get-Command "Invoke-WebRequest" | Should -NotHaveParameter Uri @@ -18,8 +19,22 @@ function Should-NotHaveParameter { The attribute [ArgumentCompleter] was added with PSv5. Previously this assertion will not be able to use the -HasArgumentCompleter parameter if the attribute does not exist. + + .LINK + https://pester.dev/docs/commands/Should-NotHaveParameter + + .LINK + https://pester.dev/docs/assertions #> + + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')] + param( + [String] $ParameterName, + $Actual, + [String] $Because + ) + $collectedInput = Collect-Input -ParameterInput $Actual -PipelineInput $local:Input -IsPipelineInput $MyInvocation.ExpectingInput -UnrollInput $Actual = $collectedInput.Actual From 41e9589c9f4d3e01e292f03e9eafb8acc56f3304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Jare=C5=A1?= Date: Sat, 22 Feb 2025 21:10:18 +0100 Subject: [PATCH 3/3] Fix --- .../Parameter/Should-NotHaveParameter.ps1 | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 b/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 index dcb2febec..66191d16b 100644 --- a/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 +++ b/src/functions/assert/Parameter/Should-NotHaveParameter.ps1 @@ -1,24 +1,28 @@ function Should-NotHaveParameter { <# .SYNOPSIS - Asserts that a command has does not have the parameter. + Asserts that a command has does not have the parameter. .PARAMETER ParameterName - The name of the parameter to check. E.g. Uri + The name of the parameter to check. E.g. Uri + .PARAMETER Actual - The actual command to check. E.g. Get-Command "Invoke-WebRequest" + The actual command to check. E.g. Get-Command "Invoke-WebRequest" + .PARAMETER Because - The reason why the input should be the expected value. + The reason why the input should be the expected value. .EXAMPLE - Get-Command "Invoke-WebRequest" | Should -NotHaveParameter Uri + ```powershell + Get-Command "Invoke-WebRequest" | Should -NotHaveParameter Uri + ``` - This test fails, because it expected the parameter URI to not exist. + This test fails, because it expected the parameter URI to not exist. .NOTES - The attribute [ArgumentCompleter] was added with PSv5. Previously this - assertion will not be able to use the -HasArgumentCompleter parameter - if the attribute does not exist. + The attribute [ArgumentCompleter] was added with PSv5. Previously this + assertion will not be able to use the -HasArgumentCompleter parameter + if the attribute does not exist. .LINK https://pester.dev/docs/commands/Should-NotHaveParameter @@ -31,6 +35,7 @@ function Should-NotHaveParameter { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseProcessBlockForPipelineCommand', '')] param( [String] $ParameterName, + [Parameter(ValueFromPipeline = $true)] $Actual, [String] $Because )