mirror of
https://github.com/actions/versions-package-tools.git
synced 2025-12-11 03:56:46 +00:00
Update ShouldReturnZeroExitCode function
This commit is contained in:
@@ -6,28 +6,29 @@ Pester extension that allows to run command and validate exit code
|
|||||||
#>
|
#>
|
||||||
function ShouldReturnZeroExitCode {
|
function ShouldReturnZeroExitCode {
|
||||||
Param(
|
Param(
|
||||||
[Parameter (Mandatory = $true)] [ValidateNotNullOrEmpty()]
|
[String] $ActualValue,
|
||||||
[String]$ActualValue,
|
[switch] $Negate,
|
||||||
[switch]$Negate
|
[string] $Because # This parameter is unused by we need it to match Pester asserts signature
|
||||||
)
|
)
|
||||||
|
|
||||||
Write-Host "Run command '${ActualValue}'"
|
$result = Get-CommandResult $ActualValue
|
||||||
Invoke-Expression -Command $ActualValue | ForEach-Object { Write-Host $_ }
|
|
||||||
$actualExitCode = $LASTEXITCODE
|
|
||||||
|
|
||||||
[bool]$succeeded = $actualExitCode -eq 0
|
[bool]$succeeded = $result.ExitCode -eq 0
|
||||||
if ($Negate) { $succeeded = -not $succeeded }
|
if ($Negate) { $succeeded = -not $succeeded }
|
||||||
|
|
||||||
if (-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
|
Succeeded = $succeeded
|
||||||
FailureMessage = $failureMessage
|
FailureMessage = $failureMessage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Add-AssertionOperator -Name ReturnZeroExitCode `
|
if (Get-Command -Name Add-AssertionOperator -ErrorAction SilentlyContinue) {
|
||||||
-Test $function:ShouldReturnZeroExitCode
|
Add-AssertionOperator -Name ReturnZeroExitCode -InternalName ShouldReturnZeroExitCode -Test ${function:ShouldReturnZeroExitCode}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user