From 15842ce7fee1f0544ef1e9305a05bde5f4dfab9a Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Mon, 24 Aug 2020 18:40:08 +0300 Subject: [PATCH] Update ShouldReturnZeroExitCode function --- pester-extensions.psm1 | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/pester-extensions.psm1 b/pester-extensions.psm1 index 46d2a75..b697fef 100644 --- a/pester-extensions.psm1 +++ b/pester-extensions.psm1 @@ -6,28 +6,29 @@ Pester extension that allows to run command and validate exit code #> function ShouldReturnZeroExitCode { Param( - [Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String]$ActualValue, - [switch]$Negate + [String] $ActualValue, + [switch] $Negate, + [string] $Because # This parameter is unused by we need it to match Pester asserts signature ) - Write-Host "Run command '${ActualValue}'" - Invoke-Expression -Command $ActualValue | ForEach-Object { Write-Host $_ } - $actualExitCode = $LASTEXITCODE + $result = Get-CommandResult $ActualValue - [bool]$succeeded = $actualExitCode -eq 0 + [bool]$succeeded = $result.ExitCode -eq 0 if ($Negate) { $succeeded = -not $succeeded } if (-not $succeeded) { - $failureMessage = "Command '${ActualValue}' has finished with exit code ${actualExitCode}" + $commandOutputIndent = " " * 4 + $commandOutput = ($result.Output | ForEach-Object { "${commandOutputIndent}${_}" }) -join "`n" + $failureMessage = "Command '${ActualValue}' has finished with exit code ${actualExitCode}`n${commandOutput}" } - return New-Object PSObject -Property @{ + return [PSCustomObject] @{ Succeeded = $succeeded FailureMessage = $failureMessage } } -Add-AssertionOperator -Name ReturnZeroExitCode ` - -Test $function:ShouldReturnZeroExitCode +if (Get-Command -Name Add-AssertionOperator -ErrorAction SilentlyContinue) { + Add-AssertionOperator -Name ReturnZeroExitCode -InternalName ShouldReturnZeroExitCode -Test ${function:ShouldReturnZeroExitCode} +}