Skip to content

Commit

Permalink
fix(loadFilesFromDir): validating options.cwd
Browse files Browse the repository at this point in the history
checking if options.cwd is a valid string.
  • Loading branch information
ar4mirez committed Feb 7, 2017
1 parent e387964 commit 904e000
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

const Glob = require('glob');
const Hoek = require('hoek');
const Joi = require('joi');
const _ = require('lodash');

const internals = {
options: {
cwd: process.cwd()
}
options: {}
};

exports.loadFilesFromDir = (pattern, options) => {

const validation = Joi.validate(options, Joi.object().keys({
cwd: Joi.string()
}));

if (validation.error) {
throw new Error(validation.error.annotate());
}

const files = Glob.sync(pattern, Hoek.applyToDefaults(internals.options, options));
return _.map(files, (file) => (`${options.cwd}/${file}`));
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
}
},
"dependencies": {
"joi": "10.2.1"
}
}
15 changes: 14 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@ Lab.describe(`${Lab.pkg.name}:utils`, () => {
return done();
});

Lab.it('should use fail if not cwd were passed.', (done) => {
Lab.it('should return an empty array if not files were found.', (done) => {

const files = Utils.loadFilesFromDir('*.js');

Lab.expect(files).to.exist();
Lab.expect(files).to.be.an.array();
Lab.expect(files).to.have.length(0);

return done();
});

Lab.it('should fail if an invalid cwd option were passed.', (done) => {

Lab.expect(() => Utils.loadFilesFromDir('*.js', { cwd: 123 })).to.throw(Error);

return done();
});
Expand Down

0 comments on commit 904e000

Please sign in to comment.