Skip to content

Commit 9922f6b

Browse files
authored
move stacktrace test utils to its own file (#35)
1 parent 9b04cf8 commit 9922f6b

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

tests/stacktrace.tests.ps1

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
BeforeAll {
22
. "$PSScriptRoot/utils.ps1"
3+
. "$PSScriptRoot/throwing.ps1"
34
$events = [System.Collections.Generic.List[Sentry.SentryEvent]]::new();
45
$transport = [RecordingTransport]::new()
56
StartSentryForEventTests ([ref] $events) ([ref] $transport)
67

8+
function ContextLines($start, $lines, $path = $null)
9+
{
10+
if ($null -eq $path)
11+
{
12+
$path = "$PSScriptRoot/throwing.ps1"
13+
}
14+
15+
Get-Content $path | Select-Object -Skip ($start - 1) -First $lines
16+
}
17+
718
$checkFrame = {
819
param([Sentry.SentryStackFrame] $frame, [string] $funcName, [int] $funcLine)
920
$frame.Function | Should -Be $funcName
10-
$frame.AbsolutePath | Should -Be (Join-Path $PSScriptRoot 'utils.ps1')
21+
$frame.AbsolutePath | Should -Be (Join-Path $PSScriptRoot 'throwing.ps1')
1122
$frame.LineNumber | Should -BeGreaterThan 0
1223
$frame.InApp | Should -Be $true
1324
$frame.PreContext | Should -Be (ContextLines -Start ($funcLine - 5) -Lines 5)
@@ -28,26 +39,26 @@ BeforeAll {
2839

2940
if ($event.SentryExceptions[1].Type -eq 'Write-Error')
3041
{
31-
$checkFrame.Invoke((GetListItem $frames -1), 'funcB', 46)
42+
$checkFrame.Invoke((GetListItem $frames -1), 'funcB', 18)
3243
$event.SentryExceptions[0].Type | Should -Be 'Microsoft.PowerShell.Commands.WriteErrorException'
3344
$event.SentryExceptions[0].Module | Should -Match 'Microsoft.PowerShell.Commands.Utility'
3445
}
3546
else
3647
{
3748
if ($event.SentryExceptions[1].Type -eq 'error')
3849
{
39-
$checkFrame.Invoke((GetListItem $frames -1), 'funcB', 45)
50+
$checkFrame.Invoke((GetListItem $frames -1), 'funcB', 17)
4051
$(GetListItem $frames -1).ColumnNumber | Should -BeGreaterThan 0
4152
}
4253
else
4354
{
44-
$checkFrame.Invoke((GetListItem $frames -1), 'funcB', 52)
55+
$checkFrame.Invoke((GetListItem $frames -1), 'funcB', 24)
4556
}
4657
$event.SentryExceptions[0].Type | Should -Be 'System.Management.Automation.RuntimeException'
4758
$event.SentryExceptions[0].Module | Should -Match 'System.Management.Automation'
4859
}
4960

50-
$checkFrame.Invoke((GetListItem $frames -2), 'funcA', 35)
61+
$checkFrame.Invoke((GetListItem $frames -2), 'funcA', 7)
5162

5263
$event.SentryExceptions[0].Value | Should -Be 'error'
5364
if ($event.SentryExceptions[1].Type -eq 'error,funcB')
@@ -88,8 +99,8 @@ Describe 'Out-Sentry' {
8899
$event.SentryThreads.Count | Should -Be 2
89100
[Sentry.SentryStackFrame[]] $frames = $event.SentryThreads[0].Stacktrace.Frames
90101
$frames.Count | Should -BeGreaterThan 0
91-
$checkFrame.Invoke((GetListItem $frames -1), 'funcB', 47)
92-
$checkFrame.Invoke((GetListItem $frames -2), 'funcA', 35)
102+
$checkFrame.Invoke((GetListItem $frames -1), 'funcB', 19)
103+
$checkFrame.Invoke((GetListItem $frames -2), 'funcA', 7)
93104

94105
# A module-based frame should be in-app=false
95106
$frames | Where-Object -Property Module | Select-Object -First 1 -ExpandProperty 'InApp' | Should -Be $false

tests/throwing.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This file is used in stacktrace tests.
2+
# Changes here may require changes in tests/stacktrace.test.ps1
3+
# Especially in the contexts-lines checks.
4+
5+
function funcA($action, $param)
6+
{
7+
funcB $action $param
8+
}
9+
10+
function funcB
11+
{
12+
[CmdletBinding()]
13+
param([string]$action, [string] $value)
14+
15+
switch ($action)
16+
{
17+
'throw' { throw $value }
18+
'write' { Write-Error $value -ErrorAction Stop }
19+
'pass' { $value | Out-Sentry }
20+
'pipeline'
21+
{
22+
try
23+
{
24+
throw $value
25+
}
26+
catch
27+
{
28+
[System.Management.Automation.ErrorRecord]$ErrorRecord = $_
29+
$PSCmdlet.ThrowTerminatingError($ErrorRecord)
30+
}
31+
}
32+
}
33+
}

tests/utils.ps1

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,46 +30,6 @@ class TestIntegration : Sentry.Integrations.ISdkIntegration
3030
}
3131
}
3232

33-
function funcA($action, $param)
34-
{
35-
funcB $action $param
36-
}
37-
38-
function funcB
39-
{
40-
[CmdletBinding()]
41-
param([string]$action, [string] $value)
42-
43-
switch ($action)
44-
{
45-
'throw' { throw $value }
46-
'write' { Write-Error $value -ErrorAction Stop }
47-
'pass' { $value | Out-Sentry }
48-
'pipeline'
49-
{
50-
try
51-
{
52-
throw $value
53-
}
54-
catch
55-
{
56-
[System.Management.Automation.ErrorRecord]$ErrorRecord = $_
57-
$PSCmdlet.ThrowTerminatingError($ErrorRecord)
58-
}
59-
}
60-
}
61-
}
62-
63-
function ContextLines($start, $lines, $path = $null)
64-
{
65-
if ($null -eq $path)
66-
{
67-
$path = $PSCommandPath
68-
}
69-
70-
Get-Content $path | Select-Object -Skip ($start - 1) -First $lines
71-
}
72-
7333
function StartSentryForEventTests([ref] $events, [ref] $transport)
7434
{
7535
Start-Sentry {

0 commit comments

Comments
 (0)