forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
490 lines (461 loc) · 10.5 KB
/
error_test.go
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package actionlint
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"math"
"sort"
"strconv"
"strings"
"testing"
"github.com/fatih/color"
"github.com/google/go-cmp/cmp"
)
func init() {
color.NoColor = true
}
type testErrorWriter struct{}
func (w testErrorWriter) Write(b []byte) (int, error) {
return 0, errors.New("dummy write error")
}
func TestErrorErrorAt(t *testing.T) {
m := "message"
k := "kind"
err := errorAt(&Pos{1, 2}, k, m)
if err.Message != m {
t.Errorf("wanted %q but got %q", m, err.Message)
}
if err.Filepath != "" {
t.Errorf("wanted %q to be empty", err.Filepath)
}
if err.Line != 1 {
t.Errorf("wanted line 1 but got %q", err.Line)
}
if err.Column != 2 {
t.Errorf("wanted col 2 but got %q", err.Column)
}
if err.Kind != k {
t.Errorf("wanted %q but got %q", k, err.Kind)
}
}
func TestErrorErrorfAt(t *testing.T) {
m := "this is message"
k := "kind"
err := errorfAt(&Pos{1, 2}, k, "%s is %s", "this", "message")
if err.Message != m {
t.Errorf("wanted %q but got %q", m, err.Message)
}
if err.Filepath != "" {
t.Errorf("wanted %q to be empty", err.Filepath)
}
if err.Line != 1 {
t.Errorf("wanted line 1 but got %q", err.Line)
}
if err.Column != 2 {
t.Errorf("wanted col 2 but got %q", err.Column)
}
if err.Kind != k {
t.Errorf("wanted %q but got %q", k, err.Kind)
}
}
func TestErrorPrettyPrint(t *testing.T) {
testCases := []struct {
message string
line int
column int
kind string
expected string
source string
}{
{
message: "simple message",
line: 1,
column: 1,
expected: "filename.txt:1:1: simple message [kind]",
},
{
message: "simple message with source",
line: 1,
column: 1,
source: "this is source",
expected: `filename.txt:1:1: simple message with source [kind]
|
1 | this is source
| ^~~~`,
},
{
message: "error at middle of source",
line: 1,
column: 6,
source: "this is source",
expected: `filename.txt:1:6: error at middle of source [kind]
|
1 | this is source
| ^~`,
},
{
message: "error at end of source",
line: 1,
column: 15,
source: "this is source",
expected: `filename.txt:1:15: error at end of source [kind]
|
1 | this is source
| ^`,
},
{
message: "error at one character word in source",
line: 1,
column: 5,
source: "foo . bar",
expected: `filename.txt:1:5: error at one character word in source [kind]
|
1 | foo . bar
| ^`,
},
{
message: "error at space in source",
line: 1,
column: 4,
source: "foo bar",
expected: `filename.txt:1:4: error at space in source [kind]
|
1 | foo bar
| ^`,
},
{
message: "simple message with multi-line source",
line: 3,
column: 3,
source: "this\nis\nsource",
expected: `filename.txt:3:3: simple message with multi-line source [kind]
|
3 | source
| ^~~~`,
},
{
message: "error at end of multi-line source",
line: 3,
column: 7,
source: "this\nis\nsource",
expected: `filename.txt:3:7: error at end of multi-line source [kind]
|
3 | source
| ^`,
},
{
message: "error at newline of multi-line source",
line: 2,
column: 3,
source: "this\nis\nsource",
expected: `filename.txt:2:3: error at newline of multi-line source [kind]
|
2 | is
| ^`,
},
{
message: "error at blank line of multi-line source",
line: 2,
column: 1,
source: "this\n\nsource",
expected: `filename.txt:2:1: error at blank line of multi-line source [kind]
|
2 |
| ^`,
},
{
message: "error at line more than 10",
line: 11,
column: 2,
source: "\n\n\n\n\n\n\n\n\n\nfooo",
expected: `filename.txt:11:2: error at line more than 10 [kind]
|
11 | fooo
| ^~~`,
},
{
message: "error at out of source",
line: 2,
column: 6,
source: "aaa\nbbb\nccc",
expected: "filename.txt:2:6: error at out of source [kind]",
},
{
message: "error at zero column",
line: 1,
column: 0,
source: "this is source",
expected: `filename.txt:1:0: error at zero column [kind]
|
1 | this is source
| `,
},
{
message: "error at zero line and zero column",
line: 0,
column: 0,
source: "this is source",
expected: `filename.txt:0:0: error at zero line and zero column [kind]`,
},
}
for _, tc := range testCases {
t.Run(tc.message, func(t *testing.T) {
err := errorAt(&Pos{tc.line, tc.column}, "kind", tc.message)
err.Filepath = "filename.txt"
var buf bytes.Buffer
err.PrettyPrint(&buf, []byte(tc.source))
out := buf.String()
want := tc.expected + "\n"
if out != want {
t.Fatalf("wanted:\n%q\n\nhave:\n%q", want, out)
}
})
}
}
func TestErrorSortByErrorPosition(t *testing.T) {
testCases := [][]struct {
line int
col int
}{
{},
{
{1, 2},
},
{
{1, 2},
{4, 1},
{3, 20},
{1, 1},
},
{
{1, 1},
{1, 1},
{1, 1},
},
}
for i, tc := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
errs := make([]*Error, 0, len(tc))
for _, p := range tc {
errs = append(errs, &Error{Line: p.line, Column: p.col})
}
sort.Sort(ByErrorPosition(errs))
for i := 0; i < len(errs)-1; i++ {
l, r := errs[i], errs[i+1]
sorted := l.Line <= r.Line
if l.Line == r.Line {
sorted = l.Column <= r.Column
}
if !sorted {
t.Fatalf("errs[%d] and errs[%d] are not sorted: %s", i, i+1, errs)
}
}
})
}
}
func TestErrorGetTemplateFields(t *testing.T) {
testCases := []struct {
message string
column int
source string
snippet string
}{
{
message: "simple message with source",
column: 1,
source: "this is source",
snippet: "this is source\n^~~~",
},
{
message: "simple message",
column: 1,
snippet: "",
},
{
message: "error at zero column",
column: 0,
source: "this is source",
snippet: "this is source",
},
}
for i, tc := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
err := errorAt(&Pos{1, tc.column}, "kind", tc.message)
err.Filepath = "filename.txt"
f := err.GetTemplateFields([]byte(tc.source))
if f.Message != tc.message {
t.Fatalf("wanted %q but have %q", tc.message, f.Message)
}
if f.Line != err.Line {
t.Fatalf("wanted %d but have %d", err.Line, f.Line)
}
if f.Column != tc.column {
t.Fatalf("wanted %d but have %d", tc.column, f.Column)
}
if f.Filepath != err.Filepath {
t.Fatalf("wanted %q but have %q", err.Filepath, f.Filepath)
}
if f.Kind != err.Kind {
t.Fatalf("wanted %q but have %q", err.Kind, f.Kind)
}
if f.Snippet != tc.snippet {
t.Fatalf("wanted %q but have %q", tc.snippet, f.Snippet)
}
})
}
}
var testErrorTemplateFields = []*ErrorTemplateFields{
{
Message: "message 1",
Filepath: "file1",
Line: 1,
Column: 2,
Snippet: "snippet 1",
Kind: "kind1",
},
{
Message: "message 2",
Filepath: "file2",
Line: 3,
Column: 4,
Snippet: "snippet 2",
Kind: "kind2",
},
}
func TestErrorPrintFormattedWithTemplateFields(t *testing.T) {
testCases := []struct {
temp string
want string
}{
{
temp: "{{(index . 0).Message}} {{(index . 1).Message}}",
want: "message 1 message 2",
},
{
temp: "{{range $ = .}}({{$.Line}}, {{$.Column}}){{end}}",
want: "(1, 2)(3, 4)",
},
{
temp: "{{range $ = .}}{{json $.Snippet}}{{end}}",
want: "\"snippet 1\"\n\"snippet 2\"\n",
},
{
temp: "{{range $ = .}}{{replace $.Kind \"kind\" \"king\"}}{{end}}",
want: "king1king2",
},
}
for _, tc := range testCases {
t.Run(tc.temp, func(t *testing.T) {
f, err := NewErrorFormatter(tc.temp)
if err != nil {
t.Fatal(err)
}
var b strings.Builder
if err := f.Print(&b, testErrorTemplateFields); err != nil {
t.Fatal(err)
}
have := b.String()
if tc.want != have {
t.Fatalf("wanted %q but have %q", tc.want, have)
}
})
}
}
func TestErrorPrintFormattedErrors(t *testing.T) {
errs := []*Error{
errorAt(&Pos{1, 1}, "kind1", "error1"),
errorAt(&Pos{1, 0}, "kind2", "error2"),
}
f, err := NewErrorFormatter("{{range $ = .}}({{$.Message | printf \"%q\"}},{{$.Snippet | printf \"%q\"}}){{end}}")
if err != nil {
t.Fatal(err)
}
var b strings.Builder
if err := f.PrintErrors(&b, errs, []byte("this is source")); err != nil {
t.Fatal(err)
}
have := b.String()
want := `("error1","this is source\n^~~~")("error2","this is source")`
if want != have {
t.Fatalf("wanted %q but have %q", want, have)
}
}
func TestErrorPrintSerializedIntoJSON(t *testing.T) {
f, err := NewErrorFormatter("{{json .}}")
if err != nil {
t.Fatal(err)
}
var b bytes.Buffer
if err := f.Print(&b, testErrorTemplateFields); err != nil {
t.Fatal(err)
}
decoded := []*ErrorTemplateFields{}
if err := json.Unmarshal(b.Bytes(), &decoded); err != nil {
t.Fatal(err)
}
if !cmp.Equal(testErrorTemplateFields, decoded) {
t.Fatal(cmp.Diff(testErrorTemplateFields, decoded))
}
}
func TestErrorNewErrorFormatterError(t *testing.T) {
testCases := []struct {
temp string
want string
}{
{"hello", "template to format error messages must contain at least one {{ }} placeholder"},
{"{{xxx", "template \"{{xxx\" to format error messages could not be parsed"},
}
for _, tc := range testCases {
t.Run(tc.temp, func(t *testing.T) {
_, err := NewErrorFormatter(tc.temp)
if err == nil {
t.Fatal("error did not occur")
}
if !strings.Contains(err.Error(), tc.want) {
t.Fatalf("%q is not contained in error message %q", tc.want, err.Error())
}
})
}
}
func TestErrorFormatterPrintError(t *testing.T) {
testCases := []struct {
out io.Writer
temp string
want string
}{
{ioutil.Discard, "{{.Foo}}", "can't evaluate field Foo in type"},
{testErrorWriter{}, "{{(index . 0).Message}}", "dummy write error"},
}
for _, tc := range testCases {
t.Run(tc.temp, func(t *testing.T) {
f, err := NewErrorFormatter(tc.temp)
if err != nil {
t.Fatal(err)
}
err = f.Print(tc.out, testErrorTemplateFields)
if err == nil {
t.Fatal("error did not occur")
}
if !strings.Contains(err.Error(), tc.want) {
t.Fatalf("%q is not contained in error message %q", tc.want, err.Error())
}
})
}
}
func TestErrorFormatterPrintJSONEncodeError(t *testing.T) {
f, err := NewErrorFormatter("{{json .}}")
if err != nil {
t.Fatal(err)
}
var b strings.Builder
err = f.temp.Execute(&b, math.NaN())
if err == nil {
t.Fatal("error did not occur", b.String())
}
want := "could not encode template value into JSON"
if !strings.Contains(err.Error(), want) {
t.Fatalf("%q is not contained in error message %q", want, err.Error())
}
}