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) { If (Get-Command -Name Add-AssertionOperator -ErrorAction SilentlyContinue) {
Add-AssertionOperator -Name ReturnZeroExitCode -InternalName ShouldReturnZeroExitCode -Test ${function:ShouldReturnZeroExitCode} Add-AssertionOperator -Name ReturnZeroExitCode -InternalName ShouldReturnZeroExitCode -Test ${function:ShouldReturnZeroExitCode}
Add-AssertionOperator -Name MatchCommandOutput -InternalName ShouldMatchCommandOutput -Test ${function:ShouldMatchCommandOutput}
} }

View File

@@ -41,3 +41,4 @@ Choco-Install -PackageName hub
Add-MachinePathItem "C:\Program Files\Git\bin" Add-MachinePathItem "C:\Program Files\Git\bin"
Invoke-PesterTests -TestFile "Git" -TestName "Git" Invoke-PesterTests -TestFile "Git" -TestName "Git"
Invoke-PesterTests -TestFile "Git" -TestName "Hub"

View File

@@ -17,11 +17,11 @@ Describe "Haskell" {
} }
It "GHC <ghcVersion> is installed" -TestCases $ghcTestCases { It "GHC <ghcVersion> is installed" -TestCases $ghcTestCases {
& $binGhcPath --version | Should -Match $ghcVersion "$binGhcPath --version" | Should -MatchCommandOutput $ghcVersion
} }
It "GHC <defaultGhcVersion> is the default version and should be the latest installed" -TestCases @{defaultGhcVersion = $defaultGhcVersion} { It "GHC <defaultGhcVersion> is the default version and should be the latest installed" -TestCases @{defaultGhcVersion = $defaultGhcVersion} {
ghc --version | Should -Match $defaultGhcVersion "ghc --version" | Should -MatchCommandOutput $defaultGhcVersion
} }
It "Cabal is installed" { It "Cabal is installed" {