Permalink
Browse files

Move re variable definition in lookupFiles Utility (#3082)

* move regexp definition in lookupFiles.

I am using mocha programmatically, along with its utility `lookupFiles`.

A regular expression must be evaluated to lookup files although it is only needed in one case where a glob is not provided. Shifting the `re` variable definition down to where it is needed will avoid this issue, also eliminate the requirement of always sending an array to the extensions parameter (although it might not be needed)

* add unit tests

remove commented file
  • Loading branch information...
1 parent 575d83d commit 6fc2d9e90c4b190811173da3e8db5f2cef45a6d4 @Bamieh Bamieh committed with boneskull Dec 13, 2017
Showing with 21 additions and 1 deletion.
  1. +1 −1 lib/utils.js
  2. +20 −0 test/node-unit/file-utils.spec.js
View
@@ -476,7 +476,6 @@ exports.canonicalize = function canonicalize (value, stack, typeHint) {
*/
exports.lookupFiles = function lookupFiles (path, extensions, recursive) {
var files = [];
- var re = new RegExp('\\.(' + extensions.join('|') + ')$');
if (!exists(path)) {
if (exists(path + '.js')) {
@@ -514,6 +513,7 @@ exports.lookupFiles = function lookupFiles (path, extensions, recursive) {
// ignore error
return;
}
+ var re = new RegExp('\\.(?:' + extensions.join('|') + ')$');
if (!stat.isFile() || !re.test(file) || basename(file)[0] === '.') {
return;
}
@@ -82,6 +82,26 @@ describe('file utils', function () {
.have
.length(expectedLength);
});
+
+ it('should parse extensions from extnsions parameter', function () {
+ var nonJsFile = tmpFile('mocha-utils-text.txt');
+ fs.writeFileSync(nonJsFile, 'yippy skippy ying yang yow');
+
+ var res = utils.lookupFiles(tmpDir, ['txt'], false);
+ expect(res).to.contain(nonJsFile).and.to.have.length(1);
+ });
+
+ it('should not require the extensions parameter when looking up a file', function () {
+ var res = utils.lookupFiles(tmpFile('mocha-utils'), undefined, false);
+ expect(res).to.equal(tmpFile('mocha-utils.js'));
+ });
+
+ it('should require the extensions parameter when looking up a directory', function () {
+ var dirLookup = function () {
+ return utils.lookupFiles(tmpDir, undefined, false);
+ };
+ expect(dirLookup).to.throwError();
+ });
});
describe('.files', function () {

0 comments on commit 6fc2d9e

Please sign in to comment.