MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe 'Breakpoint SDK Unit Tests' -Tags 'CI' {5 6 BeforeAll {7 # Start a job; this will create a runspace in which we can manage breakpoints8 $job = Start-Job -Scriptblock {9 Set-PSBreakpoint -Command Start-Sleep10 1..240 | ForEach-Object {11 Start-Sleep -Milliseconds 25012 $_13 Write-Error 'boo'14 Write-Verbose 'Verbose' -Verbose15 $DebugPreference = 'Continue'16 Write-Debug 'Debug'17 Write-Warning 'Warning'18 }19 }20 21 # Wait for the child job that gets created to hit the breakpoint. This is the22 # only safe way to know that the job has actually entered a running state and23 # that the remote runspace is listening for requests.24 Wait-UntilTrue { $job.ChildJobs.Count -gt 0 -and $job.ChildJobs[0].State -eq 'AtBreakpoint' } -TimeoutInMilliseconds 60000 -IntervalInMilliseconds 25025 26 # Get the runspace for the running job27 $jobRunspace = $job.ChildJobs[0].Runspace28 }29 30 AfterAll {31 # Remove the running job forcibly (whether it has finished or not)32 Remove-Job -Job $job -Force33 }34 35 Context 'Managing breakpoints in the host runspace via the SDK' {36 37 AfterAll {38 foreach ($bp in $Host.Runspace.Debugger.GetBreakpoints()) {39 $Host.Runspace.Debugger.RemoveBreakpoint($bp) | Should -BeTrue40 }41 }42 43 It 'Can set command breakpoints' {44 $Host.Runspace.Debugger.SetCommandBreakpoint('Test-ThisCommandDoesNotExist', $null, $null) | Should -BeOfType System.Management.Automation.CommandBreakpoint45 }46 47 It 'Can set variable breakpoints' {48 $Host.Runspace.Debugger.SetVariableBreakpoint('DebugPreference', 'ReadWrite', { continue }, $null) | Should -BeOfType System.Management.Automation.VariableBreakpoint49 }50 51 It 'Can set line breakpoints' {52 $Host.Runspace.Debugger.SetLineBreakpoint($PSCommandPath, 1, 1, { continue }) | Should -BeOfType System.Management.Automation.LineBreakpoint53 }54 55 It 'Can get breakpoints' {56 $Host.Runspace.Debugger.GetBreakpoints() | Should -HaveCount 357 }58 59 It 'Can disable breakpoints' {60 foreach ($bp in $Host.Runspace.Debugger.GetBreakpoints()) {61 $bp = $Host.Runspace.Debugger.DisableBreakpoint($bp)62 $bp.Enabled | Should -BeFalse63 }64 }65 66 It 'Can enable breakpoints' {67 foreach ($bp in $Host.Runspace.Debugger.GetBreakpoints()) {68 $bp = $Host.Runspace.Debugger.EnableBreakpoint($bp)69 $bp.Enabled | Should -BeTrue70 }71 }72 73 It 'Can remove breakpoints' {74 foreach ($bp in $Host.Runspace.Debugger.GetBreakpoints()) {75 $Host.Runspace.Debugger.RemoveBreakpoint($bp) | Should -BeTrue76 }77 }78 79 It 'Returns an empty collection when there are no breakpoints' {80 $Host.Runspace.Debugger.GetBreakpoints() | Should -HaveCount 081 }82 83 It 'Can set multiple breakpoints' {84 $breakpoints = [System.Collections.Generic.List[System.Management.Automation.Breakpoint]] @(85 [System.Management.Automation.LineBreakpoint]::new("/Path/to/foo.ps1", 1)86 [System.Management.Automation.LineBreakpoint]::new("/Path/to/foo.ps1", 2)87 [System.Management.Automation.LineBreakpoint]::new("/Path/to/foo.ps1", 3)88 )89 90 $Host.Runspace.Debugger.SetBreakpoints($breakpoints)91 $Host.Runspace.Debugger.GetBreakpoints() | Should -HaveCount 392 }93 }94 95 Context 'Managing breakpoints in a remote runspace via the SDK' {96 97 AfterAll {98 foreach ($bp in $jobRunspace.Debugger.GetBreakpoints()) {99 $jobRunspace.Debugger.RemoveBreakpoint($bp) | Should -BeTrue100 }101 }102 103 It 'Can set command breakpoints' {104 $jobRunspace.Debugger.SetCommandBreakpoint('Write-Verbose', { break }, $null) | Should -BeOfType System.Management.Automation.CommandBreakpoint105 }106 107 It 'Can set variable breakpoints' {108 $jobRunspace.Debugger.SetVariableBreakpoint('DebugPreference', 'ReadWrite', { break }, $null) | Should -BeOfType System.Management.Automation.VariableBreakpoint109 }110 111 It 'Can set line breakpoints' {112 $jobRunspace.Debugger.SetLineBreakpoint($PSCommandPath, 1, 1, { break }) | Should -BeOfType System.Management.Automation.LineBreakpoint113 }114 115 It 'Can get breakpoints' {116 # This is 4, not 3, because we set a breakpoint in our job script117 $jobRunspace.Debugger.GetBreakpoints() | Should -HaveCount 4118 }119 120 It 'Can disable breakpoints' {121 foreach ($bp in $jobRunspace.Debugger.GetBreakpoints()) {122 $bp = $jobRunspace.Debugger.DisableBreakpoint($bp)123 $bp.Enabled | Should -BeFalse124 }125 }126 127 It 'Can enable breakpoints' {128 foreach ($bp in $jobRunspace.Debugger.GetBreakpoints()) {129 $bp = $jobRunspace.Debugger.EnableBreakpoint($bp)130 $bp.Enabled | Should -BeTrue131 }132 }133 134 It 'Doesn''t manipulate any breakpoints in the default runspace' {135 # Issue https://github.com/PowerShell/PowerShell/issues/10167 fix:136 # Ensure that breakpoints were not created in the default runspace.137 # Prior to this issue being fixed, breakpoints with the same id138 # would be created or updated in the default runspace.139 $Host.Runspace.Debugger.GetBreakpoints() | Should -BeNullOrEmpty140 }141 142 It 'Can remove breakpoints' {143 foreach ($bp in $jobRunspace.Debugger.GetBreakpoints()) {144 $jobRunspace.Debugger.RemoveBreakpoint($bp) | Should -BeTrue145 }146 }147 148 It 'Returns an empty collection when there are no breakpoints' {149 $jobRunspace.Debugger.GetBreakpoints() | Should -HaveCount 0150 }151 152 It 'Can set multiple breakpoints' {153 $breakpoints = [System.Collections.Generic.List[System.Management.Automation.Breakpoint]] @(154 [System.Management.Automation.LineBreakpoint]::new("/Path/to/foo.ps1", 1)155 [System.Management.Automation.LineBreakpoint]::new("/Path/to/foo.ps1", 2)156 [System.Management.Automation.LineBreakpoint]::new("/Path/to/foo.ps1", 3)157 )158 159 $jobRunspace.Debugger.SetBreakpoints($breakpoints)160 $jobRunspace.Debugger.GetBreakpoints() | Should -HaveCount 3161 }162 }163 164 Context 'Handling empty collections and errors while managing breakpoints in the host runspace via the SDK' {165 166 BeforeAll {167 $bp = [System.Management.Automation.CommandBreakpoint]::new($TestDrive, $null, 'Test-ThisCommandDoesNotExist')168 }169 170 It 'Returns false when trying to disable a breakpoint that does not exist' {171 $Host.Runspace.Debugger.DisableBreakpoint($bp) | Should -Be $null172 }173 174 It 'Returns false when trying to enable a breakpoint that does not exist' {175 $Host.Runspace.Debugger.EnableBreakpoint($bp) | Should -Be $null176 }177 178 It 'Returns false when trying to remove a breakpoint that does not exist' {179 $Host.Runspace.Debugger.RemoveBreakpoint($bp) | Should -BeFalse180 }181 }182 183 Context 'Handling errors while managing breakpoints in a remote runspace via the SDK' {184 185 BeforeAll {186 $bp = $jobRunspace.Debugger.SetCommandBreakpoint('Test-ThisCommandDoesNotExist', $null, $null)187 $jobRunspace.Debugger.RemoveBreakpoint($bp) > $null188 }189 190 It 'Returns false when trying to disable a breakpoint that does not exist' {191 $jobRunspace.Debugger.DisableBreakpoint($bp) | Should -Be $null192 }193 194 It 'Returns false when trying to enable a breakpoint that does not exist' {195 $jobRunspace.Debugger.EnableBreakpoint($bp) | Should -Be $null196 }197 198 It 'Returns false when trying to remove a breakpoint that does not exist' {199 $jobRunspace.Debugger.RemoveBreakpoint($bp) | Should -BeFalse200 }201 }202 203 Context 'Manage breakpoints in another runspace' {204 BeforeAll {205 $runspace = [runspacefactory]::CreateRunspace()206 $runspace.Open()207 }208 209 AfterAll {210 $runspace.Close()211 $runspace.Dispose()212 }213 214 It 'Can set command breakpoints' {215 $Host.Runspace.Debugger.SetCommandBreakpoint('Test-ThisCommandDoesNotExist', $null, $null, $runspace.Id) | Should -BeOfType System.Management.Automation.CommandBreakpoint216 }217 218 It 'Can set variable breakpoints' {219 $Host.Runspace.Debugger.SetVariableBreakpoint('DebugPreference', 'ReadWrite', { continue }, $null, $runspace.Id) | Should -BeOfType System.Management.Automation.VariableBreakpoint220 }221 222 It 'Can set line breakpoints' {223 $Host.Runspace.Debugger.SetLineBreakpoint($PSCommandPath, 1, 1, { continue }, $runspace.Id) | Should -BeOfType System.Management.Automation.LineBreakpoint224 }225 226 It 'Can get breakpoints' {227 $Host.Runspace.Debugger.GetBreakpoints($runspace.Id) | Should -HaveCount 3228 }229 230 It 'Can disable breakpoints' {231 foreach ($bp in $Host.Runspace.Debugger.GetBreakpoints($runspace.Id)) {232 $bp = $Host.Runspace.Debugger.DisableBreakpoint($bp, $runspace.Id)233 $bp.Enabled | Should -BeFalse234 }235 }236 237 It 'Can enable breakpoints' {238 foreach ($bp in $Host.Runspace.Debugger.GetBreakpoints($runspace.Id)) {239 $bp = $Host.Runspace.Debugger.EnableBreakpoint($bp, $runspace.Id)240 $bp.Enabled | Should -BeTrue241 }242 }243 244 It 'Doesn''t manipulate any breakpoints in the default runspace' {245 $Host.Runspace.Debugger.GetBreakpoints() | Should -BeNullOrEmpty246 }247 248 It 'Can remove breakpoints' {249 foreach ($bp in $Host.Runspace.Debugger.GetBreakpoints($runspace.Id)) {250 $Host.Runspace.Debugger.RemoveBreakpoint($bp, $runspace.Id) | Should -BeTrue251 }252 }253 254 It 'Returns an empty collection when there are no breakpoints' {255 $Host.Runspace.Debugger.GetBreakpoints($runspace.Id) | Should -HaveCount 0256 }257 258 It 'Can set multiple breakpoints' {259 $breakpoints = [System.Collections.Generic.List[System.Management.Automation.Breakpoint]] @(260 [System.Management.Automation.LineBreakpoint]::new("/Path/to/foo.ps1", 1)261 [System.Management.Automation.LineBreakpoint]::new("/Path/to/foo.ps1", 2)262 [System.Management.Automation.LineBreakpoint]::new("/Path/to/foo.ps1", 3)263 )264 265 $Host.Runspace.Debugger.SetBreakpoints($breakpoints, $runspace.Id)266 $Host.Runspace.Debugger.GetBreakpoints($runspace.Id) | Should -HaveCount 3267 }268 }269}270 