Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new API method for failing build upon validation error #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var gutil = require('gulp-util'),
PJV = require('package-json-validator').PJV,
through = require('through2');

var PLUGIN_NAME = require('./package').name;

function printErrors(results) {
if (results) {

Expand Down Expand Up @@ -41,11 +43,11 @@ function printErrors(results) {
}

} else {
throw new gutil.PluginError('gulp-nice-package', 'Failed to get results from validator');
throw new gutil.PluginError(PLUGIN_NAME, 'Failed to get results from validator');
}
}

module.exports = function (spec, options) {
var nicePackagePlugin = function (spec, options) {

function validate(file, enc, cb) {

Expand All @@ -55,7 +57,7 @@ module.exports = function (spec, options) {
}

if (file.isStream()) {
this.emit('error', new gutil.PluginError('gulp-nice-package', 'Streaming not supported'));
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return cb();
}

Expand All @@ -70,4 +72,28 @@ module.exports = function (spec, options) {
}

return through.obj(validate);
};
};

nicePackagePlugin.failOnError = function () {
return through.obj(function (file, enc, cb) {
var error = null;

function size(arr) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this function to avoid adding a dependency on Underscore or Lodash.

return arr ? arr.length : 0;
}

if (file.nicePackage.valid === false) {
error = new gutil.PluginError(
PLUGIN_NAME,
'Failed with ' +
size(file.nicePackage.errors) + ' error(s), ' +
size(file.nicePackage.warnings) + ' warning(s), ' +
size(file.nicePackage.recommendations) + ' recommendation(s)'
);
}

return cb(error, file);
});
};

module.exports = nicePackagePlugin;
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ I know, it seems a bit terse. The reasoning is, you may want to pipe other trans
error is thrown, the pipe will cease and you have no way to continue. A valid use case may be to actually fix the
the package.json file programmatically if it's invalid, e.g. with [gulp-shrinkwrap](https://github.com/chmontgomery/gulp-shrinkwrap).

If you have no need to further process the stream after validation, you can pipe the stream to `validate.failOnError()`
to fail the build in the event one or more errors are raised during validation.

```js
// gulpfile.js
var gulp = require('gulp');
var validate = require('gulp-nice-package');

gulp.task('validate-json', function () {
return gulp.src('package.json')
.pipe(validate())
.pipe(validate.failOnError());
});
```

## License

[MIT](http://opensource.org/licenses/MIT) © [Chris Montgomery](http://www.chrismontgomery.info/)
Expand Down
61 changes: 55 additions & 6 deletions test/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ var gutil = require('gulp-util'),
validator = require('./../index.js'),
assert = require('assert');

function createPackageFile(path) {
return new gutil.File({
contents: new Buffer(JSON.stringify(require(path)))
});
}

function createInvalidPackageFile() {
return createPackageFile('./fixtures/package-with-errors.json');
}

function createValidPackageFile() {
return createPackageFile('./fixtures/package-valid.json');
}

it('should have invalid json file', function (cb) {
var stream = validator();

Expand All @@ -12,9 +26,7 @@ it('should have invalid json file', function (cb) {
cb();
});

stream.write(new gutil.File({
contents: new Buffer(JSON.stringify(require('./fixtures/package-with-errors.json')))
}));
stream.write(createInvalidPackageFile());
});

it('should have valid json file', function (cb) {
Expand All @@ -25,7 +37,44 @@ it('should have valid json file', function (cb) {
cb();
});

stream.write(new gutil.File({
contents: new Buffer(JSON.stringify(require('./fixtures/package-valid.json')))
}));
stream.write(createValidPackageFile());
});

describe('failOnError', function () {
it('should report an error when the json file is invalid', function (cb) {
var errorReported = false;
var stream = validator()
.on('end', function () {
assert.ok(errorReported);
cb();
});

stream
.pipe(validator.failOnError())
.on('error', function (error) {
errorReported = true;
assert.strictEqual(error.message, 'Failed with 1 error(s), 3 warning(s), 2 recommendation(s)');
});

stream.write(createInvalidPackageFile());
stream.end();
});

it('should not report an error when the json file is valid', function (cb) {
var errorReported = false;
var stream = validator()
.on('end', function () {
assert.ok(!errorReported);
cb();
});

stream
.pipe(validator.failOnError())
.on('error', function () {
errorReported = true;
});

stream.write(createValidPackageFile());
stream.end();
});
});