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

restore removed methods which still used #3268

Merged
merged 1 commit into from Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 47 additions & 0 deletions lib/utils.js
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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`.
*
Expand Down
20 changes: 20 additions & 0 deletions test/node-unit/file-utils.spec.js
Expand Up @@ -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 () {
Expand Down