-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathcli.js
545 lines (418 loc) · 14.6 KB
/
cli.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
const assert = require('assert');
const fs = require('fs-extra');
const path = require('path');
const exec = require('child_process').exec;
describe('cli', function () {
it('should return help instructions', function (done) {
var command = 'node bin/sass-lint -h';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
assert(stdout.indexOf('Usage') > -1);
return done();
});
});
it('should return a version', function (done) {
var command = 'node bin/sass-lint --version';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
assert(stdout.match(/^[0-9]+.[0-9]+(.[0-9]+)?/));
return done();
});
});
it('should not try to read and lint a directory', function (done) {
var command = 'node bin/sass-lint "tests/dir-test/**/*.scss" --no-exit --verbose --format json';
exec(command, function (err, stdout) {
var result = JSON.parse(stdout);
if (err) {
return done(err);
}
assert(stdout.indexOf('.scss') !== -1);
assert(stdout.indexOf('.sass') === -1);
assert.equal(result.length, 1);
assert.equal(result[0].filePath, 'tests/dir-test/dir.scss/test.scss');
return done();
});
});
it('Should accept multiple input paths', function (done) {
var command = 'node bin/sass-lint "tests/cli/cli-error.scss, tests/cli/cli-error.sass" --no-exit --verbose';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
assert(stdout.indexOf('.scss') !== -1);
assert(stdout.indexOf('.sass') !== -1);
return done();
});
});
it('Should accept multiple input globs', function (done) {
var command = 'node bin/sass-lint "tests/cli/*.scss, tests/cli/*.sass" --no-exit --verbose';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
assert(stdout.indexOf('.scss') !== -1);
assert(stdout.indexOf('.sass') !== -1);
return done();
});
});
it('Should accept multiple input paths from a config file', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.multiple-inputs.yml --no-exit --verbose';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
assert(stdout.indexOf('.scss') !== -1);
assert(stdout.indexOf('.sass') !== -1);
return done();
});
});
it('Should accept multiple input paths and multiple ignore paths', function (done) {
var command = 'node bin/sass-lint "tests/cli/cli-error.scss, tests/cli/cli-error.sass" -i "tests/cli/cli-error.scss, tests/cli/cli-error.sass" --no-exit --verbose';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
assert(stdout.indexOf('.scss') === -1);
assert(stdout.indexOf('.sass') === -1);
return done();
});
});
it('Should accept multiple input paths and multiple ignores from a config file', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.multiple-ignores.yml --no-exit --verbose';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
assert(stdout.indexOf('.scss') === -1);
assert(stdout.indexOf('.sass') === -1);
return done();
});
});
it('CLI format option should output JSON', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.stylish-output.yml tests/cli/cli.scss --verbose --format json';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
else {
try {
JSON.parse(stdout);
return done();
}
catch (e) {
return done(new Error('Not JSON'));
}
}
});
});
it('CLI format option should output valid JSON', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.stylish-output.yml tests/cli/*.scss --verbose --format json';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
else {
try {
var result = JSON.parse(stdout);
if (result && result.length && result.length > 1) {
return done();
}
return done(new Error('Output is not combined'));
}
catch (e) {
return done(new Error('Not JSON'));
}
}
});
});
it('CLI output option should write to test file', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.stylish-output.yml tests/cli/cli.scss --verbose --format json --output tests/cli-output.json',
outputFile = path.resolve(process.cwd(), 'tests/cli-output.json');
exec(command, function (err) {
if (err) {
return done(err);
}
else {
var contents = fs.readFileSync(outputFile, 'utf8');
if (contents.length > 0) {
fs.removeSync(outputFile);
return done();
}
else {
return done(new Error(outputFile + 'is empty'));
}
}
});
});
it('CLI output option should write JSON to test file', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.stylish-output.yml tests/cli/cli.scss --verbose --format json --output tests/cli-output.json',
outputFile = path.resolve(process.cwd(), 'tests/cli-output.json');
exec(command, function (err) {
if (err) {
return done(err);
}
else {
var contents = fs.readFileSync(outputFile, 'utf8');
if (contents.length > 0) {
try {
JSON.parse(contents);
fs.removeSync(outputFile);
return done();
}
catch (e) {
fs.removeSync(outputFile);
return done(new Error('Written file is not in JSON format'));
}
}
else {
fs.removeSync(outputFile);
return done(new Error(outputFile + 'is empty'));
}
}
});
});
// Test default config files
it('should return JSON from .sass-lint.yml', function (done) {
var command = 'node ../../../bin/sass-lint ../../cli/cli.scss --verbose';
exec(command, { cwd: path.join(__dirname, 'yml', '.sass-lint.yml') }, function (err, stdout) {
if (err) {
return done(err);
}
else {
try {
JSON.parse(stdout);
return done();
}
catch (e) {
return done(new Error('Not JSON'));
}
}
});
});
it('should return JSON from .sasslintrc', function (done) {
var command = 'node ../../../bin/sass-lint ../../cli/cli.scss -c ".sasslintrc" --verbose';
exec(command, { cwd: path.join(__dirname, 'yml', '.sasslintrc') }, function (err, stdout) {
if (err) {
return done(err);
}
else {
try {
JSON.parse(stdout);
return done();
}
catch (e) {
return done(new Error('Not JSON'));
}
}
});
});
// Test custom config path
it('should return JSON from a custom config', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.color-keyword-errors.yml tests/cli/cli.scss --verbose';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
else {
try {
JSON.parse(stdout);
return done();
}
catch (e) {
return done(new Error('Not JSON'));
}
}
});
});
// Test 0 errors/warnings when rules set to 0 in config
it('output should return no errors/warnings', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.json-lint.yml tests/cli/cli.scss --verbose';
exec(command, function (err, stdout) {
var result = 0;
if (err) {
return done(err);
}
result = stdout.length;
if (result !== 0) {
return done(new Error('warnings/errors were returned'));
}
return done();
});
});
// Test 1 warning when rules set to 0 in config
it('should return a warning', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.color-keyword-errors.yml tests/cli/cli.scss --verbose';
exec(command, function (err, stdout) {
var result = '';
if (err) {
return done(err);
}
else {
try {
result = JSON.parse(stdout);
}
catch (e) {
return done(new Error('Not JSON'));
}
if (result[0].warningCount === 1 && result[0].errorCount === 0) {
return done();
}
else {
return done(new Error('warnings/errors were expected to be returned but weren\'t'));
}
}
});
});
it('should return a warning - stylish', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.stylish-errors.yml tests/cli/cli.scss --verbose',
expectedOutputLength = 154;
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
else {
assert.equal(expectedOutputLength, stdout.length);
return done();
}
});
});
it('should not include ignored paths', function (done) {
var command = 'node bin/sass-lint -i "**/*.scss" -v -q --format json "**/cli/*.scss"';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
assert(stdout.indexOf('.scss') === -1);
return done();
});
});
it('should not include multiple ignored paths', function (done) {
var command = 'node bin/sass-lint -i "**/*.scss, **/*.sass" -q -v --format json';
exec(command, function (err, stdout) {
if (err) {
return done(err);
}
assert(stdout.indexOf('.scss') === -1);
assert(stdout.indexOf('.sass') === -1);
return done();
});
});
it('should override filename convention if a valid --syntax is provided', function (done) {
var command = 'node bin/sass-lint --syntax scss tests/cli/cli.txt --verbose --format json';
exec(command, function (err, stdout) {
var result = 0;
if (err) {
return done(err);
}
result = stdout.length;
if (result !== 0) {
return done(new Error('warnings/errors were returned'));
}
return done();
});
});
it('should exit with error when quiet', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.error-output.yml tests/cli/cli-error.scss --verbose --no-exit';
exec(command, function (err) {
if (err) {
return done();
}
return done(new Error('No error on exit'));
});
});
it('should exit with error when more warnings than --max-warnings', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.color-keyword-errors.yml tests/cli/cli.scss --max-warnings 0';
exec(command, function (err) {
if (err) {
return done();
}
return done(new Error('No error on exit'));
});
});
it('should not exit with an error if no config is specified', function (done) {
var command = 'node bin/sass-lint tests/cli/cli-clean.scss --verbose --no-exit';
exec(command, function (err) {
if (!err) {
return done();
}
return done(new Error('Exited with error code 1'));
});
});
/**
* We disabled eslints handle callback err rule here as we are deliberately throwing errors that we don't care about
*/
it('parse errors should report as a lint error', function (done) {
var command = 'node bin/sass-lint --config tests/yml/.stylish-output.yml tests/sass/parse.scss --verbose --no-exit --format json';
exec(command, function (err, stdout) { // eslint-disable-line handle-callback-err
var result = JSON.parse(stdout)[0];
assert.equal(1, result.errorCount);
done();
});
});
it('parse errors should report as severity 2', function (done) {
var command = 'node bin/sass-lint --config tests/yml/.stylish-output.yml tests/sass/parse.scss --verbose --no-exit --format json';
exec(command, function (err, stdout) { // eslint-disable-line handle-callback-err
var result = JSON.parse(stdout)[0],
messages = result.messages[0],
severity = 2;
assert.equal(severity, messages.severity);
done();
});
});
it('parse errors should report the correct message', function (done) {
var command = 'node bin/sass-lint --config tests/yml/.stylish-output.yml tests/sass/parse.scss --verbose --no-exit --format json';
exec(command, function (err, stdout) { // eslint-disable-line handle-callback-err
var result = JSON.parse(stdout)[0],
message = result.messages[0].message,
expected = 'Please check validity of the block starting from line #5';
assert.equal(expected, message);
done();
});
});
it('parse errors rule Id should be \'Fatal\'', function (done) {
var command = 'node bin/sass-lint --config tests/yml/.stylish-output.yml tests/sass/parse.scss --verbose --no-exit --format json';
exec(command, function (err, stdout) { // eslint-disable-line handle-callback-err
var result = JSON.parse(stdout)[0],
messages = result.messages[0],
ruleId = 'Fatal';
assert.equal(ruleId, messages.ruleId);
done();
});
});
});
// ==============================================================================
// UTF-8 BOM
// ==============================================================================
describe('Reading files with UTF-8 BOM', function () {
before(function () {
var testText = fs.readFileSync('tests/bom-utf8/starts-with-mixin-utf8-bom.scss').toString();
if (testText.charCodeAt(0) !== 0xFEFF) {
throw new Error('BOM test files have no BOM markers');
}
});
it('should not throw a parse error from file containing a BOM', function (done) {
var command = 'node bin/sass-lint -v tests/bom-utf8/starts-with-mixin-utf8-bom.scss --format json';
exec(command, function (err, stdout) { // eslint-disable-line handle-callback-err
var result = JSON.parse(stdout)[0];
// Files with BOM markers that start with a mixin throw a fatal error
// https://github.com/sasstools/sass-lint/issues/880
assert.equal(result.errorCount, 0);
done();
});
});
it('should return the correct amount of warnings from a file containing BOM markers', function (done) {
var command = 'node bin/sass-lint -v tests/bom-utf8/var-utf8-bom.scss --format json';
exec(command, function (err, stdout) { // eslint-disable-line handle-callback-err
var result = JSON.parse(stdout)[0];
// Files starting with variables threw extra errors
// see https://github.com/sasstools/sass-lint/issues/880
assert.equal(result.errorCount, 0);
assert.equal(result.warningCount, 2);
done();
});
});
});