mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add new debugging tests which are based on existing earlier non-Pester tests
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user