File size: 7,253 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# Functional tests to verify that native executables throw errors (non-terminating and terminating) appropriately
# when $PSNativeCommandUseErrorActionPreference is $true
Describe 'Native command error handling tests' -Tags 'CI' {
BeforeAll {
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$exeName = $IsWindows ? 'testexe.exe' : 'testexe'
$exePath = @(Get-Command $exeName -Type Application)[0].Path
$errorActionPrefTestCases = @(
@{ ErrorActionPref = 'Stop' }
@{ ErrorActionPref = 'Continue' }
@{ ErrorActionPref = 'SilentlyContinue' }
@{ ErrorActionPref = 'Ignore' }
)
}
AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
}
BeforeEach {
$Error.Clear()
}
Context 'PSNativeCommandUseErrorActionPreference is $true' {
BeforeEach {
$PSNativeCommandUseErrorActionPreference = $true
}
It 'Non-zero exit code throws teminating error for $ErrorActionPreference = ''Stop''' {
$ErrorActionPreference = 'Stop'
{ testexe -returncode 1 } | Should -Throw -ErrorId 'ProgramExitedWithNonZeroCode'
$error.Count | Should -Be 1
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
$error[0].TargetObject | Should -BeExactly $exePath
}
It 'Non-zero exit code outputs a non-teminating error for $ErrorActionPreference = ''Continue''' {
$ErrorActionPreference = 'Continue'
$stderr = testexe -returncode 1 2>&1
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
$error[0].TargetObject | Should -BeExactly $exePath
$stderr[1].Exception.Message | Should -BeExactly ("Program `"$exeName`" ended with non-zero exit code: 1 ({0})." -f ($IsWindows ? '0x00000001' : '0x01'))
}
It "Non-boolean value should not cause type casting error when the native command exited with non-zero code" {
$ErrorActionPreference = 'Continue'
$PSNativeCommandUseErrorActionPreference = 'Yeah'
$PSNativeCommandUseErrorActionPreference | Should -BeExactly 'Yeah'
$stderr = testexe -returncode 1 2>&1
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
$error[0].TargetObject | Should -BeExactly $exePath
$stderr[1].Exception.Message | Should -BeExactly ("Program `"$exeName`" ended with non-zero exit code: 1 ({0})." -f ($IsWindows ? '0x00000001' : '0x01'))
}
It 'Non-zero exit code generates a non-teminating error for $ErrorActionPreference = ''SilentlyContinue''' {
$ErrorActionPreference = 'SilentlyContinue'
testexe -returncode 1 > $null
$error.Count | Should -Be 1
$error[0].FullyQualifiedErrorId | Should -BeExactly 'ProgramExitedWithNonZeroCode'
$error[0].TargetObject | Should -BeExactly $exePath
}
It 'Non-zero exit code does not generates an error record for $ErrorActionPreference = ''Ignore''' {
$ErrorActionPreference = 'Ignore'
testexe -returncode 1 > $null
$LASTEXITCODE | Should -Be 1
$error.Count | Should -Be 0
}
It 'Zero exit code generates no error for $ErrorActionPreference = ''<ErrorActionPref>''' -TestCases $errorActionPrefTestCases {
param($ErrorActionPref)
$ErrorActionPreference = $ErrorActionPref
$output = testexe -returncode 0
$output | Should -BeExactly '0'
$LASTEXITCODE | Should -Be 0
$Error.Count | Should -Be 0
}
It 'Works as expected with a try/catch block when $ErrorActionPreference = ''<ErrorActionPref>''' -TestCase $errorActionPrefTestCases {
param($ErrorActionPref)
$ErrorActionPreference = $ErrorActionPref
$threw = $false
$continued = $false
$hitFinally = $false
try
{
testexe -returncode 17 2>&1 > $null
$continued = $true
}
catch
{
$threw = $true
$exception = $_.Exception
}
finally
{
$hitFinally = $true
}
$hitFinally | Should -BeTrue
$continued | Should -Be ($ErrorActionPreference -ne 'Stop')
$threw | Should -Be ($ErrorActionPreference -eq 'Stop')
if ($threw)
{
$exception.Path | Should -BeExactly (Get-Command -Name testexe -CommandType Application).Path
$exception.ExitCode | Should -Be $LASTEXITCODE
$exception.ProcessId | Should -BeGreaterThan 0
}
}
It 'Works with trap when $ErrorActionPreference = ''<ErrorActionPref>''' -TestCases $errorActionPrefTestCases {
param($ErrorActionPref)
$ErrorActionPreference = $ErrorActionPref
trap
{
$hitTrap = $true
$exception = $_
continue
}
$hitTrap = $false
# Expect this to be trapped
testexe -returncode 17 2>&1 > $null
if ($ErrorActionPreference -eq 'Stop')
{
$hitTrap | Should -BeTrue
$exception.ExitCode | Should -Be $LASTEXITCODE
$exception.Path | Should -BeExactly (Get-Command -Name testexe -CommandType Application).Path
$exception.ProcessId | Should -BeGreaterThan 0
}
else
{
$hitTrap | Should -BeFalse
$exception | Should -BeNullOrEmpty
}
}
}
Context 'PSNativeCommandUseErrorActionPreference is $false' {
BeforeEach {
$PSNativeCommandUseErrorActionPreference = $false
}
It 'Non-zero exit code generates no error for $ErrorActionPreference = ''<ErrorActionPref>''' -TestCases $errorActionPrefTestCases {
param($ErrorActionPref)
$ErrorActionPreference = $ErrorActionPref
if ($ErrorActionPref -eq 'Stop') {
{ testexe -returncode 1 } | Should -Not -Throw
}
else {
testexe -returncode 1 > $null
}
$LASTEXITCODE | Should -Be 1
$Error.Count | Should -Be 0
}
It "Non-boolean value should not cause type casting error when the native command exited with non-zero code" {
$ErrorActionPreference = 'Continue'
$PSNativeCommandUseErrorActionPreference = 0
$PSNativeCommandUseErrorActionPreference | Should -Be 0
$PSNativeCommandUseErrorActionPreference | Should -BeOfType 'System.Int32'
testexe -returncode 1 > $null
$LASTEXITCODE | Should -Be 1
$Error.Count | Should -Be 0
}
}
}
|