1 | var path = require('path'),
|
2 | grunt = require('grunt'),
|
3 | hooker = grunt.util.hooker,
|
4 | chalk = require('chalk'),
|
5 | scsslint = require('../tasks/lib/scss-lint').init(grunt),
|
6 | fixtures = path.join(__dirname, 'fixtures'),
|
7 | reporterOutFile = path.join(__dirname, 'output.xml'),
|
8 | escapeRe = function (str) {
|
9 | return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
10 | },
|
11 | options = {config: path.join(fixtures, '.scss-lint-test.yml')};
|
12 |
|
13 | exports.scsslint = {
|
14 | setUp: function (done) {
|
15 | grunt.file.delete(reporterOutFile);
|
16 | done();
|
17 | },
|
18 |
|
19 | fail: function (test) {
|
20 | test.expect(5);
|
21 | var files = path.join(fixtures, 'fail.scss');
|
22 | scsslint.lint(files, options, function (results) {
|
23 | results = results.split("\n");
|
24 | test.ok(
|
25 | results[0].indexOf('Class `Button` in selector should be written in all lowercase as `button`') !== -1,
|
26 | 'Should report bad case.'
|
27 | );
|
28 | test.ok(
|
29 | results[1].indexOf('Properties should be sorted in order, with vendor-prefixed extensions before the ' +
|
30 | 'standardized CSS property') !== -1,
|
31 | 'Should report bad ordering.'
|
32 | );
|
33 | test.ok(
|
34 | results[2].indexOf('Color `black` should be written in hexadecimal form as `#000000`') !== -1,
|
35 | 'Should report string colour usage.'
|
36 | );
|
37 | test.ok(
|
38 | results[3].indexOf('Files should end with a trailing newline') !== -1,
|
39 | 'Should report trailing newline.'
|
40 | );
|
41 | test.ok(results.length === 4);
|
42 | test.done();
|
43 | });
|
44 | },
|
45 |
|
46 | pass: function (test) {
|
47 | test.expect(1);
|
48 | var files = path.join(fixtures, 'pass.scss');
|
49 | scsslint.lint(files, options, function (results) {
|
50 | test.ok(!results, 'There should be no lint errors');
|
51 | test.done();
|
52 | });
|
53 | },
|
54 |
|
55 | debugOption: function (test) {
|
56 | test.expect(1);
|
57 | var files = path.join(fixtures, 'pass.scss'),
|
58 | muted = grunt.log.muted,
|
59 | stdoutMsg = '';
|
60 |
|
61 | grunt.log.muted = false;
|
62 |
|
63 | hooker.hook(process.stdout, 'write', {
|
64 | pre: function (result) {
|
65 | stdoutMsg += grunt.log.uncolor(result);
|
66 | return hooker.preempt();
|
67 | }
|
68 | });
|
69 |
|
70 | grunt.option('debug', true);
|
71 | scsslint.lint(files, options, function () {
|
72 | grunt.option('debug', undefined);
|
73 | hooker.unhook(process.stdout, 'write');
|
74 | grunt.log.muted = muted;
|
75 |
|
76 | test.ok(stdoutMsg.indexOf('Run command: scss-lint -c') !== -1, 'Show debug information');
|
77 | test.done();
|
78 | });
|
79 | },
|
80 |
|
81 | bundleExec: function (test) {
|
82 | test.expect(1);
|
83 | var files = path.join(fixtures, 'pass.scss');
|
84 | options['bundleExec'] = true;
|
85 |
|
86 | scsslint.lint(files, options, function (results) {
|
87 | test.ok(!results, 'There should be no lint errors');
|
88 | test.done();
|
89 | });
|
90 | },
|
91 |
|
92 | passWithExcludedFile: function (test) {
|
93 | test.expect(1);
|
94 | var files = path.join(fixtures, '*.scss'),
|
95 | options = {exclude: [path.join(fixtures, 'fail.scss'), path.join(fixtures, 'fail2.scss')]};
|
96 |
|
97 | scsslint.lint(files, options, function (results) {
|
98 | test.ok(!results, 'There should be no lint errors');
|
99 | test.done();
|
100 | });
|
101 | },
|
102 |
|
103 | failWithBadOptions: function (test) {
|
104 | test.expect(1);
|
105 | var files = '--incorrectlySpecifyingAnOptionAsAFile';
|
106 |
|
107 | scsslint.lint(files, options, function (results) {
|
108 | test.ok(!results, 'There should be no lint errors but should return failure');
|
109 | test.done();
|
110 | });
|
111 | },
|
112 |
|
113 | multipleFiles: function (test) {
|
114 | test.expect(1);
|
115 | var files = path.join(fixtures, 'pass.scss');
|
116 | scsslint.lint([files, files, files], options, function (results) {
|
117 | test.ok(!results, 'There should be no lint errors');
|
118 | test.done();
|
119 | });
|
120 | },
|
121 |
|
122 | reporter: function (test) {
|
123 | test.expect(2);
|
124 | var files = path.join(fixtures, 'fail.scss');
|
125 | scsslint.lint(files, {reporterOutput: reporterOutFile}, function (results) {
|
126 | var report = grunt.file.read(reporterOutFile);
|
127 |
|
128 | results = results.split("\n");
|
129 |
|
130 | test.ok(report.indexOf(results[0]) !== -1, 'Should write the errors out to a report');
|
131 | test.ok(report.indexOf('errors="4"') !== -1, 'Should write the number of errors out to a report');
|
132 | test.done();
|
133 | });
|
134 | },
|
135 |
|
136 | reporterErrors: function (test) {
|
137 | test.expect(1);
|
138 | var files = path.join(fixtures, 'pass.scss');
|
139 | scsslint.lint(files, {reporterOutput: reporterOutFile}, function (results) {
|
140 |
|
141 | var report = grunt.file.read(reporterOutFile);
|
142 |
|
143 | test.ok(report.indexOf('errors="0"') !== -1, 'Should write the number of errors out to a report');
|
144 | test.done();
|
145 | });
|
146 | },
|
147 |
|
148 | colorizeOutput: function (test) {
|
149 | test.expect(3);
|
150 | var file = path.join(fixtures, 'fail.scss');
|
151 | scsslint.lint(file, {colorizeOutput: true, colouriseOutput: true}, function (results) {
|
152 | var styles = chalk.styles;
|
153 |
|
154 | results = results.split("\n")[0];
|
155 |
|
156 | test.ok(
|
157 | results.indexOf(styles.cyan.open + file) !== -1,
|
158 | 'Should report colorized file name.'
|
159 | );
|
160 | test.ok(
|
161 | results.indexOf(styles.magenta.open + '1' !== -1,
|
162 | 'Should report colorized file line.')
|
163 | );
|
164 | test.ok(
|
165 | results.indexOf(styles.yellow.open + '[W]' !== -1,
|
166 | 'Should report colorized warning.')
|
167 | );
|
168 | test.done();
|
169 | });
|
170 | },
|
171 |
|
172 | compactWithoutColor: function (test) {
|
173 | test.expect(4);
|
174 | var file1 = path.join(fixtures, 'fail.scss'),
|
175 | file2 = path.join(fixtures, 'fail2.scss');
|
176 | scsslint.lint([file1, file2], {colorizeOutput: false, compact: true}, function (results) {
|
177 |
|
178 | results = results.split("\n");
|
179 |
|
180 | test.ok(
|
181 | results[1].indexOf(file1) !== -1,
|
182 | 'Should report file name of first file.'
|
183 | );
|
184 |
|
185 | test.ok(
|
186 | results[2].indexOf('1:') !== -1,
|
187 | 'Should report line number for first file.'
|
188 | );
|
189 |
|
190 | test.ok(
|
191 | typeof(/\:\s(.+)/.exec(results[2])[1]) === 'string',
|
192 | 'Should report description for first file.'
|
193 | );
|
194 |
|
195 | test.ok(
|
196 | results[7].indexOf(file2) !== -1,
|
197 | 'Should report file name of second file.'
|
198 | );
|
199 |
|
200 | test.done();
|
201 | });
|
202 | },
|
203 |
|
204 | compactWithColor: function (test) {
|
205 | test.expect(4);
|
206 | var file1 = path.join(fixtures, 'fail.scss'),
|
207 | file2 = path.join(fixtures, 'fail2.scss');
|
208 | scsslint.lint([file1, file2], {colorizeOutput: true, compact: true}, function (results) {
|
209 | var styles = chalk.styles;
|
210 | results = results.split("\n");
|
211 |
|
212 | test.ok(
|
213 | results[1].indexOf(styles.bold.open + file1) !== -1,
|
214 | 'Should report file name of first file.'
|
215 | );
|
216 |
|
217 | test.ok(
|
218 | results[2].indexOf(styles.magenta.open + '1' + styles.magenta.close + ':') !== -1,
|
219 | 'Should report line number for first file.'
|
220 | );
|
221 |
|
222 | test.ok(
|
223 | typeof(new RegExp(escapeRe(styles.magenta.close) + '\\:\\s(.+)').exec(results[2])[1]) === 'string',
|
224 | 'Should report description for first file.'
|
225 | );
|
226 |
|
227 | test.ok(
|
228 | results[7].indexOf(styles.bold.open + file2) !== -1,
|
229 | 'Should report file name of second file.'
|
230 | );
|
231 |
|
232 | test.done();
|
233 | });
|
234 | },
|
235 |
|
236 | pluralizeSingleFile: function (test) {
|
237 | test.expect(1);
|
238 | var files = path.join(fixtures, 'pass.scss'),
|
239 | muted = grunt.log.muted,
|
240 | stdoutMsg = '';
|
241 |
|
242 | grunt.log.muted = false;
|
243 |
|
244 | hooker.hook(process.stdout, 'write', {
|
245 | pre: function (result) {
|
246 | stdoutMsg += grunt.log.uncolor(result);
|
247 | return hooker.preempt();
|
248 | }
|
249 | });
|
250 |
|
251 | scsslint.lint(files, options, function () {
|
252 | hooker.unhook(process.stdout, 'write');
|
253 | grunt.log.muted = muted;
|
254 |
|
255 | test.ok(stdoutMsg.indexOf('1 file is lint free') !== -1, 'Report single file lint free');
|
256 | test.done();
|
257 | });
|
258 | },
|
259 |
|
260 | pluralizeMultipleFiles: function (test) {
|
261 | test.expect(1);
|
262 | var files = path.join(fixtures, 'pass.scss'),
|
263 | muted = grunt.log.muted,
|
264 | stdoutMsg = '';
|
265 |
|
266 | grunt.log.muted = false;
|
267 |
|
268 | hooker.hook(process.stdout, 'write', {
|
269 | pre: function (result) {
|
270 | stdoutMsg += grunt.log.uncolor(result);
|
271 | return hooker.preempt();
|
272 | }
|
273 | });
|
274 |
|
275 | files = [files, files];
|
276 | scsslint.lint(files, options, function () {
|
277 | hooker.unhook(process.stdout, 'write');
|
278 | grunt.log.muted = muted;
|
279 |
|
280 | test.ok(stdoutMsg.indexOf(files.length + ' files are lint free') !== -1, 'Report multiple files lint free');
|
281 | test.done();
|
282 | });
|
283 | },
|
284 |
|
285 | emitError: function (test) {
|
286 | test.expect(1);
|
287 | var file1 = path.join(fixtures, 'fail.scss');
|
288 | options['emitError'] = true;
|
289 |
|
290 | scsslint.lint(file1, options, function (results) {
|
291 | results = results.split("\n");
|
292 | test.ok(results.length === 4);
|
293 | test.done();
|
294 | });
|
295 | }
|
296 | };
|