From eb094216bc022efd0557316b6615bda392613443 Mon Sep 17 00:00:00 2001 From: Outsider Date: Wed, 7 Mar 2018 13:55:17 +0900 Subject: [PATCH] restore removed methods which still used It removed from --- lib/utils.js | 47 +++++++++++++++++++++++++++++++ test/node-unit/file-utils.spec.js | 20 +++++++++++++ 2 files changed, 67 insertions(+) diff --git a/lib/utils.js b/lib/utils.js index a308490aac..60151b66d4 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -8,8 +8,15 @@ var debug = require('debug')('mocha:watch'); var fs = require('fs'); var glob = require('glob'); var path = require('path'); +var join = path.join; var he = require('he'); +/** + * Ignored directories. + */ + +var ignore = ['node_modules', '.git']; + exports.inherits = require('util').inherits; /** @@ -54,6 +61,46 @@ exports.watch = function (files, fn) { }); }; +/** + * Ignored files. + * + * @api private + * @param {string} path + * @return {boolean} + */ +function ignored (path) { + return !~ignore.indexOf(path); +} + +/** + * Lookup files in the given `dir`. + * + * @api private + * @param {string} dir + * @param {string[]} [ext=['.js']] + * @param {Array} [ret=[]] + * @return {Array} + */ +exports.files = function (dir, ext, ret) { + ret = ret || []; + ext = ext || ['js']; + + var re = new RegExp('\\.(' + ext.join('|') + ')$'); + + fs.readdirSync(dir) + .filter(ignored) + .forEach(function (path) { + path = join(dir, path); + if (fs.lstatSync(path).isDirectory()) { + exports.files(path, ext, ret); + } else if (path.match(re)) { + ret.push(path); + } + }); + + return ret; +}; + /** * Compute a slug from the given `str`. * diff --git a/test/node-unit/file-utils.spec.js b/test/node-unit/file-utils.spec.js index e3c4f4a4ca..1ee06724db 100644 --- a/test/node-unit/file-utils.spec.js +++ b/test/node-unit/file-utils.spec.js @@ -105,6 +105,26 @@ describe('file utils', function () { }); }); + describe('.files', function () { + (symlinkSupported ? it : it.skip)('should return broken symlink file path', function () { + expect(utils.files(tmpDir, ['js'])) + .to.contain(tmpFile('mocha-utils-link.js')) + .and.contain(tmpFile('mocha-utils.js')) + .and.have.length(2); + + expect(existsSync(tmpFile('mocha-utils-link.js'))) + .to.be(true); + + fs.renameSync(tmpFile('mocha-utils.js'), tmpFile('bob')); + + expect(existsSync(tmpFile('mocha-utils-link.js'))) + .to.be(false); + + expect(utils.files(tmpDir, ['js'])) + .to.eql([tmpFile('mocha-utils-link.js')]); + }); + }); + afterEach(removeTempDir); function makeTempDir () {