File size: 9,592 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 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'Tests for setting $? for execution success' -Tag 'CI' {
BeforeAll {
$script:hookValues = [System.Collections.Generic.List[bool]]::new()
function Hook
{
param([Parameter(Position = 0)][bool]$Value)
$script:hookValues.Add($Value)
}
}
AfterEach {
$script:hookValues.Clear()
}
It 'Sets $? for case ''<Script>''' -TestCases @(
# Cmdlets
@{ Script = 'Write-Output "Hi"; Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; Hook $?'; Results = @($false) }
@{ Script = 'Write-Error "Bad"; Hook $?; Hook $?'; Results = @($false, $true) }
# Native executables
@{ Script = 'testexe -returncode 0; Hook $?'; Results = @($true) }
@{ Script = 'testexe -returncode 1; Hook $?; Hook $?'; Results = @($false, $true) }
# Pure values
@{ Script = '"Hi"; Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; "Hi"; Hook $?'; Results = @($true) }
# Paren expressions (subpipelines)
@{ Script = '(Write-Output "Hi"); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; ("Hi"); Hook $?'; Results = @($true) }
@{ Script = '(Write-Error "Bad"); Hook $?; Hook $?'; Results = @($false, $true) }
@{ Script = '(testexe -returncode 0); Hook $?'; Results = @($true) }
@{ Script = '(testexe -returncode 1); Hook $?; Hook $?'; Results = @($false, $true) }
# Subexpressions
@{ Script = '$("Hi"); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; $("Hi"); Hook $?'; Results = @($true) }
@{ Script = '$(); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; $(); Hook $?'; Results = @($true) }
@{ Script = '$(Write-Error "Bad"); Hook $?; Hook $?'; Results = @($false, $true) }
@{ Script = '$(testexe -returncode 1); Hook $?; Hook $?'; Results = @($false, $true) }
@{ Script = 'Write-Error "Bad"; $(trap { continue }); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; $(trap { continue } "Hi"); Hook $?'; Results = @($true) }
# Array expressions
@{ Script = '@("Hi"); Hook $?'; Results = @($true) }
@{ Script = '@(); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; @("Hi"); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; @(); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; @("Hi", "There"); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; @("Hi"; "There"); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; @(trap { continue }); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; @(trap { continue } "Hi"); Hook $?'; Results = @($true) }
@{ Script = 'Write-Error "Bad"; @(trap { continue } "Hi", "There"); Hook $?'; Results = @($true) }
@{ Script = '@(Write-Error "Bad"); Hook $?; Hook $?'; Results = @($false, $true) }
@{ Script = '@(Write-Error "Bad"; "Hi"); Hook $?; Hook $?'; Results = @($true, $true) }
@{ Script = '@("Hi"; Write-Error "Bad"); Hook $?; Hook $?'; Results = @($false, $true) }
@{ Script = '@(Write-Error "Bad"; Write-Output "Hi"); Hook $?; Hook $?'; Results = @($true, $true) }
@{ Script = '@(Write-Output "Hi"; Write-Error "Bad"); Hook $?; Hook $?'; Results = @($false, $true) }
) {
param([string]$Script, [object[]]$Results)
Invoke-Expression $Script 2>&1 > $null
$script:hookValues | Should -Be $Results
}
It 'Sets $? correctly for string expression ''<Expression>''' -TestCases @(
@{ Expression = '"Hi"; Hook $?'; HookResults = $($true); PipelineResults = @('Hi') }
@{ Expression = '"Hi $("Good")"; Hook $?'; HookResults = $($true); PipelineResults = @('Hi Good') }
@{ Expression = '"Hi $(Write-Error "Bad")"; Hook $?'; HookResults = $($true); PipelineResults = @('Hi ') }
@{ Expression = '"Hi $(Write-Output "Good")"; Hook $?'; HookResults = $($true); PipelineResults = @('Hi Good') }
) {
param([string]$Expression, [object[]]$HookResults, [object[]]$PipelineResults)
$output = Invoke-Expression $Expression 2>$null
$script:hookValues | Should -Be $HookResults
$output | Should -Be $PipelineResults
}
It 'Sets $? correctly for single expression with redirection ''<Expression>''' -TestCases @(
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" > $null; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" >> $null; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" > TESTDRIVE:\out.txt; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" >> TESTDRIVE:\out.txt; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" 2> $null; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" 2>> $null; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" 2> TESTDRIVE:\out.txt; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" 2>> TESTDRIVE:\out.txt; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" 2>&1; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" 2>&1; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" 2>&1 > $null; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; "b" 2>&1 > TESTDRIVE:\out.txt; Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" > $null); Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" >> $null); Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" > TESTDRIVE:\out.txt); Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" >> TESTDRIVE:\out.txt); Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" 2> $null); Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" 2>> $null); Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" 2> TESTDRIVE:\out.txt); Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" 2>> TESTDRIVE:\out.txt); Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" 2>&1); Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" 2>&1 > $null); Hook $?'; HookResults = $($false, $true); }
@{ Expression = 'Write-Error "Bad"; Hook $?; ("b" 2>&1 > TESTDRIVE:\out.txt); Hook $?'; HookResults = $($false, $true); }
) {
param([string]$Expression, [object[]]$HookResults)
Invoke-Expression $Expression 2>&1 >$null
$script:hookValues | Should -Be $HookResults
}
Context 'Validate $? with potential terminating error' {
## Script execution directly in Pester tests will be enclosed in try/catch by the Pester,
## and therefore, general exceptions thrown from an expression like "1/0" will be turned
## into a terminating exception, which will stop the execution of remaining scripts.
##
## For those test cases, we have to use a PowerShell instance, so as to keep the default
## error handling behavior for the general exceptions.
BeforeAll {
$pwsh = [powershell]::Create()
function Invoke([string] $script)
{
$pwsh.Commands.Clear()
$pwsh.Streams.ClearStreams()
$pwsh.AddScript($script).Invoke()
}
$root = Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString())
$null = Invoke "New-PSDrive -Name TESTDRIVE -PSProvider FileSystem -Root $root"
}
Afterall {
$null = Invoke "Remove-PSDrive -Name TESTDRIVE -PSProvider FileSystem -Force"
$pwsh.Dispose()
}
It 'Sets $? correctly for single expression with redirection ''<Expression>''' -TestCases @(
@{ Expression = '1/0 > $null; $?'; Result = $false; }
@{ Expression = '1/0 >> $null; $?'; Result = $false; }
@{ Expression = '1/0 > TESTDRIVE:\out.txt; $?'; Result = $false; }
@{ Expression = '1/0 >> TESTDRIVE:\out.txt; $?'; Result = $false; }
@{ Expression = '1/0 2>&1; $?'; Result = $false; }
@{ Expression = '1/0 2>&1 > $null; $?'; Result = $false; }
@{ Expression = '1/0 2>&1 > TESTDRIVE:\out.txt; $?'; Result = $false; }
@{ Expression = '"b" > NonExistDrive:\nowhere.txt; $?'; Result = $false; }
@{ Expression = '"b" >> NonExistDrive:\nowhere.txt; $?'; Result = $false; }
@{ Expression = '"b" 2>&1 > NonExistDrive:\nowhere.txt; $?'; Result = $false; }
) {
param([string]$Expression, $Result)
Invoke $Expression | Should -Be $Result
}
}
}
|