From 9c99bdbef92386a2ec0c5a1b013f5e6579f90c86 Mon Sep 17 00:00:00 2001 From: James Truher Date: Wed, 24 Aug 2016 15:58:05 -0700 Subject: [PATCH] Add new debugging tests which are based on existing earlier non-Pester tests --- test/powershell/Common/Test.Helpers.psm1 | 25 +++++ .../Scripting/Debugging/Debugging.Tests.ps1 | 96 +++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 test/powershell/Common/Test.Helpers.psm1 create mode 100644 test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 diff --git a/test/powershell/Common/Test.Helpers.psm1 b/test/powershell/Common/Test.Helpers.psm1 new file mode 100644 index 0000000000..b168c97911 --- /dev/null +++ b/test/powershell/Common/Test.Helpers.psm1 @@ -0,0 +1,25 @@ +function Wait-CompleteExecution +{ + [CmdletBinding()] + param ( + [ScriptBlock]$sb, + [int]$TimeoutInMilliseconds = 10000, + [int]$IntervalInMilliseconds = 1000 + ) + # Get the current time + $startTime = [DateTime]::Now + + # Loop until the script block evaluates to true + while (-not ($sb.Invoke())) { + # Sleep for the specified interval + start-sleep -mil $intervalInMilliseconds + + # If the timeout period has passed, throw an exception + if (([DateTime]::Now - $startTime).TotalMilliseconds -gt $timeoutInMilliseconds) + { + return $false + } + } + return $true +} +export-modulemember -function Wait-CompleteExecution diff --git a/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 b/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 new file mode 100644 index 0000000000..402d43b578 --- /dev/null +++ b/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 @@ -0,0 +1,96 @@ +$script1 = @' +'aaa'.ToString() > $null +'aa' > $null +"a" 2> $null | % { $_ } +'bb' > $null +'bb'.ToSTring() > $null +'bbb' +'@ +$script2 = @' +"line 1" +"line 2" +"line 3" +'@ + +$root = git rev-parse --show-toplevel +$pestertestroot = join-path $root test/powershell +$common = join-path $pestertestroot Common +$helperModule = join-path $common Test.Helpers.psm1 + +Describe "Breakpoints when set should be hit" -tag "CI" { + BeforeAll { + $path = setup -pass -f TestScript_1.ps1 -content $script1 + $bps = 1..6 | %{ 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 + } + } +} + +Describe "It should be possible to reset runspace debugging" { + BeforeAll { + import-module $helperModule -force + $path = setup -pass -f TestScript_2.ps1 -content $script2 + $scriptPath = "$testdrive/TestScript_2.ps1" + $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-CompleteExecution { $rs.Debugger.InBreakPoint -eq $true } -timeout 10000 -interval 200 + $ps.Stop() + $rs.ResetRunspaceState() + } + AfterAll { + if ( $ps -ne $null ) { $ps.Dispose() } + if ( $ss -ne $null ) { $rs.Dispose() } + } + It "2 breakpoints should have been set" { + $breakpoints.Count | Should be 2 + } + It "The breakpoint Should have been hit" { + $completed | should be $true + } + It "The reset debugger should not be in a breakpoint" { + $rs.Debugger.InBreakPoint | should be $false + } + It "The reset debugger should not be active" { + $rs.Debugger.IsActive | should be $false + } + 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("$testdrive/TestScript_2.ps1").Invoke().Count | should be 3 + } +}