add assert MatchCommandOutput

This commit is contained in:
Aleksandr Chebotov
2020-07-13 13:37:04 +03:00
parent 51da94d189
commit 3356fa7bd0
3 changed files with 36 additions and 2 deletions

View File

@@ -91,6 +91,39 @@ function ShouldReturnZeroExitCode {
}
}
# Pester Assert to match output of command
function ShouldMatchCommandOutput {
Param(
[String] $ActualValue,
[String] $RegularExpression,
[switch] $Negate
)
$output = (Get-CommandResult $ActualValue).Output | Out-String
[bool] $succeeded = $output -cmatch $RegularExpression
if ($Negate) {
$succeeded = -not $succeeded
}
$failureMessage = ''
if (-not $succeeded) {
if ($Negate) {
$failureMessage = "Expected regular expression '$RegularExpression' for '$ActualValue' command to not match '$output',but it did match."
}
else {
$failureMessage = "Expected regular expression '$RegularExpression' for '$ActualValue' command to match '$output',but it did not match."
}
}
return [PSCustomObject] @{
Succeeded = $succeeded
FailureMessage = $failureMessage
}
}
If (Get-Command -Name Add-AssertionOperator -ErrorAction SilentlyContinue) {
Add-AssertionOperator -Name ReturnZeroExitCode -InternalName ShouldReturnZeroExitCode -Test ${function:ShouldReturnZeroExitCode}
Add-AssertionOperator -Name MatchCommandOutput -InternalName ShouldMatchCommandOutput -Test ${function:ShouldMatchCommandOutput}
}