UNPKG

11.5 kBJavaScriptView Raw
1var path = require('path'),
2 grunt = require('grunt'),
3 _ = require('lodash'),
4 hooker = grunt.util.hooker,
5 chalk = require('chalk'),
6 scsslint = require('../tasks/lib/scss-lint').init(grunt),
7 fixtures = path.join(__dirname, 'fixtures'),
8 reporterOutFile = path.join(__dirname, 'output.xml'),
9 escapeRe = function (str) {
10 return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
11 },
12 defaultOptions;
13
14defaultOptions = {
15 config: path.join(fixtures, '.scss-lint-test.yml')
16};
17
18exports.scsslint = {
19 setUp: function (done) {
20 grunt.file.delete(reporterOutFile);
21 done();
22 },
23
24 fail: function (test) {
25 test.expect(5);
26 var files = path.join(fixtures, 'fail.scss');
27 scsslint.lint(files, defaultOptions, function (results) {
28 results = results.split('\n');
29 test.ok(
30 results[0].indexOf('Class `Button` in selector should be written in all lowercase as `button`') !== -1,
31 'Should report bad case.'
32 );
33 test.ok(
34 results[1].indexOf('Properties should be sorted in order, with vendor-prefixed extensions before the ' +
35 'standardized CSS property') !== -1,
36 'Should report bad ordering.'
37 );
38 test.ok(
39 results[2].indexOf('Color `black` should be written in hexadecimal form as `#000000`') !== -1,
40 'Should report string colour usage.'
41 );
42 test.ok(
43 results[3].indexOf('Files should end with a trailing newline') !== -1,
44 'Should report trailing newline.'
45 );
46 test.ok(results.length === 4);
47 test.done();
48 });
49 },
50
51 pass: function (test) {
52 test.expect(1);
53 var files = path.join(fixtures, 'pass.scss');
54 scsslint.lint(files, defaultOptions, function (results) {
55 test.ok(!results, 'There should be no lint errors');
56 test.done();
57 });
58 },
59
60 passWithForce: function (test) {
61 test.expect(2);
62 var files = path.join(fixtures, 'fail.scss'),
63 muted = grunt.log.muted,
64 stdout = [],
65 testOptions;
66
67 testOptions = _.assign({}, defaultOptions, {
68 force: true
69 });
70
71 grunt.log.muted = false;
72
73 hooker.hook(process.stdout, 'write', {
74 pre: function (result) {
75 stdout.push(grunt.log.uncolor(result));
76 return hooker.preempt();
77 }
78 });
79
80 scsslint.lint(files, testOptions, function (results) {
81 grunt.option('debug', undefined);
82 hooker.unhook(process.stdout, 'write');
83 grunt.log.muted = muted;
84
85 test.ok(results, 'Should return results.');
86
87 test.ok(
88 stdout[1].indexOf('scss-lint failed, but was run in force mode') !== -1,
89 'Should log forcing.'
90 );
91
92 test.done();
93 });
94 },
95
96 debugOption: function (test) {
97 test.expect(1);
98 var files = path.join(fixtures, 'pass.scss'),
99 muted = grunt.log.muted,
100 stdoutMsg = '';
101
102 grunt.log.muted = false;
103
104 hooker.hook(process.stdout, 'write', {
105 pre: function (result) {
106 stdoutMsg += grunt.log.uncolor(result);
107 return hooker.preempt();
108 }
109 });
110
111 grunt.option('debug', true);
112
113 scsslint.lint(files, defaultOptions, function () {
114 grunt.option('debug', undefined);
115 hooker.unhook(process.stdout, 'write');
116 grunt.log.muted = muted;
117
118 test.ok(stdoutMsg.indexOf('Run command: scss-lint -c') !== -1, 'Show debug information');
119 test.done();
120 });
121 },
122
123 bundleExec: function (test) {
124 test.expect(1);
125 var files = path.join(fixtures, 'pass.scss'),
126 testOptions;
127
128 testOptions = _.assign({}, defaultOptions, {
129 bundleExec: true
130 });
131
132 scsslint.lint(files, testOptions, function (results) {
133 test.ok(!results, 'There should be no lint errors');
134 test.done();
135 });
136 },
137
138 passWithExcludedFile: function (test) {
139 test.expect(1);
140 var files = path.join(fixtures, '*.scss'),
141 testOptions;
142
143 testOptions = _.assign({}, defaultOptions, {
144 exclude: [
145 path.join(fixtures, 'fail.scss'),
146 path.join(fixtures, 'fail2.scss')
147 ]
148 });
149
150 scsslint.lint(files, testOptions, function (results) {
151 test.ok(!results, 'There should be no lint errors');
152 test.done();
153 });
154 },
155
156 failWithBadOptions: function (test) {
157 test.expect(1);
158 var files = '--incorrectlySpecifyingAnOptionAsAFile';
159
160 scsslint.lint(files, defaultOptions, function (results) {
161 test.ok(!results,
162 'There should be no lint errors but should return failure');
163 test.done();
164 });
165 },
166
167 multipleFiles: function (test) {
168 test.expect(1);
169 var files = path.join(fixtures, 'pass.scss');
170 scsslint.lint([files, files, files], defaultOptions, function (results) {
171 test.ok(!results,
172 'There should be no lint errors');
173 test.done();
174 });
175 },
176
177 reporter: function (test) {
178 test.expect(2);
179 var files = path.join(fixtures, 'fail.scss'),
180 testOptions;
181
182 testOptions = _.assign({}, defaultOptions, {
183 reporterOutput: reporterOutFile
184 });
185
186 scsslint.lint(files, testOptions, function (results) {
187 var report = grunt.file.read(reporterOutFile);
188
189 results = results.split('\n');
190
191 test.ok(report.indexOf(results[0]) !== -1,
192 'Should write the errors out to a report');
193 test.ok(report.indexOf('errors="4"') !== -1,
194 'Should write the number of errors out to a report');
195 test.done();
196 });
197 },
198
199 reporterErrors: function (test) {
200 test.expect(1);
201 var files = path.join(fixtures, 'pass.scss'),
202 testOptions;
203
204 testOptions = _.assign({}, defaultOptions, {
205 reporterOutput: reporterOutFile
206 });
207
208 scsslint.lint(files, testOptions, function (results) {
209 var report = grunt.file.read(reporterOutFile);
210
211 test.ok(report.indexOf('errors="0"') !== -1,
212 'Should write the number of errors out to a report');
213 test.done();
214 });
215 },
216
217 colorizeOutput: function (test) {
218 test.expect(3);
219 var file = path.join(fixtures, 'fail.scss'),
220 testOptions;
221
222 testOptions = _.assign({}, defaultOptions, {
223 colorizeOutput: true,
224 colouriseOutput: true
225 });
226
227 scsslint.lint(file, testOptions, function (results) {
228 var styles = chalk.styles;
229
230 results = results.split('\n')[0];
231
232 test.ok(
233 results.indexOf(styles.cyan.open + file) !== -1,
234 'Should report colorized file name.'
235 );
236 test.ok(
237 results.indexOf(styles.magenta.open + '1' !== -1,
238 'Should report colorized file line.')
239 );
240 test.ok(
241 results.indexOf(styles.yellow.open + '[W]' !== -1,
242 'Should report colorized warning.')
243 );
244 test.done();
245 });
246 },
247
248 compactWithoutColor: function (test) {
249 test.expect(4);
250 var file1 = path.join(fixtures, 'fail.scss'),
251 file2 = path.join(fixtures, 'fail2.scss'),
252 testOptions;
253
254 testOptions = _.assign({}, defaultOptions, {
255 compact: true
256 });
257
258 scsslint.lint([file1, file2], testOptions, function (results) {
259 results = results.split('\n');
260
261 test.ok(
262 results[1].indexOf(file1) !== -1,
263 'Should report file name of first file.'
264 );
265
266 test.ok(
267 results[2].indexOf('1:') !== -1,
268 'Should report line number for first file.'
269 );
270
271 test.ok(
272 typeof(/\:\s(.+)/.exec(results[2])[1]) === 'string',
273 'Should report description for first file.'
274 );
275
276 test.ok(
277 results[7].indexOf(file2) !== -1,
278 'Should report file name of second file.'
279 );
280
281 test.done();
282 });
283 },
284
285 compactWithColor: function (test) {
286 test.expect(4);
287 var file1 = path.join(fixtures, 'fail.scss'),
288 file2 = path.join(fixtures, 'fail2.scss'),
289 testOptions;
290
291 testOptions = _.assign({}, defaultOptions, {
292 colorizeOutput: true,
293 colourizeOutput: true,
294 compact: true
295 });
296
297 scsslint.lint([file1, file2], testOptions, function (results) {
298 var styles = chalk.styles;
299 results = results.split('\n');
300
301 test.ok(
302 results[1].indexOf(styles.bold.open + file1) !== -1,
303 'Should report file name of first file.'
304 );
305
306 test.ok(
307 results[2].indexOf(styles.magenta.open + '1' + styles.magenta.close + ':') !== -1,
308 'Should report line number for first file.'
309 );
310
311 test.ok(
312 typeof(new RegExp(escapeRe(styles.magenta.close) + '\\:\\s(.+)').exec(results[2])[1]) === 'string',
313 'Should report description for first file.'
314 );
315
316 test.ok(
317 results[7].indexOf(styles.bold.open + file2) !== -1,
318 'Should report file name of second file.'
319 );
320
321 test.done();
322 });
323 },
324
325 pluralizeSingleFile: function (test) {
326 test.expect(1);
327 var files = path.join(fixtures, 'pass.scss'),
328 muted = grunt.log.muted,
329 stdoutMsg = '';
330
331 grunt.log.muted = false;
332
333 hooker.hook(process.stdout, 'write', {
334 pre: function (result) {
335 stdoutMsg += grunt.log.uncolor(result);
336 return hooker.preempt();
337 }
338 });
339
340 scsslint.lint(files, defaultOptions, function () {
341 hooker.unhook(process.stdout, 'write');
342 grunt.log.muted = muted;
343 test.ok(stdoutMsg.indexOf('1 file is lint free') !== -1, 'Report single file lint free');
344 test.done();
345 });
346 },
347
348 pluralizeMultipleFiles: function (test) {
349 test.expect(1);
350 var files = path.join(fixtures, 'pass.scss'),
351 muted = grunt.log.muted,
352 stdoutMsg = '';
353
354 grunt.log.muted = false;
355
356 hooker.hook(process.stdout, 'write', {
357 pre: function (result) {
358 stdoutMsg += grunt.log.uncolor(result);
359 return hooker.preempt();
360 }
361 });
362
363 files = [files, files];
364 scsslint.lint(files, defaultOptions, function () {
365 hooker.unhook(process.stdout, 'write');
366 grunt.log.muted = muted;
367
368 test.ok(stdoutMsg.indexOf(files.length + ' files are lint free') !== -1, 'Report multiple files lint free');
369 test.done();
370 });
371 },
372
373 emitError: function (test) {
374 test.expect(1);
375 var file1 = path.join(fixtures, 'fail.scss'),
376 testOptions;
377
378 testOptions = _.assign({}, defaultOptions, {
379 emitError: true
380 });
381
382 scsslint.lint(file1, testOptions, function (results) {
383 results = results.split('\n');
384 test.ok(results.length === 4);
385 test.done();
386 });
387 },
388
389 exitCodeOnFailure: function (test) {
390 test.expect(1);
391 grunt.util.spawn({grunt: true, args: ['scsslint']}, function (error, result, code) {
392 test.notEqual(code, 0);
393 test.done();
394 });
395 },
396
397 exitCodeAndOutputOnMissingRuby: function (test) {
398 test.expect(2);
399 grunt.util.spawn({grunt: true, args: ['scsslint'], opts: {env: {PATH: '.'}}}, function (error, result, code) {
400 test.notEqual(code, 0);
401 test.ok(result.stdout.match('Please make sure you have ruby installed'));
402 test.done();
403 });
404 },
405
406 exitCodeOnSuccess: function (test) {
407 test.expect(1);
408 grunt.util.spawn({grunt: true, args: ['scsslint:success']}, function (error, result, code) {
409 test.equal(code, 0);
410 test.done();
411 });
412 },
413
414 maxBuffer: function (test) {
415 test.expect(1);
416 var files = path.join(fixtures, 'fail.scss'),
417 testOptions;
418
419 testOptions = _.assign({}, defaultOptions, {
420 maxBuffer: false
421 });
422
423 scsslint.lint(files, testOptions, function (results) {
424 test.ok(!results, 'There should be no lint errors');
425 test.done();
426 });
427 }
428};