|
| 1 | + |
| 2 | +$script:ExceptionRegex = [regex]::new('\s*Exception: (.*)$', 'Compiled,Multiline,IgnoreCase') |
| 3 | +function ReportLogErrors |
| 4 | +{ |
| 5 | + param( |
| 6 | + [Parameter()][string]$LogPath, |
| 7 | + |
| 8 | + [Parameter()][ref]<#[int]#>$FromIndex = 0, |
| 9 | + |
| 10 | + [Parameter()][string[]]$IgnoreException = @() |
| 11 | + ) |
| 12 | + |
| 13 | + $logEntries = Parse-PsesLog $LogPath | |
| 14 | + Where-Object Index -ge $FromIndex.Value |
| 15 | + |
| 16 | + # Update the index to the latest in the log |
| 17 | + $FromIndex.Value = ($FromIndex.Value,$errorLogs.Index | Measure-Object -Maximum).Maximum |
| 18 | + |
| 19 | + $errorLogs = $logEntries | |
| 20 | + Where-Object LogLevel -eq Error | |
| 21 | + Where-Object { |
| 22 | + $match = $script:ExceptionRegex.Match($_.Message.Data) |
| 23 | + |
| 24 | + (-not $match) -or ($match.Groups[1].Value.Trim() -notin $IgnoreException) |
| 25 | + } |
| 26 | + |
| 27 | + if ($errorLogs) |
| 28 | + { |
| 29 | + $errorLogs | ForEach-Object { Write-Error "ERROR from PSES log: $($_.Message.Data)" } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +Describe "Loading and running PowerShellEditorServices" { |
| 34 | + BeforeAll { |
| 35 | + Import-Module -Force "$PSScriptRoot/../../module/PowerShellEditorServices" |
| 36 | + Import-Module -Force "$PSScriptRoot/../../tools/PsesPsClient/out/PsesPsClient" |
| 37 | + Import-Module -Force "$PSScriptRoot/../../tools/PsesLogAnalyzer" |
| 38 | + |
| 39 | + $logIdx = 0 |
| 40 | + $psesServer = Start-PsesServer |
| 41 | + $client = Connect-PsesServer -PipeName $psesServer.SessionDetails.languageServicePipeName |
| 42 | + } |
| 43 | + |
| 44 | + # This test MUST be first |
| 45 | + It "Starts and responds to an initialization request" { |
| 46 | + $request = Send-LspInitializeRequest -Client $client |
| 47 | + $response = Get-LspResponse -Client $client -Id $request.Id |
| 48 | + $response.Id | Should -BeExactly $request.Id |
| 49 | + |
| 50 | + ReportLogErrors -LogPath $psesServer.LogPath -FromIndex ([ref]$logIdx) |
| 51 | + } |
| 52 | + |
| 53 | + # This test MUST be last |
| 54 | + It "Shuts down the process properly" { |
| 55 | + $request = Send-LspShutdownRequest -Client $client |
| 56 | + $response = Get-LspResponse -Client $client -Id $request.Id |
| 57 | + $response.Id | Should -BeExactly $request.Id |
| 58 | + $response.Result | Should -BeNull |
| 59 | + # TODO: The server seems to stay up waiting for the debug connection |
| 60 | + # $psesServer.PsesProcess.HasExited | Should -BeTrue |
| 61 | + |
| 62 | + # We close the process here rather than in an AfterAll |
| 63 | + # since errors can occur and we want to test for them. |
| 64 | + # Naturally this depends on Pester executing tests in order. |
| 65 | + |
| 66 | + # We also have to dispose of everything properly, |
| 67 | + # which means we have to use these cascading try/finally statements |
| 68 | + try |
| 69 | + { |
| 70 | + $psesServer.PsesProcess.Kill() |
| 71 | + } |
| 72 | + finally |
| 73 | + { |
| 74 | + try |
| 75 | + { |
| 76 | + $psesServer.PsesProcess.Dispose() |
| 77 | + } |
| 78 | + finally |
| 79 | + { |
| 80 | + $client.Dispose() |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + ReportLogErrors -LogPath $psesServer.LogPath -FromIndex ([ref]$logIdx) |
| 85 | + } |
| 86 | +} |
0 commit comments