@@ -3,9 +3,6 @@ package stack
3
3
import (
4
4
"runtime"
5
5
"testing"
6
- "github.com/dkushner/stack"
7
- "strings"
8
- "strconv"
9
6
)
10
7
11
8
func TestCaller (t * testing.T ) {
@@ -27,6 +24,49 @@ func TestCaller(t *testing.T) {
27
24
}
28
25
}
29
26
27
+ func TestCallerPanic (t * testing.T ) {
28
+ t .Parallel ()
29
+
30
+ var (
31
+ line int
32
+ ok bool
33
+ )
34
+
35
+ defer func () {
36
+ if recover () != nil {
37
+ // count frames to runtime.sigpanic
38
+ panicIdx := - 1
39
+ for i , c := range Trace () {
40
+ if c .name () == "runtime.sigpanic" {
41
+ panicIdx = i
42
+ break
43
+ }
44
+ }
45
+ if panicIdx == - 1 {
46
+ t .Fatal ("no runtime.sigpanic entry on the stack" )
47
+ }
48
+ if got , want := Caller (panicIdx ).name (), "runtime.sigpanic" ; got != want {
49
+ t .Errorf ("sigpanic frame: got name == %v, want name == %v" , got , want )
50
+ }
51
+ if got , want := Caller (panicIdx + 1 ).name (), "github.com/go-stack/stack.TestCallerPanic" ; got != want {
52
+ t .Errorf ("TestCallerPanic frame: got name == %v, want name == %v" , got , want )
53
+ }
54
+ if got , want := Caller (panicIdx + 1 ).line (), line ; got != want {
55
+ t .Errorf ("TestCallerPanic frame: got line == %v, want line == %v" , got , want )
56
+ }
57
+ }
58
+ }()
59
+
60
+ _ , _ , line , ok = runtime .Caller (0 )
61
+ line += 7 // adjust to match line of panic below
62
+ if ! ok {
63
+ t .Fatal ("runtime.Caller(0) failed" )
64
+ }
65
+ // Initiate a sigpanic.
66
+ var x * uintptr
67
+ _ = * x
68
+ }
69
+
30
70
type fholder struct {
31
71
f func () CallStack
32
72
}
@@ -35,12 +75,16 @@ func (fh *fholder) labyrinth() CallStack {
35
75
for {
36
76
return fh .f ()
37
77
}
38
- panic ("this line only needed for go 1.0" )
39
78
}
40
79
41
80
func TestTrace (t * testing.T ) {
42
81
t .Parallel ()
43
82
83
+ _ , _ , line , ok := runtime .Caller (0 )
84
+ if ! ok {
85
+ t .Fatal ("runtime.Caller(0) failed" )
86
+ }
87
+
44
88
fh := fholder {
45
89
f : func () CallStack {
46
90
cs := Trace ()
@@ -50,7 +94,7 @@ func TestTrace(t *testing.T) {
50
94
51
95
cs := fh .labyrinth ()
52
96
53
- lines := []int {46 , 36 , 51 }
97
+ lines := []int {line + 7 , line - 7 , line + 12 }
54
98
55
99
for i , line := range lines {
56
100
if got , want := cs [i ].line (), line ; got != want {
@@ -63,39 +107,43 @@ func TestTrace(t *testing.T) {
63
107
func TestTracePanic (t * testing.T ) {
64
108
t .Parallel ()
65
109
110
+ var (
111
+ line int
112
+ ok bool
113
+ )
114
+
66
115
defer func () {
67
116
if recover () != nil {
68
- trace := stack .Trace ().TrimRuntime ()
69
-
70
- if len (trace ) != 6 {
71
- t .Errorf ("got len(trace) == %v, want %v" , len (trace ), 6 )
72
- }
73
-
74
- // Check frames in this file, the interceding frames are somewhat
75
- // platform-dependent.
76
- lines := []int64 {68 , 101 }
77
-
78
- var local []int64
79
- for _ , call := range trace {
80
- parts := strings .Split (call .String (), ":" )
81
- if parts [0 ] == "stackinternal_test.go" {
82
- line , _ := strconv .ParseInt (parts [1 ], 10 , 32 )
83
- local = append (local , line )
117
+ trace := Trace ()
118
+
119
+ // find runtime.sigpanic
120
+ panicIdx := - 1
121
+ for i , c := range trace {
122
+ if c .name () == "runtime.sigpanic" {
123
+ panicIdx = i
124
+ break
84
125
}
85
126
}
86
-
87
- if len (local ) != 2 {
88
- t .Errorf ("expected %v local frames but got %v" , 2 , len (local ))
127
+ if panicIdx == - 1 {
128
+ t .Fatal ("no runtime.sigpanic entry on the stack" )
89
129
}
90
-
91
- for i , line := range lines {
92
- if got , want := local [i ], line ; got != want {
93
- t .Errorf ("got line[%d] == %v, want line[%d] == %v" , i , got , i , want )
94
- }
130
+ if got , want := trace [panicIdx ].name (), "runtime.sigpanic" ; got != want {
131
+ t .Errorf ("sigpanic frame: got name == %v, want name == %v" , got , want )
132
+ }
133
+ if got , want := trace [panicIdx + 1 ].name (), "github.com/go-stack/stack.TestTracePanic" ; got != want {
134
+ t .Errorf ("TestTracePanic frame: got name == %v, want name == %v" , got , want )
135
+ }
136
+ if got , want := trace [panicIdx + 1 ].line (), line ; got != want {
137
+ t .Errorf ("TestTracePanic frame: got line == %v, want line == %v" , got , want )
95
138
}
96
139
}
97
140
}()
98
141
142
+ _ , _ , line , ok = runtime .Caller (0 )
143
+ line += 7 // adjust to match line of panic below
144
+ if ! ok {
145
+ t .Fatal ("runtime.Caller(0) failed" )
146
+ }
99
147
// Initiate a sigpanic.
100
148
var x * uintptr
101
149
_ = * x
0 commit comments