-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.test.js
335 lines (310 loc) · 8.12 KB
/
index.test.js
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
const matcher = require(".");
const validMatcher = {
owner: "test-matcher",
severity: "Error",
pattern: [
{
regexp: "^([^:]+):([^:]+):([^:]+)$",
message: 1,
file: 2,
line: 3,
},
],
};
const eslintSingleMatcher = {
owner: "eslint-compact",
pattern: [
{
regexp:
"^(.+):\\sline\\s(\\d+),\\scol\\s(\\d+),\\s(Error|Warning|Info)\\s-\\s(.+)\\s\\((.+)\\)$",
file: 1,
line: 2,
column: 3,
severity: 4,
message: 5,
code: 6,
},
],
};
const eslintLoopMatcher = {
owner: "eslint-stylish",
pattern: [
{
// Matches the 1st line in the output
regexp: "^([^\\s].*)$",
file: 1,
},
{
// Matches the 2nd and 3rd line in the output
regexp: "^\\s+(\\d+):(\\d+)\\s+(error|warning|info)\\s+(.*)\\s\\s+(.*)$",
// File is carried through from above, so we define the rest of the groups
line: 1,
column: 2,
severity: 3,
message: 4,
code: 5,
loop: true,
},
],
};
const loopMatcherWithDefaultSeverity = {
owner: "test-matcher-loop",
severity: "error",
pattern: [
{
// Matches the 1st line in the output
regexp: "^([^\\s].*)$",
file: 1,
},
{
// Matches the 2nd and 3rd line in the output
regexp: "^\\s+(\\d+):(\\d+)\\s+\\s+(.*)\\s\\s+(.*)$",
// File is carried through from above, so we define the rest of the groups
line: 1,
column: 2,
message: 3,
code: 4,
loop: true,
},
],
};
test("throws an error if the matcher is missing", () => {
actual = () => matcher(undefined, "error::Something went wrong");
expect(actual).toThrow("No matcher provided");
});
test("throws an error if the matcher is invalid (no owner)", () => {
const m = {
pattern: [],
};
actual = () => matcher(m, "error::Something went wrong");
expect(actual).toThrow("No matcher.owner provided");
});
test("throws an error if the matcher is invalid (no pattern)", () => {
const m = {
owner: "test",
};
actual = () => matcher(m, "error::Something went wrong");
expect(actual).toThrow("No matcher.pattern provided");
});
test("throws an error if the matcher is invalid (empty pattern)", () => {
const m = {
owner: "test",
pattern: [],
};
actual = () => matcher(m, "error::Something went wrong");
expect(actual).toThrow(
"matcher.pattern must be an array with at least one value"
);
});
test("throws an error if the matcher is invalid (invalid pattern)", () => {
const m = {
owner: "test",
pattern: { invalid: true },
};
actual = () => matcher(m, "error::Something went wrong");
expect(actual).toThrow(
"matcher.pattern must be an array with at least one value"
);
});
test("throws an error if the input is missing", () => {
actual = () => matcher(validMatcher, undefined);
expect(actual).toThrow("No input provided");
});
test("single line matcher, no match", () => {
actual = matcher(eslintSingleMatcher, "this line won't match");
expect(actual).toEqual([]);
});
test("single line matcher, does match", () => {
const input =
"badFile.js: line 50, col 11, Error - 'myVar' is defined but never used. (no-unused-vars)";
actual = matcher(eslintSingleMatcher, input);
expect(actual).toEqual([
{
file: "badFile.js",
line: "50",
column: "11",
severity: "Error",
message: "'myVar' is defined but never used.",
code: "no-unused-vars",
},
]);
});
test("loop line matcher, does match", () => {
const input = `test.js
1:0 error Missing "use strict" statement strict
5:10 error 'addOne' is defined but never used no-unused-vars
foo.js
36:10 error Expected parentheses around arrow function argument arrow-parens
37:13 error Expected parentheses around arrow function argument arrow-parens
✖ 4 problems (4 errors, 0 warnings)`;
actual = matcher(eslintLoopMatcher, input);
expect(actual).toEqual([
{
file: "test.js",
line: "1",
column: "0",
severity: "error",
message: 'Missing "use strict" statement',
code: "strict",
},
{
file: "test.js",
line: "5",
column: "10",
severity: "error",
message: "'addOne' is defined but never used",
code: "no-unused-vars",
},
{
file: "foo.js",
line: "36",
column: "10",
severity: "error",
message: "Expected parentheses around arrow function argument",
code: "arrow-parens",
},
{
file: "foo.js",
line: "37",
column: "13",
severity: "error",
message: "Expected parentheses around arrow function argument",
code: "arrow-parens",
},
]);
});
test("loop line matcher with default severity, does match", () => {
const input = `test.js
1:0 Missing "use strict" statement strict
5:10 'addOne' is defined but never used no-unused-vars
foo.js
36:10 Expected parentheses around arrow function argument arrow-parens
37:13 Expected parentheses around arrow function argument arrow-parens
✖ 4 problems (4 errors, 0 warnings)`;
actual = matcher(loopMatcherWithDefaultSeverity, input);
expect(actual).toEqual([
{
file: "test.js",
line: "1",
column: "0",
severity: "error",
message: 'Missing "use strict" statement',
code: "strict",
},
{
file: "test.js",
line: "5",
column: "10",
severity: "error",
message: "'addOne' is defined but never used",
code: "no-unused-vars",
},
{
file: "foo.js",
line: "36",
column: "10",
severity: "error",
message: "Expected parentheses around arrow function argument",
code: "arrow-parens",
},
{
file: "foo.js",
line: "37",
column: "13",
severity: "error",
message: "Expected parentheses around arrow function argument",
code: "arrow-parens",
},
]);
});
test("uses default severity", () => {
const input = "Some Message:/path/to/file.js:12";
actual = matcher(validMatcher, input);
expect(actual).toEqual([
{
file: "/path/to/file.js",
line: "12",
severity: "Error",
message: "Some Message",
},
]);
});
test("uses overridden severity", () => {
const input = "Warning:Some Message:/path/to/file.js:12";
const warningMatcher = {
owner: "test-matcher",
severity: "Error",
pattern: [
{
regexp: "^([^:]+):([^:]+):([^:]+):([^:]+)$",
severity: 1,
message: 2,
file: 3,
line: 4,
},
],
};
actual = matcher(warningMatcher, input);
expect(actual).toEqual([
{
file: "/path/to/file.js",
line: "12",
severity: "Warning",
message: "Some Message",
},
]);
});
test("pattern index provided that doesn't match a capture group", () => {
const input = "Some Message:/path/to/file.js:12";
const invalidOffsetMatcher = {
owner: "test-matcher",
pattern: [
{
regexp: "^([^:]+):([^:]+):([^:]+)$",
message: 1,
file: 2,
line: 3,
code: 4,
},
],
};
actual = () => matcher(invalidOffsetMatcher, input);
expect(actual).toThrow(
"Invalid capture group provided. Group 4 (code) does not exist in regexp"
);
});
test("pattern index provided that matches group 0", () => {
const input = "Some Message:/path/to/file.js:12";
const invalidOffsetMatcher = {
owner: "test-matcher",
pattern: [
{
regexp: "^([^:]+):([^:]+):([^:]+)$",
message: 0,
},
],
};
actual = () => matcher(invalidOffsetMatcher, input);
expect(actual).toThrow(
`Group 0 is not a valid capture group (it contains the entire matched string)`
);
});
test("unsupported pattern configuration", () => {
const multipleNoLoopMatcher = {
owner: "test-matcher",
pattern: [
{
regexp: "^([^:]+)",
message: 1,
},
{
regexp: '^([^"]+)',
message: 1,
},
],
};
actual = () => matcher(multipleNoLoopMatcher, "Test");
expect(actual).toThrow(
`Unsupported pattern configuration. We currently support single pattern and multi-line loop pattern configurations`
);
});