From ec8901a23c5194b6f7e6eee9c2568e5020c944ce Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Wed, 28 Feb 2018 21:47:26 -0800 Subject: [PATCH] remove unused functionality in utils module --- lib/utils.js | 84 ++++++------------------------- test/node-unit/file-utils.spec.js | 26 ++-------- 2 files changed, 20 insertions(+), 90 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index a6a74540d9..a32e0a4917 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 f425468a26..e3c4f4a4ca 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 () {