|
| 1 | + |
| 2 | + ########################################### |
| 3 | +# # |
| 4 | +# Assert Functions # |
| 5 | +# # |
| 6 | +# Copyright (C) Microsoft Corporation, 2016 # |
| 7 | +# # |
| 8 | + ########################################### |
| 9 | + |
| 10 | +# |
| 11 | +# Assert <bool> |
| 12 | +# |
| 13 | +# AssertEquals <object1> <object2> |
| 14 | +# |
| 15 | +# AssertEqualsCaseInsensitive <object1> <object2> |
| 16 | +# |
| 17 | +# AssertNotEquals <object1> <object2> |
| 18 | +# |
| 19 | +# AssertNotEqualsCaseInsensitive <object1> <object2> |
| 20 | +# |
| 21 | +# AssertNotNull <object> |
| 22 | +# |
| 23 | +# AssertFullyQualifiedErrorIdEquals <scriptblock> <expectedFullyQualifiedErrorId> |
| 24 | +# |
| 25 | + |
| 26 | +# |
| 27 | +# Converts a callstack into a simple string we can use in the error message of the form: |
| 28 | +# bar.ps1: Line 1 <- foo.ps1: Line 1 <- prompt |
| 29 | +# |
| 30 | +function CallStackToString |
| 31 | +{ |
| 32 | + param($callstack) |
| 33 | + |
| 34 | + $str = @($callStack | foreach { $_.Location }) -join ' <- '; |
| 35 | + return $str; |
| 36 | +} |
| 37 | + |
| 38 | +# |
| 39 | +# Creates a new exception for when the Assert fails |
| 40 | +# |
| 41 | +function NewException |
| 42 | +{ |
| 43 | + param |
| 44 | + ( |
| 45 | + $message |
| 46 | + ) |
| 47 | + |
| 48 | + $callstack = get-pscallstack |
| 49 | + $callStackStr = CallStackToString ($callstack | select -skip 1); |
| 50 | + $errMessage = $message; |
| 51 | + $exception = new-object System.Management.Automation.RuntimeException $errMessage; |
| 52 | + $exception.Data['PSCallStack'] = $callstack; |
| 53 | + |
| 54 | + return $exception; |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +# Usage: Assert <bool> <message> |
| 59 | +# |
| 60 | +function Assert |
| 61 | +{ |
| 62 | + $errMessage = ''; |
| 63 | + |
| 64 | + if ($args.Length -ne 2) |
| 65 | + { |
| 66 | + $errMessage = "Assert takes two parameters." |
| 67 | + } |
| 68 | + |
| 69 | + if (!$args[0]) |
| 70 | + { |
| 71 | + $errMessage = $args[1] |
| 72 | + } |
| 73 | + |
| 74 | + if ($errMessage) |
| 75 | + { |
| 76 | + throw (NewException $errMessage); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +# Usage: AssertFullyQualifiedErrorIdEquals <scriptblock> <expectedFullyQualifiedErrorId> |
| 81 | +# |
| 82 | +function AssertFullyQualifiedErrorIdEquals([scriptblock]$scriptblock, [string]$expectedFullyQualifiedErrorId) |
| 83 | +{ |
| 84 | + # Save old error action preference to restore it in the finally block. |
| 85 | + $oldErrorActionPreference = $ErrorActionPreference |
| 86 | + |
| 87 | + try |
| 88 | + { |
| 89 | + $ErrorActionPreference = "Continue" |
| 90 | + $myError = $null |
| 91 | + $myError = . { $out = & $scriptblock } 2>&1 |
| 92 | + } |
| 93 | + catch |
| 94 | + { |
| 95 | + $myError = $_ |
| 96 | + if ($myError -eq $null) |
| 97 | + { |
| 98 | + throw (NewException "No error records were writen for the given script block: $scriptblock"); |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + $message = "FullyQualifiedId does not match: Excepted '" + $expectedFullyQualifiedErrorId + "' and got '" + $myError.FullyQualifiedErrorId + "'" |
| 103 | + AssertEquals $myError.FullyQualifiedErrorId $expectedFullyQualifiedErrorId $message |
| 104 | + } |
| 105 | + finally |
| 106 | + { |
| 107 | + $ErrorActionPreference = $oldErrorActionPreference |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +# Usage: AssertEquals <object1> <object2> <message> |
| 112 | +# |
| 113 | +function AssertEquals |
| 114 | +{ |
| 115 | + Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertEquals takes either two or three parameters." |
| 116 | + if ($args.Length -eq 2) |
| 117 | + { |
| 118 | + Assert ($args[0] -ceq $args[1]) ("'" + $args[0] + "' does not equal '" + $args[1] + "'") |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + Assert ($args[0] -ceq $args[1]) ($args[2] + ": " + "'" + $args[0] + "' does not equal '" + $args[1] + "'") |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +# Usage: AssertEqualsCaseInsensitive <object1> <object2> <message> |
| 127 | +# |
| 128 | +function AssertEqualsCaseInsensitive |
| 129 | +{ |
| 130 | + Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertEqualsCaseInsensitive takes either two or three parameters." |
| 131 | + if ($args.Length -eq 2) |
| 132 | + { |
| 133 | + Assert ($args[0] -ieq $args[1]) ("'" + $args[0] + "' does not equal '" + $args[1] + "'") |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + Assert ($args[0] -ieq $args[1]) ($args[2] + ": " + "'" + $args[0] + "' does not equal '" + $args[1] + "'") |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +# Usage: AssertNotEquals <object1> <object2> <message> |
| 142 | +# |
| 143 | +function AssertNotEquals |
| 144 | +{ |
| 145 | + Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertNotEquals takes either two or three parameters." |
| 146 | + if ($args.Length -eq 2) |
| 147 | + { |
| 148 | + Assert ($args[0] -cne $args[1]) ("'" + $args[0] + "' equals '" + $args[1] + "'") |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + Assert ($args[0] -cne $args[1]) $args[2] |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +# Usage: AssertNotEqualsCaseInsensitive <object1> <object2> <message> |
| 157 | +# |
| 158 | +function AssertNotEqualsCaseInsensitive |
| 159 | +{ |
| 160 | + Assert ($args.Length -ge 2 -and $args.Length -le 3) "AssertNotEqualsCaseInsensitive takes either two or three parameters." |
| 161 | + if ($args.Length -eq 2) |
| 162 | + { |
| 163 | + Assert ($args[0] -ine $args[1]) ("'" + $args[0] + "' equals '" + $args[1] + "'") |
| 164 | + } |
| 165 | + else |
| 166 | + { |
| 167 | + Assert ($args[0] -ine $args[1]) $args[2] |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +# Usage: AssertNotNull <object> <message> |
| 172 | +# |
| 173 | +function AssertNotNull |
| 174 | +{ |
| 175 | + Assert ($args.Length -eq 2) "AssertNotNull takes two parameters." |
| 176 | + Assert ($args[0] -ne $()) $args[1] |
| 177 | +} |
| 178 | + |
| 179 | +# Usage: AssertNull <object> <message> |
| 180 | +# |
| 181 | +function AssertNull |
| 182 | +{ |
| 183 | + Assert ($args.Length -eq 2) "AssertNull takes two parameters." |
| 184 | + Assert ($args[0] -eq $()) $args[1] |
| 185 | +} |
| 186 | + |
| 187 | +# This function waits for either a specified amount of time or for a script block to evaluate to true and throws an exception if the timeout period elapses. Example: |
| 188 | +# |
| 189 | +# WaitFor {get-process calc} 10000 250 "Calc.exe wasn't started within 10 seconds." |
| 190 | +# |
| 191 | +function WaitFor( |
| 192 | + [Management.Automation.ScriptBlock]$scriptBlock, |
| 193 | + $timeoutInMilliseconds = 10000, |
| 194 | + $intervalInMilliseconds = 1000, |
| 195 | + $exceptionMessage = (throw "Please provide a descriptive exception message to 'WaitFor' so people don't have a hard time debugging it.") |
| 196 | + ) |
| 197 | +{ |
| 198 | + # Get the current time |
| 199 | + $startTime = [DateTime]::Now |
| 200 | + |
| 201 | + # Loop until the script block evaluates to true |
| 202 | + while (-not ($scriptBlock.Invoke())) |
| 203 | + { |
| 204 | + # Sleep for the specified interval |
| 205 | + sleep -mil $intervalInMilliseconds |
| 206 | + |
| 207 | + # If the timeout period has passed, throw an exception |
| 208 | + if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) |
| 209 | + { |
| 210 | + throw $exceptionMessage |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments