Skip to content

Commit 122cfe8

Browse files
committed
Merge pull request #36 from SimenB/print-huge-strings
Use process.stdout.write to print the report
2 parents f516a10 + 9a9c5ad commit 122cfe8

File tree

5 files changed

+124
-23
lines changed

5 files changed

+124
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*.iml
55

66
coverage/
7+
test-output.xml

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,56 @@ gulp.task('lint', function() {
102102
});
103103
```
104104
105+
### Reporter options
106+
You can also pass options to the built-in formatter, by passing a second option to `reporter`.
107+
108+
```js
109+
gulp.task('lint', function() {
110+
gulp.files('lib/*.css')
111+
.pipe(csslint())
112+
.pipe(csslint.reporter('junit-xml', options));
113+
});
114+
```
115+
116+
See the documentation for the formatters regarding what options they support.
117+
118+
This plugin supports one option outside of that, called `logger`, allowing you to specify how to log out the report.
119+
Default is using `process.stdout.write`, but you can use e.g. `console.log`, or `gutil.log`.
120+
121+
```js
122+
gulp.task('lint', function() {
123+
gulp.files('lib/*.css')
124+
.pipe(csslint())
125+
.pipe(csslint.reporter('junit-xml', {logger: console.log.bind(console)}));
126+
});
127+
```
128+
129+
```js
130+
gulp.task('lint', function() {
131+
gulp.files('lib/*.css')
132+
.pipe(csslint())
133+
.pipe(csslint.reporter('junit-xml', {logger: gutil.log.bind(null, 'gulp-csslint:')}));
134+
});
135+
```
136+
137+
`logger` is called once for the starting format of the reporter, then once for each file containing violations, then
138+
lastly once for the ending format. Instead of writing to `stdout`, you can write to file using this option.
139+
140+
```js
141+
gulp.task('lint', function(cb) {
142+
var fs = require('fs');
143+
var output = '';
144+
145+
gulp.files('lib/*.css')
146+
.pipe(csslint())
147+
.pipe(csslint.reporter('junit-xml', {logger: function(str) { output += str; }}));
148+
149+
fs.writeFile('some/path/junit.xml', output, cb);
150+
});
151+
```
152+
153+
This functionality is only available when not using custom reporters.
154+
105155
## Custom rules
106156
107157
Use the `csslint.addRule(rule)` method to define custom rules that run in addition to the rules defined in the csslintrc file. See [Working with Rules](https://github.com/CSSLint/csslint/wiki/Working-with-Rules) for details.

index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ var cssLintPlugin = function(options) {
7171
});
7272
};
7373

74-
cssLintPlugin.reporter = function(customReporter) {
74+
cssLintPlugin.reporter = function(customReporter, options) {
7575
var reporter = csslint.getFormatter('text');
7676
var builtInReporter = true;
7777
var output;
7878

79+
options = options || {};
80+
81+
var logger = options.logger || process.stdout.write.bind(process.stdout);
82+
7983
if (typeof customReporter === 'function') {
8084
reporter = customReporter;
8185
builtInReporter = false;
@@ -101,7 +105,7 @@ cssLintPlugin.reporter = function(customReporter) {
101105
// Only report if CSSLint was ran and errors were found
102106
if (file.csslint && !file.csslint.success) {
103107
if (builtInReporter) {
104-
output.push(reporter.formatResults(file.csslint.originalReport, file.path));
108+
output.push(reporter.formatResults(file.csslint.originalReport, file.path, options));
105109
}
106110
else {
107111
reporter(file);
@@ -111,14 +115,14 @@ cssLintPlugin.reporter = function(customReporter) {
111115
return cb(null, file);
112116
},
113117
function(cb) {
114-
var report;
115-
116118
if (builtInReporter) {
117119
output.push(reporter.endFormat());
118-
report = output.join('');
119120

120-
// Only print report if the report is not empty
121-
report && gutil.log(report);
121+
output
122+
.filter(function(str) {
123+
return str;
124+
})
125+
.forEach(logger);
122126
}
123127

124128
return cb();

test/expected/checkstyle-xml.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?xml version="1.0" encoding="utf-8"?><checkstyle><file name="test/fixtures/usingImportant.css"><error line="2" column="1" severity="warning" message="Bad naming: .mybox" source="net.csslint.OOCSS"/><error line="3" column="3" severity="warning" message="Use of !important" source="net.csslint.Disallow!important"/></file></checkstyle>
1+
<file name="test/fixtures/usingImportant.css"><error line="2" column="1" severity="warning" message="Bad naming: .mybox" source="net.csslint.OOCSS"/><error line="3" column="3" severity="warning" message="Use of !important" source="net.csslint.Disallow!important"/></file>

test/main.js

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var getFile = function(filePath) {
1919

2020
var getContents = function(filePath) {
2121
filePath = 'test/' + filePath;
22-
return fs.readFileSync(filePath, 'utf8');
22+
return fs.readFileSync(filePath, 'utf8').trim();
2323
};
2424

2525
describe('gulp-csslint', function() {
@@ -276,16 +276,15 @@ describe('gulp-csslint', function() {
276276
});
277277

278278
describe('cssLintPlugin.reporter()', function() {
279-
it('should support built-in CSSLint formatters', sinon.test(function(done) {
279+
it('should support built-in CSSLint formatters', function(done) {
280280
var a = 0;
281281

282282
var file = getFile('fixtures/usingImportant.css');
283283
var expected = getContents('expected/checkstyle-xml.xml');
284+
var callback = sinon.spy();
284285

285286
var lintStream = cssLintPlugin();
286-
var reporterStream = cssLintPlugin.reporter('checkstyle-xml');
287-
288-
sinon.stub(gutil, 'log');
287+
var reporterStream = cssLintPlugin.reporter('checkstyle-xml', {logger: callback});
289288

290289
reporterStream.on('data', function() {
291290
++a;
@@ -299,27 +298,75 @@ describe('gulp-csslint', function() {
299298

300299
reporterStream.once('end', function() {
301300
a.should.equal(1);
302-
sinon.assert.calledOnce(gutil.log);
303-
sinon.assert.calledWith(gutil.log, expected);
301+
sinon.assert.calledThrice(callback);
302+
callback.firstCall.args[0].should.equal('<?xml version="1.0" encoding="utf-8"?><checkstyle>');
303+
callback.secondCall.args[0].should.equal(expected);
304+
callback.thirdCall.args[0].should.equal('</checkstyle>');
304305

305-
gutil.log.restore();
306306
done();
307307
});
308308

309309
lintStream.write(file);
310310
lintStream.end();
311-
}));
311+
});
312312

313-
it('should not print empty output by built-in CSSLint formatters', sinon.test(function(done) {
313+
it('should write report to disk', function(done) {
314314
var a = 0;
315+
var output = '';
315316

316-
var file = getFile('fixtures/validCSS.css');
317+
var file = getFile('fixtures/usingImportant.css');
318+
var expected = getContents('expected/checkstyle-xml.xml');
317319

318320
var lintStream = cssLintPlugin();
319-
var reporterStream = cssLintPlugin.reporter('text');
321+
var reporterStream = cssLintPlugin.reporter('checkstyle-xml', {
322+
logger: function(str) {
323+
output += str;
324+
}
325+
});
320326

321327
sinon.stub(gutil, 'log');
322328

329+
reporterStream.on('data', function() {
330+
++a;
331+
});
332+
lintStream.on('data', function(file) {
333+
reporterStream.write(file);
334+
});
335+
lintStream.once('end', function() {
336+
reporterStream.end();
337+
});
338+
339+
reporterStream.once('end', function() {
340+
fs.writeFile('test-output.xml', output, function() {
341+
a.should.equal(1);
342+
sinon.assert.notCalled(gutil.log);
343+
344+
gutil.log.restore();
345+
346+
fs.readFile('test-output.xml', function(err, content) {
347+
(err === null).should.be.true;
348+
349+
var toString = content.toString();
350+
351+
toString.should.equal('<?xml version="1.0" encoding="utf-8"?><checkstyle>' + expected + '</checkstyle>');
352+
done();
353+
});
354+
});
355+
});
356+
357+
lintStream.write(file);
358+
lintStream.end();
359+
});
360+
361+
it('should not print empty output by built-in CSSLint formatters', function(done) {
362+
var a = 0;
363+
364+
var file = getFile('fixtures/validCSS.css');
365+
var callback = sinon.spy();
366+
367+
var lintStream = cssLintPlugin();
368+
var reporterStream = cssLintPlugin.reporter('text', {logger: callback});
369+
323370
reporterStream.on('data', function() {
324371
++a;
325372
});
@@ -333,15 +380,14 @@ describe('gulp-csslint', function() {
333380
});
334381

335382
reporterStream.once('end', function() {
383+
sinon.assert.notCalled(callback);
336384
a.should.equal(1);
337-
sinon.assert.callCount(gutil.log, 0);
338385

339-
gutil.log.restore();
340386
done();
341387
});
342388

343389
lintStream.write(file);
344390
lintStream.end();
345-
}));
391+
});
346392
});
347393
});

0 commit comments

Comments
 (0)