-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaths_test.go
More file actions
328 lines (320 loc) · 6.96 KB
/
paths_test.go
File metadata and controls
328 lines (320 loc) · 6.96 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"slices"
"strings"
"testing"
"gotest.tools/v3/assert"
)
func TestScanLines(t *testing.T) {
tests := []struct {
name string
input string
want []string
}{
{
name: "multiple lines",
input: "hello\nworld\n",
want: []string{"hello", "world"},
},
{
name: "no trailing newline",
input: "hello\nworld",
want: []string{"hello", "world"},
},
{
name: "single line",
input: "hello\n",
want: []string{"hello"},
},
{
name: "empty input",
input: "",
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := slices.Collect(scanLines(strings.NewReader(tt.input)))
assert.DeepEqual(t, got, tt.want)
})
}
}
func TestUnwrap(t *testing.T) {
tests := []struct {
name string
lines []string
width int
want []string
}{
{
name: "no wrapping, all lines shorter than width",
lines: []string{"hello", "world"},
width: 10,
want: []string{"hello", "world"},
},
{
name: "single wrapped line",
lines: []string{"error in /very/long/path/to/some/deep/di", "r/file.go"},
width: 40,
want: []string{"error in /very/long/path/to/some/deep/dir/file.go"},
},
{
name: "multiple consecutive wraps",
lines: []string{"aaaaaaaaaa", "bbbbbbbbbb", "ccc"},
width: 10,
want: []string{"aaaaaaaaaabbbbbbbbbbccc"},
},
{
name: "last line is exactly width",
lines: []string{"short", "aaaaaaaaaa"},
width: 10,
want: []string{"short", "aaaaaaaaaa"},
},
{
name: "empty input",
lines: nil,
width: 80,
want: nil,
},
{
name: "width zero is pass-through",
lines: []string{"hello", "world"},
width: 0,
want: []string{"hello", "world"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := slices.Collect(unwrap(slices.Values(tt.lines), tt.width))
assert.DeepEqual(t, got, tt.want)
})
}
}
func TestFindPaths(t *testing.T) {
tests := []struct {
name string
input []string
want []Span
}{
{
name: "simple relative path",
input: []string{"error in ./src/main.go"},
want: []Span{
{Line: 0, Start: 9, End: 22, Text: "./src/main.go"},
},
},
{
name: "absolute path",
input: []string{"see /tmp/log.txt"},
want: []Span{
{Line: 0, Start: 4, End: 16, Text: "/tmp/log.txt"},
},
},
{
name: "path with line and column",
input: []string{"./src/main.go:42:10 something"},
want: []Span{
{Line: 0, Start: 0, End: 19, Text: "./src/main.go"},
},
},
{
name: "path with line only",
input: []string{"./src/main.go:42"},
want: []Span{
{Line: 0, Start: 0, End: 16, Text: "./src/main.go"},
},
},
{
name: "quoted path",
input: []string{`open "src/main.go" failed`},
want: []Span{
{Line: 0, Start: 6, End: 17, Text: "src/main.go"},
},
},
{
name: "parenthesized path",
input: []string{"(src/main.go)"},
want: []Span{
{Line: 0, Start: 1, End: 12, Text: "src/main.go"},
},
},
{
name: "bracketed path",
input: []string{"[src/main.go]"},
want: []Span{
{Line: 0, Start: 1, End: 12, Text: "src/main.go"},
},
},
{
name: "angle bracketed path",
input: []string{"<src/main.go>"},
want: []Span{
{Line: 0, Start: 1, End: 12, Text: "src/main.go"},
},
},
{
name: "trailing comma",
input: []string{"src/main.go, src/util.go"},
want: []Span{
{Line: 0, Start: 0, End: 11, Text: "src/main.go"},
{Line: 0, Start: 13, End: 24, Text: "src/util.go"},
},
},
{
name: "trailing semicolon",
input: []string{"src/main.go;"},
want: []Span{
{Line: 0, Start: 0, End: 11, Text: "src/main.go"},
},
},
{
name: "URL skipped",
input: []string{"see https://example.com/path/file.go for details"},
want: nil,
},
{
name: "git diff prefix a/",
input: []string{"diff a/src/main.go b/src/main.go"},
want: []Span{
{Line: 0, Start: 5, End: 18, Text: "src/main.go"},
{Line: 0, Start: 19, End: 32, Text: "src/main.go"},
},
},
{
name: "multiple lines",
input: []string{"error in ./src/main.go:42", "see also /tmp/log.txt"},
want: []Span{
{Line: 0, Start: 9, End: 25, Text: "./src/main.go"},
{Line: 1, Start: 9, End: 21, Text: "/tmp/log.txt"},
},
},
{
name: "no paths",
input: []string{"hello world", "no paths here"},
want: nil,
},
{
name: "bare slash ignored",
input: []string{"/"},
want: nil,
},
{
name: "empty input",
input: nil,
want: nil,
},
{
name: "empty lines",
input: []string{"", "", ""},
want: nil,
},
{
name: "bare filename with extension",
input: []string{"main.go"},
want: []Span{
{Line: 0, Start: 0, End: 7, Text: "main.go"},
},
},
{
name: "duplicate paths on same line",
input: []string{"src/main.go src/main.go"},
want: []Span{
{Line: 0, Start: 0, End: 11, Text: "src/main.go"},
{Line: 0, Start: 12, End: 23, Text: "src/main.go"},
},
},
{
name: "single quoted path",
input: []string{"'src/main.go'"},
want: []Span{
{Line: 0, Start: 1, End: 12, Text: "src/main.go"},
},
},
{
name: "nested wrapping stripped",
input: []string{`("src/main.go")`},
want: []Span{
{Line: 0, Start: 2, End: 13, Text: "src/main.go"},
},
},
{
name: "path with spaces via tabs",
input: []string{"/tmp/log.txt\t/var/data/out.csv"},
want: []Span{
{Line: 0, Start: 0, End: 12, Text: "/tmp/log.txt"},
{Line: 0, Start: 13, End: 30, Text: "/var/data/out.csv"},
},
},
{
name: "bare go.mod and go.sum",
input: []string{"go.mod go.sum"},
want: []Span{
{Line: 0, Start: 0, End: 6, Text: "go.mod"},
{Line: 0, Start: 7, End: 13, Text: "go.sum"},
},
},
{
name: "bare filename with line suffix",
input: []string{"go.mod:42"},
want: []Span{
{Line: 0, Start: 0, End: 9, Text: "go.mod"},
},
},
{
name: "dotfile",
input: []string{".gitignore"},
want: []Span{
{Line: 0, Start: 0, End: 10, Text: ".gitignore"},
},
},
{
name: "dotfile with dots",
input: []string{".env.local"},
want: []Span{
{Line: 0, Start: 0, End: 10, Text: ".env.local"},
},
},
{
name: "too-short dotfile rejected",
input: []string{".a"},
want: nil,
},
{
name: "Makefile special name",
input: []string{"Makefile"},
want: []Span{
{Line: 0, Start: 0, End: 8, Text: "Makefile"},
},
},
{
name: "Dockerfile special name",
input: []string{"Dockerfile"},
want: []Span{
{Line: 0, Start: 0, End: 10, Text: "Dockerfile"},
},
},
{
name: "not a special name",
input: []string{"Gemfilenope"},
want: nil,
},
{
name: "non-path tokens rejected",
input: []string{"error | 42 +-"},
want: nil,
},
{
name: "git diff stat bare filename",
input: []string{" go.mod | 2 +-"},
want: []Span{
{Line: 0, Start: 1, End: 7, Text: "go.mod"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := findPaths(tt.input)
assert.DeepEqual(t, got, tt.want)
})
}
}