MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Event Subscriber Tests" -Tags "Feature" {5 BeforeEach {6 Get-EventSubscriber | Unregister-Event7 }8 AfterEach {9 Get-EventSubscriber | Unregister-Event10 }11 12 # can't let this case to work13 It "Register an event with no action, trigger it and wait for it to be raised." -Pending:$true{14 Get-EventSubscriber | Should -BeNullOrEmpty15 $messageData = New-Object psobject16 $job = Start-Job { Start-Sleep -Seconds 5; 1..5 }17 $null = Register-ObjectEvent $job -EventName StateChanged -SourceIdentifier EventSIDTest -Action {} -MessageData $messageData18 New-Event EventSIDTest19 20 Wait-Event EventSIDTest21 $eventdata = Get-Event EventSIDTest22 $eventdata.MessageData | Should -Be $messageData23 Remove-Event EventSIDTest24 Unregister-Event EventSIDTest25 Get-EventSubscriber | Should -BeNullOrEmpty26 }27 28 It "Access a global variable from an event action." {29 Get-EventSubscriber | Should -BeNullOrEmpty30 Set-Variable incomingGlobal -Scope global -Value globVarValue31 $null = Register-EngineEvent -SourceIdentifier foo -Action {Set-Variable -Scope global -Name aglobalvariable -Value $incomingGlobal}32 New-Event foo33 $getvar = Get-Variable aglobalvariable -Scope global34 $getvar.Name | Should -Be aglobalvariable35 $getvar.Value | Should -Be globVarValue36 Unregister-Event foo37 Get-EventSubscriber | Should -BeNullOrEmpty38 }39 40 It 'Should not throw when having finally block in Powershell.Exiting Action scriptblock' {41 $pwsh = "$PSHOME/pwsh"42 $output = & $pwsh {43 Register-EngineEvent -SourceIdentifier Powershell.Exiting -Action {44 try{45 try{} finally{}46 }47 catch{ Write-Host "Exception" -NoNewline }48 }49 } | Out-String50 51 $output | Should -Not -BeLike "*Exception*"52 }53}54 