Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Debug-Runspace.Tests.ps164 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Debug-Runspace" -Tag "CI" {4    BeforeAll {5        $rs1 = [runspacefactory]::CreateRunspace()6        $rs1.Open()7        $rs1.Name = "MyRunspace1"8        $rs2 = [runspacefactory]::CreateRunspace()9        $rs2.Open()10        $rs2.Name = "MyRunspace2"11    }12    AfterAll {13        if ( $rs1 ) { $rs1.Dispose() }14        if ( $rs2 ) { $rs2.Dispose() }15    }16 17    It "Debugging a runspace should fail if the name is ambiguous" {18        { Debug-Runspace -Name "My*" -ErrorAction stop } | Should -Throw -ErrorId "DebugRunspaceTooManyRunspaceFound,Microsoft.PowerShell.Commands.DebugRunspaceCommand"19    }20 21    It "Debugging a runspace should fail if the name is not found" {22        { Debug-Runspace -Name "dflkjsdkfjldkjssldfj" -ErrorAction stop } | Should -Throw -ErrorId "DebugRunspaceNoRunspaceFound,Microsoft.PowerShell.Commands.DebugRunspaceCommand"23    }24 25    It "Debugging a runspace should fail if the runspace is not open" {26        $rs2.Close()27        { Debug-Runspace -Runspace $rs2 -ErrorAction stop } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.DebugRunspaceCommand"28    }29 30    It "Debugging a runspace should fail if the runspace has no debugger" {31        $rs1.Debugger.SetDebugMode("None")32        { Debug-Runspace -Runspace $rs1 -ErrorAction stop } | Should -Throw -ErrorId "InvalidOperation,Microsoft.PowerShell.Commands.DebugRunspaceCommand"33    }34    35    It "Should write attach event and mark runspace as having a remote debugger attached" {36        $onAttachName = [System.Management.Automation.PSEngineEvent]::OnDebugAttach37        38        $debugTarget = [PowerShell]::Create()39        $null = $debugTarget.AddCommand('Wait-Event').AddParameter('SourceIdentifier', $onAttachName)40        $waitTask = $debugTarget.BeginInvoke()41 42        $debugTarget.Runspace.IsRemoteDebuggerAttached | Should -BeFalse43 44        $debugger = [PowerShell]::Create()45        $null = $debugger.AddCommand('Debug-Runspace').AddParameter('Id', $debugTarget.Runspace.Id)46        $debugTask = $debugger.BeginInvoke()47        48        $waitTask.AsyncWaitHandle.WaitOne(5000) | Should -BeTrue49        $waitInfo = $debugTarget.EndInvoke($waitTask)50        $waitInfo.SourceIdentifier | Should -Be $onAttachName51 52        $debugTarget.Runspace.IsRemoteDebuggerAttached | Should -BeTrue53 54        $debugger.Stop()55        $exp = {56            $debugger.EndInvoke($debugTask)57        } | Should -Throw -PassThru58        $exp.FullyQualifiedErrorId | Should -Be "PipelineStoppedException"59 60        $debugTarget.Runspace.IsRemoteDebuggerAttached | Should -BeFalse61    }62}63 64