Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

Move re variable definition in lookupFiles Utility (#3082)

Ahmad Bamieh6fc2d9e90c4b
diff --git a/lib/utils.js b/lib/utils.js
index 2c67cdc6..a6a74540 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -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;
}
diff --git a/test/node-unit/file-utils.spec.js b/test/node-unit/file-utils.spec.js
index 3ff1bd4d..5d1c44f7 100644
--- a/test/node-unit/file-utils.spec.js
+++ b/test/node-unit/file-utils.spec.js
@@ -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 () {