-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathsources_test.go
More file actions
216 lines (178 loc) · 4.93 KB
/
sources_test.go
File metadata and controls
216 lines (178 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package oops
import (
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestReadFileWithCache(t *testing.T) {
t.Parallel()
_, currentFile, _, _ := runtime.Caller(0)
t.Run("CacheMiss", func(t *testing.T) { //nolint:paralleltest
// Ensure a clean cache before the test
mutex.Lock()
delete(cache, currentFile)
mutex.Unlock()
t.Cleanup(func() {
mutex.Lock()
cache = map[string][]string{}
mutex.Unlock()
})
lines, ok := readFileWithCache(currentFile)
assert.True(t, ok)
assert.NotNil(t, lines)
assert.NotEmpty(t, lines)
})
t.Run("CacheHit", func(t *testing.T) { //nolint:paralleltest
// Pre-populate cache
mutex.Lock()
cache[currentFile] = []string{"line1", "line2"}
mutex.Unlock()
t.Cleanup(func() {
mutex.Lock()
cache = map[string][]string{}
mutex.Unlock()
})
lines, ok := readFileWithCache(currentFile)
assert.True(t, ok)
assert.Equal(t, []string{"line1", "line2"}, lines)
})
t.Run("NonGoFile", func(t *testing.T) {
t.Parallel()
lines, ok := readFileWithCache("/some/path/file.txt")
assert.False(t, ok)
assert.Nil(t, lines)
})
t.Run("MissingFile", func(t *testing.T) {
t.Parallel()
lines, ok := readFileWithCache("/nonexistent/path/file.go")
assert.False(t, ok)
assert.Nil(t, lines)
})
t.Run("ContentCorrectness", func(t *testing.T) { //nolint:paralleltest
// Ensure a clean cache for this file path
mutex.Lock()
delete(cache, currentFile)
mutex.Unlock()
t.Cleanup(func() {
mutex.Lock()
cache = map[string][]string{}
mutex.Unlock()
})
lines, ok := readFileWithCache(currentFile)
assert.True(t, ok)
assert.NotNil(t, lines)
// The test file itself must contain "package oops" on the first line
assert.Equal(t, "package oops", lines[0])
// The file should contain recognizable content from itself
found := false
for _, line := range lines {
if strings.Contains(line, "TestReadFileWithCache") {
found = true
break
}
}
assert.True(t, found, "expected to find 'TestReadFileWithCache' in file lines")
})
}
func TestGetSourceFromFrame(t *testing.T) {
t.Parallel()
_, currentFile, currentLine, _ := runtime.Caller(0)
t.Run("ValidFrame", func(t *testing.T) { //nolint:paralleltest
t.Cleanup(func() {
mutex.Lock()
cache = map[string][]string{}
mutex.Unlock()
})
frame := oopsStacktraceFrame{
file: currentFile,
line: currentLine,
function: "TestGetSourceFromFrame",
pc: 0,
}
result := getSourceFromFrame(frame)
assert.NotEmpty(t, result)
// Should contain the line number in the output
found := false
for _, s := range result {
if strings.Contains(s, "runtime.Caller") {
found = true
break
}
}
assert.True(t, found, "expected source context to include the line with runtime.Caller")
})
t.Run("OutOfBoundsLine", func(t *testing.T) { //nolint:paralleltest
t.Cleanup(func() {
mutex.Lock()
cache = map[string][]string{}
mutex.Unlock()
})
frame := oopsStacktraceFrame{
file: currentFile,
line: 999999,
function: "TestGetSourceFromFrame",
pc: 0,
}
result := getSourceFromFrame(frame)
assert.Empty(t, result)
})
t.Run("MissingFile", func(t *testing.T) {
t.Parallel()
frame := oopsStacktraceFrame{
file: "/nonexistent/path/file.go",
line: 1,
function: "TestGetSourceFromFrame",
pc: 0,
}
result := getSourceFromFrame(frame)
assert.Empty(t, result)
})
t.Run("TabCharacterHandling", func(t *testing.T) { //nolint:paralleltest
// Inject a fake file with a tab-indented line into the cache
fakeFile := "/fake/tab_test_file.go"
fakeLines := []string{
"package fake",
"\t\tfunc tabIndented() {",
"\t\t\treturn",
"\t\t}",
"",
}
mutex.Lock()
cache[fakeFile] = fakeLines
mutex.Unlock()
t.Cleanup(func() {
mutex.Lock()
cache = map[string][]string{}
mutex.Unlock()
})
// Point at line 2: "\t\tfunc tabIndented() {"
frame := oopsStacktraceFrame{
file: fakeFile,
line: 2,
function: "tabIndented",
pc: 0,
}
result := getSourceFromFrame(frame)
assert.NotEmpty(t, result)
// Find the caret line (it starts with a tab)
var caretLine string
for _, s := range result {
if strings.HasPrefix(s, "\t") && strings.Contains(s, "^") {
caretLine = s
break
}
}
assert.NotEmpty(t, caretLine, "expected a caret indicator line")
// 2 tabs => 2 * (8-1) = 14 extra spaces beyond the tab count itself
// The caret prefix should account for the expanded tab width
// prefix = repeat(" ", lenLeadingSpaces + (8-1)*nbrTabs)
// lenLeadingSpaces = 2 (two tab chars), nbrTabs = 2
// firstCharIndex = 2 + 14 = 16
// So the caret line (after the leading \t) should have 16 spaces before ^
caretContent := strings.TrimPrefix(caretLine, "\t")
assert.True(t, strings.HasPrefix(caretContent, strings.Repeat(" ", 16)),
"expected 16 leading spaces in caret line, got: %q", caretContent)
assert.Contains(t, caretContent, "^")
})
}