Skip to content

Commit

Permalink
resolves mochajs#1223: fix interaction with symbolic links
Browse files Browse the repository at this point in the history
- symbolic links are followed if they are not broken
- move `lookupFiles()` into `utils` module
- add some tests for the symbolic link stuff
- build
  • Loading branch information
Christopher Hiller authored and Joshua Appelman committed Sep 6, 2014
1 parent 340cb56 commit f0facd2
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 37 deletions.
38 changes: 1 addition & 37 deletions bin/_mocha
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ program.compilers.forEach(function(c) {
extensions.push(ext);
});

var re = new RegExp('\\.(' + extensions.join('|') + ')$');

// requires

requires.forEach(function(mod) {
Expand All @@ -302,7 +300,7 @@ var files = []
if (!args.length) args.push('test');

args.forEach(function(arg){
files = files.concat(lookupFiles(arg, program.recursive));
files = files.concat(utils.lookupFiles(arg, extensions, program.recursive));
});

// resolve
Expand Down Expand Up @@ -439,40 +437,6 @@ function stop() {
clearInterval(play.timer);
}

/**
* Lookup file names at the given `path`.
*/

function lookupFiles(path, recursive) {
var files = [];

if (!exists(path)) {
if (exists(path + '.js')) {
path += '.js'
} else {
files = glob.sync(path);
if (!files.length) throw new Error("cannot resolve path (or pattern) '" + path + "'");
return files;
}
}

var stat = fs.statSync(path);
if (stat.isFile()) return path;

fs.readdirSync(path).forEach(function(file){
file = join(path, file);
var stat = fs.statSync(file);
if (stat.isDirectory()) {
if (recursive) files = files.concat(lookupFiles(file, recursive));
return
}
if (!stat.isFile() || !re.test(file) || basename(file)[0] == '.') return;
files.push(file);
});

return files;
}

/**
* Play the given array of strings.
*/
Expand Down
49 changes: 49 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

var fs = require('fs')
, path = require('path')
, basename = path.basename
, exists = fs.existsSync || path.existsSync
, glob = require('glob')
, join = path.join
, debug = require('debug')('mocha:watch');

Expand Down Expand Up @@ -348,3 +351,49 @@ exports.canonicalize = function(obj, stack) {

return canonicalizedObj;
}

/**
* Lookup file names at the given `path`.
*/
exports.lookupFiles = function lookupFiles(path, extensions, recursive) {
var files = [];
var re = new RegExp('\\.(' + extensions.join('|') + ')$');

if (!exists(path)) {
if (exists(path + '.js')) {
path += '.js';
} else {
files = glob.sync(path);
if (!files.length) throw new Error("cannot resolve path (or pattern) '" + path + "'");
return files;
}
}

try {
var stat = fs.statSync(path);
if (stat.isFile()) return path;
}
catch (ignored) {
return;
}

fs.readdirSync(path).forEach(function(file){
file = join(path, file);
try {
var stat = fs.statSync(file);
if (stat.isDirectory()) {
if (recursive) {
files = files.concat(lookupFiles(file, recursive));
}
return;
}
}
catch (ignored) {
return;
}
if (!stat.isFile() || !re.test(file) || basename(file)[0] === '.') return;
files.push(file);
});

return files;
}
49 changes: 49 additions & 0 deletions mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -5498,6 +5498,9 @@ require.register("utils.js", function(module, exports, require){

var fs = require('browser/fs')
, path = require('browser/path')
, basename = path.basename
, exists = fs.existsSync || path.existsSync
, glob = require('glob')
, join = path.join
, debug = require('browser/debug')('mocha:watch');

Expand Down Expand Up @@ -5843,6 +5846,52 @@ exports.canonicalize = function(obj, stack) {
return canonicalizedObj;
}

/**
* Lookup file names at the given `path`.
*/
exports.lookupFiles = function lookupFiles(path, extensions, recursive) {
var files = [];
var re = new RegExp('\\.(' + extensions.join('|') + ')$');

if (!exists(path)) {
if (exists(path + '.js')) {
path += '.js';
} else {
files = glob.sync(path);
if (!files.length) throw new Error("cannot resolve path (or pattern) '" + path + "'");
return files;
}
}

try {
var stat = fs.statSync(path);
if (stat.isFile()) return path;
}
catch (ignored) {
return;
}

fs.readdirSync(path).forEach(function(file){
file = join(path, file);
try {
var stat = fs.statSync(file);
if (stat.isDirectory()) {
if (recursive) {
files = files.concat(lookupFiles(file, recursive));
}
return;
}
}
catch (ignored) {
return;
}
if (!stat.isFile() || !re.test(file) || basename(file)[0] === '.') return;
files.push(file);
});

return files;
}

}); // module: utils.js
// The global object is "self" in Web Workers.
var global = (function() { return this; })();
Expand Down
26 changes: 26 additions & 0 deletions test/acceptance/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,30 @@ describe('lib/utils', function () {
utils.stringify(travis).should.equal('{\n "name": "travis"\n "whoami": "[Circular]"\n}');
});
});

describe('lookupFiles', function () {
var fs = require('fs');

beforeEach(function () {
fs.writeFileSync('/tmp/mocha-utils.js', 'yippy skippy ying yang yow');
fs.symlinkSync('/tmp/mocha-utils.js', '/tmp/mocha-utils-link.js');
});

it('should not choke on symlinks', function () {
utils.lookupFiles('/tmp', ['js'], false).should.eql(['/tmp/mocha-utils-link.js', '/tmp/mocha-utils.js']);
fs.existsSync('/tmp/mocha-utils-link.js').should.be.true;
fs.rename('/tmp/mocha-utils.js', '/tmp/bob');
fs.existsSync('/tmp/mocha-utils-link.js').should.be.true;
utils.lookupFiles('/tmp', ['js'], false).should.eql([]);
});

afterEach(function () {
['/tmp/mocha-utils.js', '/tmp/mocha-utils-link.js', '/tmp/bob'].forEach(function (path) {
try {
fs.unlinkSync(path);
}
catch (ignored) {}
});
})
});
});

0 comments on commit f0facd2

Please sign in to comment.