Skip to content

Commit

Permalink
remove unused functionality in utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Mar 1, 2018
1 parent f71f347 commit ec8901a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 90 deletions.
84 changes: 15 additions & 69 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -60,54 +46,14 @@ 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);
}
});
});
};

/**
* 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`.
*
Expand Down Expand Up @@ -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));
Expand All @@ -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);
Expand Down
26 changes: 5 additions & 21 deletions test/node-unit/file-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit ec8901a

Please sign in to comment.