File size: 15,574 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'Basic debugger tests' -Tag 'CI' {
BeforeAll {
Register-DebuggerHandler
}
AfterAll {
Unregister-DebuggerHandler
}
Context 'The value of $? should be preserved when exiting the debugger' {
BeforeAll {
$testScript = {
function Test-DollarQuestionMark {
[CmdletBinding()]
param()
Get-Process -Id ([int]::MaxValue)
if (-not $?) {
'The value of $? was preserved during debugging.'
} else {
'The value of $? was changed to $true during debugging.'
}
}
$global:DollarQuestionMarkResults = Test-DollarQuestionMark -ErrorAction Break
}
$global:results = @(Test-Debugger -Scriptblock $testScript -CommandQueue '$?')
}
AfterAll {
Remove-Variable -Name DollarQuestionMarkResults -Scope Global -ErrorAction Ignore
}
It 'Should show 2 debugger commands were invoked' {
# One extra for the implicit 'c' command that keeps the debugger automation moving
$results.Count | Should -Be 2
}
It 'Should have $false output from the first $? command' {
$results[0].Output | Should -BeOfType bool
$results[0].Output | Should -Not -BeTrue
}
It 'Should have string output showing that $? was preserved as $false by the debugger' {
$global:DollarQuestionMarkResults | Should -BeOfType string
$global:DollarQuestionMarkResults | Should -BeExactly 'The value of $? was preserved during debugging.'
}
}
}
Describe "Breakpoints when set should be hit" -Tag "CI" {
Context "Basic tests" {
BeforeAll {
$script = @'
'aaa'.ToString() > $null
'aa' > $null
"a" 2> $null | ForEach-Object { $_ }
'bb' > $null
'bb'.ToSTring() > $null
'bbb'
'@
$path = Setup -PassThru -File BasicTest.ps1 -Content $script
$bps = 1..6 | ForEach-Object { Set-PSBreakpoint -Script $path -Line $_ -Action { continue } }
}
AfterAll {
$bps | Remove-PSBreakpoint
}
It "A redirected breakpoint is hit" {
& $path
foreach ( $bp in $bps ) {
$bp.HitCount | Should -Be 1
}
}
}
Context "Break point on switch condition should be hit only when enumerating it" {
BeforeAll {
$script = @'
$test = 1..2
switch ($test)
{
default {}
}
'@
$path = Setup -PassThru -File SwitchScript.ps1 -Content $script
$breakpoint = Set-PSBreakpoint -Script $path -Line 2 -Action { continue }
}
AfterAll {
Remove-PSBreakpoint -Breakpoint $breakpoint
}
It "switch condition should be hit 3 times" {
## MoveNext() will be called on the condition for 3 times
$null = & $path
$breakpoint.HitCount | Should -Be 3
}
}
Context "Break point on for-statement initializer should be hit" {
BeforeAll {
$for_script_1 = @'
$test = 2
for ("string".Length;
$test -gt 0; $test--) { }
'@
$for_script_2 = @'
$test = $PSCommandPath
for (Test-Path $test;
$test -eq "blah";) { }
'@
$for_script_3 = @'
for ($test = 2;
$test -gt 0; $test--) { }
'@
$for_script_4 = @'
$test = 2
for (;$test -gt 0;
$test--) { }
'@
$for_script_5 = @'
$test = $PSCommandPath
for (;Test-Path $test;)
{
$test = "blah"
}
'@
$ForScript_1 = Setup -PassThru -File ForScript_1.ps1 -Content $for_script_1
$bp_1 = Set-PSBreakpoint -Script $ForScript_1 -Line 2 -Action { continue }
$ForScript_2 = Setup -PassThru -File ForScript_2.ps1 -Content $for_script_2
$bp_2 = Set-PSBreakpoint -Script $ForScript_2 -Line 2 -Action { continue }
$ForScript_3 = Setup -PassThru -File ForScript_3.ps1 -Content $for_script_3
$bp_3 = Set-PSBreakpoint -Script $ForScript_3 -Line 1 -Action { continue }
$ForScript_4 = Setup -PassThru -File ForScript_4.ps1 -Content $for_script_4
$bp_4 = Set-PSBreakpoint -Script $ForScript_4 -Line 2 -Action { continue }
$ForScript_5 = Setup -PassThru -File ForScript_5.ps1 -Content $for_script_5
$bp_5 = Set-PSBreakpoint -Script $ForScript_5 -Line 2 -Action { continue }
$testCases = @(
@{ Name = "expression initializer should be hit once"; Path = $ForScript_1; Breakpoint = $bp_1; HitCount = 1 }
@{ Name = "pipeline initializer should be hit once"; Path = $ForScript_2; Breakpoint = $bp_2; HitCount = 1 }
@{ Name = "assignment initializer should be hit 3 times"; Path = $ForScript_3; Breakpoint = $bp_3; HitCount = 1 }
@{ Name = "pipeline condition should be hit 3 times"; Path = $ForScript_4; Breakpoint = $bp_4; HitCount = 3 }
@{ Name = "pipeline condition should be hit 2 times"; Path = $ForScript_5; Breakpoint = $bp_5; HitCount = 2 }
)
}
AfterAll {
Get-PSBreakpoint -Script $ForScript_1, $ForScript_2, $ForScript_3, $ForScript_4, $ForScript_5 | Remove-PSBreakpoint
}
It "for-statement <Name>" -TestCases $testCases {
param($Path, $Breakpoint, $HitCount)
$null = & $Path
$Breakpoint.HitCount | Should -Be $HitCount
}
}
Context "Break point on while loop condition should be hit" {
BeforeAll {
$while_script_1 = @'
$test = "string"
while ($test.Contains("str"))
{
$test = "blah"
}
'@
$while_script_2 = @'
$test = $PSCommandPath
while (Test-Path $test)
{
$test = "blah"
}
'@
$WhileScript_1 = Setup -PassThru -File WhileScript_1.ps1 -Content $while_script_1
$bp_1 = Set-PSBreakpoint -Script $WhileScript_1 -Line 2 -Action { continue }
$WhileScript_2 = Setup -PassThru -File WhileScript_2.ps1 -Content $while_script_2
$bp_2 = Set-PSBreakpoint -Script $WhileScript_2 -Line 2 -Action { continue }
$testCases = @(
@{ Name = "expression condition should be hit 2 times"; Path = $WhileScript_1; Breakpoint = $bp_1; HitCount = 2 }
@{ Name = "pipeline condition should be hit 2 times"; Path = $WhileScript_2; Breakpoint = $bp_2; HitCount = 2 }
)
}
AfterAll {
Get-PSBreakpoint -Script $WhileScript_1, $WhileScript_2 | Remove-PSBreakpoint
}
It "while loop <Name>" -TestCases $testCases {
param($Path, $Breakpoint, $HitCount)
$null = & $Path
$Breakpoint.HitCount | Should -Be $HitCount
}
}
Context "Break point on do-while loop condition should be hit" {
BeforeAll {
$do_while_script_1 = @'
$test = "blah"
do { echo $test }
while ($test.Contains("str"))
'@
$do_while_script_2 = @'
$test = "blah"
do { echo $test }
while (Test-Path $test)
'@
$DoWhileScript_1 = Setup -PassThru -File DoWhileScript_1.ps1 -Content $do_while_script_1
$bp_1 = Set-PSBreakpoint -Script $DoWhileScript_1 -Line 2 -Action { continue }
$DoWhileScript_2 = Setup -PassThru -File DoWhileScript_2.ps1 -Content $do_while_script_2
$bp_2 = Set-PSBreakpoint -Script $DoWhileScript_2 -Line 2 -Action { continue }
$testCases = @(
@{ Name = "expression condition should be hit 2 times"; Path = $DoWhileScript_1; Breakpoint = $bp_1; HitCount = 1 }
@{ Name = "pipeline condition should be hit 2 times"; Path = $DoWhileScript_2; Breakpoint = $bp_2; HitCount = 1 }
)
}
AfterAll {
Get-PSBreakpoint -Script $DoWhileScript_1, $DoWhileScript_2 | Remove-PSBreakpoint
}
It "Do-While loop <Name>" -TestCases $testCases {
param($Path, $Breakpoint, $HitCount)
$null = & $Path
$Breakpoint.HitCount | Should -Be $HitCount
}
}
Context "Break point on do-until loop condition should be hit" {
BeforeAll {
$do_until_script_1 = @'
$test = "blah"
do { echo $test }
until ($test.Contains("bl"))
'@
$do_until_script_2 = @'
$test = $PSCommandPath
do { echo $test }
until (Test-Path $test)
'@
$DoUntilScript_1 = Setup -PassThru -File DoUntilScript_1.ps1 -Content $do_until_script_1
$bp_1 = Set-PSBreakpoint -Script $DoUntilScript_1 -Line 2 -Action { continue }
$DoUntilScript_2 = Setup -PassThru -File DoUntilScript_2.ps1 -Content $do_until_script_2
$bp_2 = Set-PSBreakpoint -Script $DoUntilScript_2 -Line 2 -Action { continue }
$testCases = @(
@{ Name = "expression condition should be hit 2 times"; Path = $DoUntilScript_1; Breakpoint = $bp_1; HitCount = 1 }
@{ Name = "pipeline condition should be hit 2 times"; Path = $DoUntilScript_2; Breakpoint = $bp_2; HitCount = 1 }
)
}
AfterAll {
Get-PSBreakpoint -Script $DoUntilScript_1, $DoUntilScript_2 | Remove-PSBreakpoint
}
It "Do-Until loop <Name>" -TestCases $testCases {
param($Path, $Breakpoint, $HitCount)
$null = & $Path
$Breakpoint.HitCount | Should -Be $HitCount
}
}
Context "Break point on if condition should be hit" {
BeforeAll {
$if_script_1 = @'
if ("string".Contains('str'))
{ }
'@
$if_script_2 = @'
if (Test-Path $PSCommandPath)
{ }
'@
$if_script_3 = @'
if ($false) {}
elseif ("string".Contains('str'))
{ }
'@
$if_script_4 = @'
if ($false) {}
elseif (Test-Path $PSCommandPath)
{ }
'@
$IfScript_1 = Setup -PassThru -File IfScript_1.ps1 -Content $if_script_1
$bp_1 = Set-PSBreakpoint -Script $IfScript_1 -Line 1 -Action { continue }
$IfScript_2 = Setup -PassThru -File IfScript_2.ps1 -Content $if_script_2
$bp_2 = Set-PSBreakpoint -Script $IfScript_2 -Line 1 -Action { continue }
$IfScript_3 = Setup -PassThru -File IfScript_3.ps1 -Content $if_script_3
$bp_3 = Set-PSBreakpoint -Script $IfScript_3 -Line 2 -Action { continue }
$IfScript_4 = Setup -PassThru -File IfScript_4.ps1 -Content $if_script_4
$bp_4 = Set-PSBreakpoint -Script $IfScript_4 -Line 2 -Action { continue }
$testCases = @(
@{ Name = "expression if-condition should be hit once"; Path = $IfScript_1; Breakpoint = $bp_1; HitCount = 1 }
@{ Name = "pipeline if-condition should be hit once"; Path = $IfScript_2; Breakpoint = $bp_2; HitCount = 1 }
@{ Name = "expression elseif-condition should be hit once"; Path = $IfScript_3; Breakpoint = $bp_3; HitCount = 1 }
@{ Name = "pipeline elseif-condition should be hit once"; Path = $IfScript_4; Breakpoint = $bp_4; HitCount = 1 }
)
}
AfterAll {
Get-PSBreakpoint -Script $IfScript_1, $IfScript_2, $IfScript_3, $IfScript_4 | Remove-PSBreakpoint
}
It "If statement <Name>" -TestCases $testCases {
param($Path, $Breakpoint, $HitCount)
$null = & $Path
$Breakpoint.HitCount | Should -Be $HitCount
}
}
Context "Break point on return should hit" {
BeforeAll {
$return_script_1 = @'
return
'@
$return_script_2 = @'
return 10
'@
$return_script_3 = @'
trap {
'statement to ignore trap registration sequence point'
return
}
throw
'@
$ReturnScript_1 = Setup -PassThru -File ReturnScript_1.ps1 -Content $return_script_1
$bp_1 = Set-PSBreakpoint -Script $ReturnScript_1 -Line 1 -Action { continue }
$ReturnScript_2 = Setup -PassThru -File ReturnScript_2.ps1 -Content $return_script_2
$bp_2 = Set-PSBreakpoint -Script $ReturnScript_2 -Line 1 -Action { continue }
$ReturnScript_3 = Setup -PassThru -File ReturnScript_3.ps1 -Content $return_script_3
$bp_3 = Set-PSBreakpoint -Script $ReturnScript_3 -Line 3 -Action { continue }
$testCases = @(
@{ Name = "return without pipeline should be hit once"; Path = $ReturnScript_1; Breakpoint = $bp_1; HitCount = 1 }
@{ Name = "return with pipeline should be hit once"; Path = $ReturnScript_2; Breakpoint = $bp_2; HitCount = 1 }
@{ Name = "return from trap should be hit once"; Path = $ReturnScript_3; Breakpoint = $bp_3; HitCount = 1 }
)
}
AfterAll {
Get-PSBreakpoint -Script $ReturnScript_1, $ReturnScript_2, $ReturnScript_3 | Remove-PSBreakpoint
}
It "Return statement <Name>" -TestCases $testCases {
param($Path, $Breakpoint, $HitCount)
$null = & $Path
$Breakpoint.HitCount | Should -Be $HitCount
}
}
}
Describe "It should be possible to reset runspace debugging" -Tag "Feature" {
BeforeAll {
$script = @'
"line 1"
"line 2"
"line 3"
'@
$scriptPath = Setup -PassThru -File TestScript.ps1 -Content $script
$iss = [initialsessionstate]::CreateDefault2();
$rs = [runspacefactory]::CreateRunspace($iss)
$rs.Name = "TestRunspaceDebuggerReset"
$rs.Open()
$rs | Enable-RunspaceDebug
$debuggerBeforeReset = $rs.Debugger
# Create PowerShell to run script.
$ps = [powershell]::Create()
$ps.Runspace = $rs
# Set breakpoints in runspace.
$result = $ps.AddScript("Set-PSBreakpoint -Script '$scriptPath' -line 1").Invoke()
$ps.Commands.Clear()
$result = $ps.AddScript("Set-PSBreakpoint -Script '$scriptPath' -line 3").Invoke()
$ps.Commands.Clear()
$breakpoints = $ps.AddScript("Get-PSBreakpoint").Invoke()
# Run script file until breakpoint hit.
$ar = $ps.AddScript("$scriptPath").BeginInvoke()
$completed = Wait-UntilTrue { $rs.Debugger.InBreakPoint -eq $true } -timeout 10000 -interval 200
$ps.Stop()
$rs.ResetRunspaceState()
}
AfterAll {
if ( $null -ne $ps ) { $ps.Dispose() }
if ( $null -ne $ss ) { $rs.Dispose() }
}
It "2 breakpoints should have been set" {
$breakpoints.Count | Should -Be 2
}
It "The breakpoint Should have been hit" {
$completed | Should -BeTrue
}
It "The reset debugger should not be in a breakpoint" {
$rs.Debugger.InBreakPoint | Should -BeFalse
}
It "The reset debugger should not be active" {
$rs.Debugger.IsActive | Should -BeFalse
}
It "The reset debugger mode should be set to 'Default'" {
$rs.Debugger.DebugMode | Should -Be "Default"
}
It "The debugger should be the same before and after the reset" {
$rs.Debugger | Should -Be $debuggerBeforeReset
}
It "The breakpoints should be gone after reset" {
$ps.Commands.clear()
$ps.AddCommand("Get-PSBreakpoint").Invoke() | Should -BeNullOrEmpty
}
It "The script should run without a break" {
$ps.Commands.Clear()
$ps.addscript($scriptPath).Invoke().Count | Should -Be 3
}
}
|