Skip to content

Commit

Permalink
.files should not choke on broken symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ville Saukkonen committed Nov 15, 2016
1 parent 5bb938c commit 20f56ca
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 39 deletions.
6 changes: 4 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ 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 existsSync = require('fs').existsSync;
var toISOString = require('./to-iso-string');

/**
Expand Down Expand Up @@ -252,9 +254,9 @@ exports.files = function (dir, ext, ret) {
.filter(ignored)
.forEach(function (path) {
path = join(dir, path);
if (statSync(path).isDirectory()) {
if (lstatSync(path).isDirectory()) {
exports.files(path, ext, ret);
} else if (path.match(re)) {
} else if (path.match(re) && existsSync(path)) {
ret.push(path);
}
});
Expand Down
96 changes: 59 additions & 37 deletions test/acceptance/file-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var os = require('os');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');

describe('lookupFiles', function () {
var tmpDir = path.join(os.tmpDir(), 'mocha-lookup-files');
describe('file utils', function () {
var tmpDir = path.join(os.tmpDir(), 'mocha-file-test-folder');
var existsSync = fs.existsSync;
var tmpFile = path.join.bind(path, tmpDir);
var symlinkSupported = false;
Expand Down Expand Up @@ -36,46 +36,68 @@ describe('lookupFiles', function () {
}
});

(symlinkSupported ? it : it.skip)('should not choke on symlinks', function () {
expect(utils.lookupFiles(tmpDir, ['js'], false))
.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.lookupFiles(tmpDir, ['js'], false))
.to
.eql([]);
describe('.lookupFiles', function () {
(symlinkSupported ? it : it.skip)('lookupFiles should not choke on broken symlinks', function () {
expect(utils.lookupFiles(tmpDir, ['js'], false))
.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.lookupFiles(tmpDir, ['js'], false))
.to
.eql([]);
});

it('should accept a glob "path" value', function () {
var res = utils.lookupFiles(tmpFile('mocha-utils*'), ['js'], false)
.map(path.normalize.bind(path));

var expectedLength = 0;
var ex = expect(res)
.to
.contain(tmpFile('mocha-utils.js'));
expectedLength++;

if (symlinkSupported) {
ex = ex.and
.contain(tmpFile('mocha-utils-link.js'));
expectedLength++;
}

ex.and
.have
.length(expectedLength);
});
});

it('should accept a glob "path" value', function () {
var res = utils.lookupFiles(tmpFile('mocha-utils*'), ['js'], false)
.map(path.normalize.bind(path));
describe('.files', function () {
(symlinkSupported ? it : it.skip)('should not choke on broken symlinks', function () {
expect(utils.files(tmpDir, ['js']))
.to.contain(tmpFile('mocha-utils-link.js'))
.and.contain(tmpFile('mocha-utils.js'))
.and.have.length(2);

var expectedLength = 0;
var ex = expect(res)
.to
.contain(tmpFile('mocha-utils.js'));
expectedLength++;
expect(existsSync(tmpFile('mocha-utils-link.js')))
.to.be(true);

if (symlinkSupported) {
ex = ex.and
.contain(tmpFile('mocha-utils-link.js'));
expectedLength++;
}
fs.renameSync(tmpFile('mocha-utils.js'), tmpFile('bob'));

expect(existsSync(tmpFile('mocha-utils-link.js')))
.to.be(false);

ex.and
.have
.length(expectedLength);
expect(utils.files(tmpDir, ['js']))
.to.eql([]);
});
});

afterEach(removeTempDir);
Expand Down

0 comments on commit 20f56ca

Please sign in to comment.