Skip to content

Commit

Permalink
ignore files files which are not found by files() (#2538)
Browse files Browse the repository at this point in the history
* rename lookup-files.spec.js to file-utils.spec.js

* .files should not choke on broken symlinks
  • Loading branch information
villesau authored and boneskull committed Nov 24, 2016
1 parent 79d7414 commit 4a2e85a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 92 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function (config) {
exclude: [
'test/acceptance/http.spec.js',
'test/acceptance/fs.spec.js',
'test/acceptance/lookup-files.spec.js',
'test/acceptance/file-utils.spec.js',
'test/acceptance/require/**/*.js',
'test/acceptance/misc/**/*.js'
],
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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 toISOString = require('./to-iso-string');

/**
Expand Down Expand Up @@ -252,7 +253,7 @@ 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)) {
ret.push(path);
Expand Down
112 changes: 112 additions & 0 deletions test/acceptance/file-utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
'use strict';

var utils = require('../../lib/utils');
var fs = require('fs');
var path = require('path');
var os = require('os');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');

describe('file utils', function () {
var tmpDir = path.join(os.tmpDir(), 'mocha-file-lookup');
var existsSync = fs.existsSync;
var tmpFile = path.join.bind(path, tmpDir);
var symlinkSupported = false;

(function testSymlinkSupport () {
makeTempDir();

fs.writeFileSync(tmpFile('mocha-utils.js'), 'yippy skippy ying yang yow');
try {
fs.symlinkSync(tmpFile('mocha-utils.js'), tmpFile('mocha-utils-link.js'));
symlinkSupported = true;
} catch (ignored) {
// ignored
} finally {
removeTempDir();
}
}());

beforeEach(function () {
makeTempDir();

fs.writeFileSync(tmpFile('mocha-utils.js'), 'yippy skippy ying yang yow');
if (symlinkSupported) {
fs.symlinkSync(tmpFile('mocha-utils.js'), tmpFile('mocha-utils-link.js'));
}
});

describe('.lookupFiles', function () {
(symlinkSupported ? it : it.skip)('should not return broken symlink file path', 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);
});
});

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 () {
mkdirp.sync(tmpDir);
}

function removeTempDir () {
rimraf.sync(tmpDir);
}
});
90 changes: 0 additions & 90 deletions test/acceptance/lookup-files.spec.js

This file was deleted.

0 comments on commit 4a2e85a

Please sign in to comment.