Debian Codegraph @

stacktrace_test.go

raw
1 package raven
2
3 import (
4 "fmt"
5 "go/build"
6 "io/ioutil"
7 "os"
8 "path/filepath"
9 "runtime"
10 "strings"
11 "testing"
12 )
13
14 func init() {
15 thisFile, thisPackage = derivePackage()
16 functionNameTests = []FunctionNameTest{
17 {0, thisPackage, "TestFunctionName"},
18 {1, "testing", "tRunner"},
19 {2, "runtime", "goexit"},
20 {100, "", ""},
21 }
22 }
23
24 type FunctionNameTest struct {
25 skip int
26 pack string
27 name string
28 }
29
30 var (
31 thisFile string
32 thisPackage string
33 functionNameTests []FunctionNameTest
34 )
35
36 func TestFunctionName(t *testing.T) {
37 for _, test := range functionNameTests {
38 pc, _, _, _ := runtime.Caller(test.skip)
39 pack, name := functionName(runtime.FuncForPC(pc).Name())
40
41 if pack != test.pack {
42 t.Errorf("incorrect package; got %s, want %s", pack, test.pack)
43 }
44 if name != test.name {
45 t.Errorf("incorrect function; got %s, want %s", name, test.name)
46 }
47 }
48 }
49
50 func TestStacktrace(t *testing.T) {
51 st := trace()
52 if st == nil {
53 t.Error("got nil stacktrace")
54 }
55 if len(st.Frames) == 0 {
56 t.Error("got zero frames")
57 }
58
59 f := st.Frames[len(st.Frames)-1]
60 if f.Filename != thisFile {
61 t.Errorf("incorrect Filename; got %s, want %s", f.Filename, thisFile)
62 }
63 if !strings.HasSuffix(f.AbsolutePath, thisFile) {
64 t.Error("incorrect AbsolutePath:", f.AbsolutePath)
65 }
66 if f.Function != "trace" {
67 t.Error("incorrect Function:", f.Function)
68 }
69 if f.Module != thisPackage {
70 t.Error("incorrect Module:", f.Module)
71 }
72 if f.Lineno != 97 {
73 t.Error("incorrect Lineno:", f.Lineno)
74 }
75 if f.ContextLine != "\treturn NewStacktrace(0, 2, []string{thisPackage})" {
76 t.Errorf("incorrect ContextLine: %#v", f.ContextLine)
77 }
78 if len(f.PreContext) != 2 || f.PreContext[0] != "// a" || f.PreContext[1] != "func trace() *Stacktrace {" {
79 t.Errorf("incorrect PreContext %#v", f.PreContext)
80 }
81 if len(f.PostContext) != 2 || f.PostContext[0] != "\t// b" || f.PostContext[1] != "}" {
82 t.Errorf("incorrect PostContext %#v", f.PostContext)
83 }
84 _, filename, _, _ := runtime.Caller(0)
85 runningInVendored := strings.Contains(filename, "vendor")
86 if f.InApp != !runningInVendored {
87 t.Error("expected InApp to be true")
88 }
89
90 if f.InApp && st.Culprit() != fmt.Sprintf("%s.trace", thisPackage) {
91 t.Error("incorrect Culprit:", st.Culprit())
92 }
93 }
94
95 // a
96 func trace() *Stacktrace {
97 return NewStacktrace(0, 2, []string{thisPackage})
98 // b
99 }
100
101 func derivePackage() (file, pack string) {
102 // Get file name by seeking caller's file name.
103 _, callerFile, _, ok := runtime.Caller(1)
104 if !ok {
105 return
106 }
107
108 // Trim file name
109 file = callerFile
110 for _, dir := range build.Default.SrcDirs() {
111 dir := dir + string(filepath.Separator)
112 if trimmed := strings.TrimPrefix(callerFile, dir); len(trimmed) < len(file) {
113 file = trimmed
114 }
115 }
116
117 // Now derive package name
118 dir := filepath.Dir(callerFile)
119
120 dirPkg, err := build.ImportDir(dir, build.AllowBinary)
121 if err != nil {
122 return
123 }
124
125 pack = dirPkg.ImportPath
126 return
127 }
128
129 // TestNewStacktrace_outOfBounds verifies that a context exceeding the number
130 // of lines in a file does not cause a panic.
131 func TestNewStacktrace_outOfBounds(t *testing.T) {
132 st := NewStacktrace(0, 1000000, []string{thisPackage})
133 f := st.Frames[len(st.Frames)-1]
134 if f.ContextLine != "\tst := NewStacktrace(0, 1000000, []string{thisPackage})" {
135 t.Errorf("incorrect ContextLine: %#v", f.ContextLine)
136 }
137 }
138
139 func TestNewStacktrace_noFrames(t *testing.T) {
140 st := NewStacktrace(999999999, 0, []string{})
141 if st != nil {
142 t.Errorf("expected st.Frames to be nil: %v", st)
143 }
144 }
145
146 func TestFileContext(t *testing.T) {
147 // reset the cache
148 loader := &fsLoader{cache: make(map[string][][]byte)}
149
150 tempdir, err := ioutil.TempDir("", "")
151 if err != nil {
152 t.Fatal("failed to create temporary directory:", err)
153 }
154 defer os.RemoveAll(tempdir)
155
156 okPath := filepath.Join(tempdir, "ok")
157 missingPath := filepath.Join(tempdir, "missing")
158 noPermissionPath := filepath.Join(tempdir, "noperms")
159
160 err = ioutil.WriteFile(okPath, []byte("hello\nworld\n"), 0600)
161 if err != nil {
162 t.Fatal("failed writing file:", err)
163 }
164 err = ioutil.WriteFile(noPermissionPath, []byte("no access\n"), 0000)
165 if err != nil {
166 t.Fatal("failed writing file:", err)
167 }
168
169 tests := []struct {
170 path string
171 expectedLines int
172 expectedIndex int
173 }{
174 {okPath, 1, 0},
175 {missingPath, 0, 0},
176 {noPermissionPath, 0, 0},
177 }
178 for i, test := range tests {
179 lines, index := loader.Load(test.path, 1, 0)
180 if !(len(lines) == test.expectedLines && index == test.expectedIndex) {
181 t.Errorf("%d: fileContext(%#v, 1, 0) = %v, %v; expected len()=%d, %d",
182 i, test.path, lines, index, test.expectedLines, test.expectedIndex)
183 }
184 cacheLen := len(loader.cache)
185 if cacheLen != i+1 {
186 t.Errorf("%d: result was not cached; len=%d", i, cacheLen)
187 }
188 }
189 }