Skip to content

armgit / mocha

Public Imported from GitHub
0

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

remove unused functionality in utils module

Christopher Hillerec8901a23c51
diff --git a/lib/utils.js b/lib/utils.js
index a6a74540..a32e0a49 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -1,29 +1,15 @@
'use strict';
-/* eslint-env browser */
-
/**
* Module dependencies.
*/
-var basename = require('path').basename;
var debug = require('debug')('mocha:watch');
-var exists = require('fs').existsSync;
+var fs = require('fs');
var glob = require('glob');
var path = require('path');
-var join = path.join;
-var readdirSync = require('fs').readdirSync;
-var statSync = require('fs').statSync;
-var watchFile = require('fs').watchFile;
-var lstatSync = require('fs').lstatSync;
var he = require('he');
-/**
- * Ignored directories.
- */
-
-var ignore = ['node_modules', '.git'];
-
exports.inherits = require('util').inherits;
/**
@@ -60,7 +46,7 @@ exports.watch = function (files, fn) {
var options = { interval: 100 };
files.forEach(function (file) {
debug('file %s', file);
- watchFile(file, options, function (curr, prev) {
+ fs.watchFile(file, options, function (curr, prev) {
if (prev.mtime < curr.mtime) {
fn(file);
}
@@ -68,46 +54,6 @@ 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('|') + ')$');
-
- readdirSync(dir)
- .filter(ignored)
- .forEach(function (path) {
- path = join(dir, path);
- if (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`.
*
@@ -469,40 +415,40 @@ exports.canonicalize = function canonicalize (value, stack, typeHint) {
* Lookup file names at the given `path`.
*
* @api public
- * @param {string} path Base path to start searching from.
+ * @param {string} filepath Base path to start searching from.
* @param {string[]} extensions File extensions to look for.
* @param {boolean} recursive Whether or not to recurse into subdirectories.
* @return {string[]} An array of paths.
*/
-exports.lookupFiles = function lookupFiles (path, extensions, recursive) {
+exports.lookupFiles = function lookupFiles (filepath, extensions, recursive) {
var files = [];
- if (!exists(path)) {
- if (exists(path + '.js')) {
- path += '.js';
+ if (!fs.existsSync(filepath)) {
+ if (fs.existsSync(filepath + '.js')) {
+ filepath += '.js';
} else {
- files = glob.sync(path);
+ files = glob.sync(filepath);
if (!files.length) {
- throw new Error("cannot resolve path (or pattern) '" + path + "'");
+ throw new Error("cannot resolve path (or pattern) '" + filepath + "'");
}
return files;
}
}
try {
- var stat = statSync(path);
+ var stat = fs.statSync(filepath);
if (stat.isFile()) {
- return path;
+ return filepath;
}
} catch (err) {
// ignore error
return;
}
- readdirSync(path).forEach(function (file) {
- file = join(path, file);
+ fs.readdirSync(filepath).forEach(function (file) {
+ file = path.join(filepath, file);
try {
- var stat = statSync(file);
+ var stat = fs.statSync(file);
if (stat.isDirectory()) {
if (recursive) {
files = files.concat(lookupFiles(file, extensions, recursive));
@@ -514,7 +460,7 @@ exports.lookupFiles = function lookupFiles (path, extensions, recursive) {
return;
}
var re = new RegExp('\\.(?:' + extensions.join('|') + ')$');
- if (!stat.isFile() || !re.test(file) || basename(file)[0] === '.') {
+ if (!stat.isFile() || !re.test(file) || path.basename(file)[0] === '.') {
return;
}
files.push(file);
diff --git a/test/node-unit/file-utils.spec.js b/test/node-unit/file-utils.spec.js
index f425468a..e3c4f4a4 100644
--- a/test/node-unit/file-utils.spec.js
+++ b/test/node-unit/file-utils.spec.js
@@ -38,7 +38,11 @@ describe('file utils', function () {
});
describe('.lookupFiles', function () {
- (symlinkSupported ? it : it.skip)('should not return broken symlink file path', function () {
+ it('should not return broken symlink file path', function () {
+ if (!symlinkSupported) {
+ return this.skip();
+ }
+
expect(utils.lookupFiles(tmpDir, ['js'], false))
.to
.contain(tmpFile('mocha-utils-link.js'))
@@ -101,26 +105,6 @@ 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 () {