Add ValidateNullOrEmpty to -Name parameter of Get-Service#2542
Add ValidateNullOrEmpty to -Name parameter of Get-Service#2542vors merged 7 commits intoPowerShell:masterPowerShell/PowerShell:masterfrom HemantMahawar:GetServiceNullNameCopy head branch name to clipboard
Conversation
|
Hi @HemantMahawar, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
TTYL, MSBOT; |
| $global:PSDefaultParameterValues = $originalDefaultParameterValues | ||
| } | ||
| Context 'Check null or empty value to the -Name parameter' { | ||
| It 'Should throw if null is passed to -Name parameter' { |
There was a problem hiding this comment.
You can use the -Testcases parameter to shorten this test a bit, e.g.
$testCases =
@{ data = $null; value = 'null' },
@{ data = [String]::Empty, value = 'empty string' }
It 'Should throw if <value> is passed to -Name parameter' -TestCases $testCases {
param($data)
try { Get-Service -Name $data }
catch [System.Management.Automation.ParameterBindingException] {$_.Exception.ErrorId | Should Be 'ParameterArgumentValidationError'}
}
It 'Should throw if <value> is passed to -Name parameter via pipeline' -TestCases $testCases {
param($data)
try { $data | Get-Service }
catch [System.Management.Automation.ParameterBindingException] {$_.Exception.ErrorId | Should Be 'ParameterArgumentValidationError'}
}
|
@lzybkr Thanks for the great tip. Incorporated your feedback. |
|
@HemantMahawar it looks like GitHub cannot attribute commits to you. It's done via email on the commits. Can you add this email address to your email addresses in the GitHub settings? |
|
@vors Done |
| It 'Should throw if <value> is passed through pipeline to -Name parameter' -TestCases $testCases { | ||
| param($data) | ||
| try {$data | Get-Service -ErrorAction Stop} | ||
| catch [System.Management.Automation.ParameterBindingException] {$_.Exception.ErrorId | Should Be 'ParameterArgumentValidationError'} |
There was a problem hiding this comment.
Please, use Should Throw instead.
The current code will report success, if no error is thrown (the only check happens in the catch statement).
There was a problem hiding this comment.
Oops, I missed that - the pattern is to throw "Expected error on previous command" at the end of the try when you want to be specific about the exception thrown.
|
|
||
| $testCases = | ||
| @{ data = $null; value = 'null' }, | ||
| @{ data = [String]::Empty; value = 'empty string' } |
There was a problem hiding this comment.
Where is value used in the test cases?
There was a problem hiding this comment.
Isn't <value> in the description a reference to the variable?
Also followed the recommended practise to check for FullyQualifiedErrorId
Fix for #2540