-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
313 lines (263 loc) · 11 KB
/
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
/* eslint-disable no-param-reassign */
/* eslint-env jest */
import stripAnsi from 'strip-ansi';
import logSymbols from 'log-symbols';
import stylint from 'stylint';
import path from 'path';
import origCache from 'stylint/src/core/cache';
import origState from 'stylint/src/core/state';
const errorIcon = stripAnsi(logSymbols.error);
const warningIcon = stripAnsi(logSymbols.warning);
let stylintInstance;
beforeEach(() => {
stylintInstance = stylint().create();
stylintInstance.state = JSON.parse(JSON.stringify(origState));
stylintInstance.cache = JSON.parse(JSON.stringify(origCache));
stylintInstance.state.quiet = true;
stylintInstance.state.watching = true;
stylintInstance.state.strictMode = false;
stylintInstance.config.reporter = require.resolve('./stylish');
stylintInstance.config.reporterOptions = undefined;
stylintInstance.config.maxErrors = undefined;
stylintInstance.config.maxWarnings = undefined;
stylintInstance.init();
});
test('should report if all green', () => {
const report = stylintInstance.reporter('meh', 'done').msg;
expect(report).toEqual('');
});
test('should report violations', () => {
stylintInstance.cache.file = path.resolve('file.styl');
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.errs = ['', ''];
stylintInstance.cache.warnings = [''];
stylintInstance.state.severity = '';
stylintInstance.reporter('woop');
stylintInstance.cache.lineNo = 10;
stylintInstance.state.severity = 'Warning';
stylintInstance.reporter('dee');
stylintInstance.cache.file = 'meep.styl';
stylintInstance.cache.lineNo = 15;
stylintInstance.state.severity = '';
stylintInstance.reporter('doo');
let report = stylintInstance.reporter('meh', 'done').msg;
report = stripAnsi(report).split('\n');
expect(report.length).toEqual(11);
expect(report[0]).toEqual('');
expect(report[1]).toEqual('file.styl');
expect(report[2].trim()).toEqual('line 15 - woop');
expect(report[3].trim()).toEqual('line 10 - dee');
expect(report[4]).toEqual('');
expect(report[5]).toEqual('meep.styl');
expect(report[6].trim()).toEqual('line 15 - doo');
expect(report[7]).toEqual('');
expect(report[8].trim()).toEqual(`${errorIcon} 2 errors`);
expect(report[9].trim()).toEqual(`${warningIcon} 1 warning`);
expect(report[10]).toEqual('');
});
test('should log Max Errors if provided', () => {
stylintInstance.config.maxErrors = 1;
stylintInstance.cache.file = path.resolve('file.styl');
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.errs = ['', ''];
stylintInstance.cache.warnings = [];
stylintInstance.state.severity = '';
stylintInstance.reporter('woop');
stylintInstance.cache.lineNo = 10;
stylintInstance.state.severity = 'Warning';
stylintInstance.reporter('dee');
let report = stylintInstance.reporter('meh', 'done').msg;
report = stripAnsi(report).split('\n');
expect(report.length).toEqual(7);
expect(report[0]).toEqual('');
expect(report[1]).toEqual('file.styl');
expect(report[2].trim()).toEqual('line 15 - woop');
expect(report[3].trim()).toEqual('line 10 - dee');
expect(report[4]).toEqual('');
expect(report[5].trim()).toEqual(`${errorIcon} 2 errors (Max Errors: 1)`);
expect(report[6]).toEqual('');
});
test('should log Max Warnings if provided', () => {
stylintInstance.config.maxWarnings = 1;
stylintInstance.cache.file = path.resolve('file.styl');
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.warnings = ['', ''];
stylintInstance.cache.errs = [];
stylintInstance.state.severity = '';
stylintInstance.reporter('woop');
stylintInstance.cache.lineNo = 10;
stylintInstance.state.severity = 'Warning';
stylintInstance.reporter('dee');
let report = stylintInstance.reporter('meh', 'done').msg;
report = stripAnsi(report).split('\n');
expect(report.length).toEqual(7);
expect(report[0]).toEqual('');
expect(report[1]).toEqual('file.styl');
expect(report[2].trim()).toEqual('line 15 - woop');
expect(report[3].trim()).toEqual('line 10 - dee');
expect(report[4]).toEqual('');
expect(report[5].trim()).toEqual(
`${warningIcon} 2 warnings (Max Warnings: 1)`
);
expect(report[6]).toEqual('');
});
test('should log kill message if provided', () => {
stylintInstance.config.maxErrors = 1;
stylintInstance.cache.file = path.resolve('file.styl');
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.errs = ['', ''];
stylintInstance.cache.warnings = [];
stylintInstance.state.severity = '';
stylintInstance.reporter('woop');
stylintInstance.cache.lineNo = 10;
stylintInstance.state.severity = 'Warning';
stylintInstance.reporter('dee');
let report = stylintInstance.reporter('meh', 'done', 'kill').msg;
report = stripAnsi(report).split('\n');
expect(report.length).toEqual(8);
expect(report[0]).toEqual('');
expect(report[1]).toEqual('file.styl');
expect(report[2].trim()).toEqual('line 15 - woop');
expect(report[3].trim()).toEqual('line 10 - dee');
expect(report[4]).toEqual('');
expect(report[5].trim()).toEqual(`${errorIcon} 2 errors (Max Errors: 1)`);
expect(report[6]).toEqual('');
});
test('should report violations with absolute path', () => {
stylintInstance.config.reporterOptions = { absolutePath: true };
stylintInstance.cache.file = path.resolve('file.styl');
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.warnings = [''];
stylintInstance.state.severity = '';
stylintInstance.reporter('woop');
let report = stylintInstance.reporter('meh', 'done').msg;
report = stripAnsi(report).split('\n');
expect(report.length).toEqual(6);
expect(report[0]).toEqual('');
expect(report[1]).toEqual(path.join(process.cwd(), 'file.styl'));
expect(report[2].trim()).toEqual('line 15 - woop');
expect(report[3]).toEqual('');
expect(report[4].trim()).toEqual(`${warningIcon} 1 warning`);
expect(report[5]).toEqual('');
});
test('should report violations with absolute path with file being relative path', () => {
stylintInstance.config.reporterOptions = { absolutePath: true };
stylintInstance.cache.file = 'file.styl';
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.warnings = [''];
stylintInstance.state.severity = '';
stylintInstance.reporter('woop');
let report = stylintInstance.reporter('meh', 'done').msg;
report = stripAnsi(report).split('\n');
expect(report.length).toEqual(6);
expect(report[0]).toEqual('');
expect(report[1]).toEqual(path.join(process.cwd(), 'file.styl'));
expect(report[2].trim()).toEqual('line 15 - woop');
expect(report[3]).toEqual('');
expect(report[4].trim()).toEqual(`${warningIcon} 1 warning`);
expect(report[5]).toEqual('');
});
test('should report violations with max errors', () => {
stylintInstance.config.maxErrors = 0;
stylintInstance.cache.file = path.resolve('file.styl');
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.errs = ['meep'];
stylintInstance.state.severity = '';
stylintInstance.reporter('woop');
let report = stylintInstance.reporter('meh', 'done').msg;
report = stripAnsi(report).split('\n');
expect(report.length).toEqual(6);
expect(report[0]).toEqual('');
expect(report[1]).toEqual('file.styl');
expect(report[2].trim()).toEqual('line 15 - woop');
expect(report[3]).toEqual('');
expect(report[4].trim()).toEqual(`${errorIcon} 1 error (Max Errors: 0)`);
expect(report[5]).toEqual('');
});
test('should report original line if verbose', () => {
stylintInstance.config.reporterOptions = { verbose: true };
stylintInstance.cache.file = path.resolve('file.styl');
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.errs = [];
stylintInstance.cache.warnings = [''];
stylintInstance.cache.origLine = 'beep boop moop';
stylintInstance.state.severity = '';
stylintInstance.reporter('woop');
let report = stylintInstance.reporter('meh', 'done').msg;
report = stripAnsi(report).split('\n');
expect(report.length).toEqual(6);
expect(report[0]).toEqual('');
expect(report[1]).toEqual('file.styl');
expect(report[2].trim()).toEqual('line 15 - woop beep boop moop');
expect(report[3]).toEqual('');
expect(report[4].trim()).toEqual(`${warningIcon} 1 warning`);
expect(report[5]).toEqual('');
});
test('should report violations with column if available', () => {
stylintInstance.cache.file = path.resolve('file.styl');
stylintInstance.cache.col = 10;
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.rule = 'some rule';
stylintInstance.cache.errs = ['', ''];
stylintInstance.cache.warnings = [''];
stylintInstance.state.severity = '';
stylintInstance.reporter('woop');
stylintInstance.cache.lineNo = 10;
stylintInstance.cache.col = 25;
stylintInstance.cache.rule = 'some other rule';
stylintInstance.state.severity = 'Warning';
stylintInstance.reporter('dee');
stylintInstance.cache.file = 'meep.styl';
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.col = 42;
stylintInstance.state.severity = '';
stylintInstance.reporter('doo');
let report = stylintInstance.reporter('meh', 'done').msg;
report = stripAnsi(report).split('\n');
expect(report.length).toEqual(11);
expect(report[0]).toEqual('');
expect(report[1]).toEqual('file.styl');
expect(report[2].trim()).toEqual('line 15 col 10 woop');
expect(report[3].trim()).toEqual('line 10 col 25 dee');
expect(report[4]).toEqual('');
expect(report[5]).toEqual('meep.styl');
expect(report[6].trim()).toEqual('line 15 col 42 doo');
expect(report[7]).toEqual('');
expect(report[8].trim()).toEqual(`${errorIcon} 2 errors`);
expect(report[9].trim()).toEqual(`${warningIcon} 1 warning`);
expect(report[10]).toEqual('');
});
test('should report violations with rule name if option active', () => {
stylintInstance.config.reporterOptions = { ruleName: true };
stylintInstance.cache.file = path.resolve('file.styl');
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.rule = 'some rule';
stylintInstance.cache.errs = ['', ''];
stylintInstance.cache.warnings = [''];
stylintInstance.state.severity = '';
stylintInstance.reporter('woop');
stylintInstance.cache.lineNo = 10;
stylintInstance.cache.rule = 'some other rule';
stylintInstance.state.severity = 'Warning';
stylintInstance.reporter('dee');
stylintInstance.cache.file = 'meep.styl';
stylintInstance.cache.lineNo = 15;
stylintInstance.cache.col = 42;
stylintInstance.cache.rule = null;
stylintInstance.state.severity = '';
stylintInstance.reporter('doo');
let report = stylintInstance.reporter('meh', 'done').msg;
report = stripAnsi(report).split('\n');
expect(report.length).toEqual(11);
expect(report[0]).toEqual('');
expect(report[1]).toEqual('file.styl');
expect(report[2].trim()).toEqual('line 15 - woop some rule');
expect(report[3].trim()).toEqual('line 10 - dee some other rule');
expect(report[4]).toEqual('');
expect(report[5]).toEqual('meep.styl');
expect(report[6].trim()).toEqual('line 15 col 42 doo');
expect(report[7]).toEqual('');
expect(report[8].trim()).toEqual(`${errorIcon} 2 errors`);
expect(report[9].trim()).toEqual(`${warningIcon} 1 warning`);
expect(report[10]).toEqual('');
});