Skip to content

Commit 4a2e85a

Browse files
villesauboneskull
authored andcommitted
ignore files files which are not found by files() (#2538)
* rename lookup-files.spec.js to file-utils.spec.js * .files should not choke on broken symlinks
1 parent 79d7414 commit 4a2e85a

File tree

4 files changed

+115
-92
lines changed

4 files changed

+115
-92
lines changed

karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function (config) {
2323
exclude: [
2424
'test/acceptance/http.spec.js',
2525
'test/acceptance/fs.spec.js',
26-
'test/acceptance/lookup-files.spec.js',
26+
'test/acceptance/file-utils.spec.js',
2727
'test/acceptance/require/**/*.js',
2828
'test/acceptance/misc/**/*.js'
2929
],

lib/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var join = path.join;
1616
var readdirSync = require('fs').readdirSync;
1717
var statSync = require('fs').statSync;
1818
var watchFile = require('fs').watchFile;
19+
var lstatSync = require('fs').lstatSync;
1920
var toISOString = require('./to-iso-string');
2021

2122
/**
@@ -252,7 +253,7 @@ exports.files = function (dir, ext, ret) {
252253
.filter(ignored)
253254
.forEach(function (path) {
254255
path = join(dir, path);
255-
if (statSync(path).isDirectory()) {
256+
if (lstatSync(path).isDirectory()) {
256257
exports.files(path, ext, ret);
257258
} else if (path.match(re)) {
258259
ret.push(path);

test/acceptance/file-utils.spec.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
'use strict';
2+
3+
var utils = require('../../lib/utils');
4+
var fs = require('fs');
5+
var path = require('path');
6+
var os = require('os');
7+
var mkdirp = require('mkdirp');
8+
var rimraf = require('rimraf');
9+
10+
describe('file utils', function () {
11+
var tmpDir = path.join(os.tmpDir(), 'mocha-file-lookup');
12+
var existsSync = fs.existsSync;
13+
var tmpFile = path.join.bind(path, tmpDir);
14+
var symlinkSupported = false;
15+
16+
(function testSymlinkSupport () {
17+
makeTempDir();
18+
19+
fs.writeFileSync(tmpFile('mocha-utils.js'), 'yippy skippy ying yang yow');
20+
try {
21+
fs.symlinkSync(tmpFile('mocha-utils.js'), tmpFile('mocha-utils-link.js'));
22+
symlinkSupported = true;
23+
} catch (ignored) {
24+
// ignored
25+
} finally {
26+
removeTempDir();
27+
}
28+
}());
29+
30+
beforeEach(function () {
31+
makeTempDir();
32+
33+
fs.writeFileSync(tmpFile('mocha-utils.js'), 'yippy skippy ying yang yow');
34+
if (symlinkSupported) {
35+
fs.symlinkSync(tmpFile('mocha-utils.js'), tmpFile('mocha-utils-link.js'));
36+
}
37+
});
38+
39+
describe('.lookupFiles', function () {
40+
(symlinkSupported ? it : it.skip)('should not return broken symlink file path', function () {
41+
expect(utils.lookupFiles(tmpDir, ['js'], false))
42+
.to
43+
.contain(tmpFile('mocha-utils-link.js'))
44+
.and
45+
.contain(tmpFile('mocha-utils.js'))
46+
.and
47+
.have
48+
.length(2);
49+
expect(existsSync(tmpFile('mocha-utils-link.js')))
50+
.to
51+
.be(true);
52+
fs.renameSync(tmpFile('mocha-utils.js'), tmpFile('bob'));
53+
expect(existsSync(tmpFile('mocha-utils-link.js')))
54+
.to
55+
.be(false);
56+
expect(utils.lookupFiles(tmpDir, ['js'], false))
57+
.to
58+
.eql([]);
59+
});
60+
61+
it('should accept a glob "path" value', function () {
62+
var res = utils.lookupFiles(tmpFile('mocha-utils*'), ['js'], false)
63+
.map(path.normalize.bind(path));
64+
65+
var expectedLength = 0;
66+
var ex = expect(res)
67+
.to
68+
.contain(tmpFile('mocha-utils.js'));
69+
expectedLength++;
70+
71+
if (symlinkSupported) {
72+
ex = ex.and
73+
.contain(tmpFile('mocha-utils-link.js'));
74+
expectedLength++;
75+
}
76+
77+
ex.and
78+
.have
79+
.length(expectedLength);
80+
});
81+
});
82+
83+
describe('.files', function () {
84+
(symlinkSupported ? it : it.skip)('should return broken symlink file path', function () {
85+
expect(utils.files(tmpDir, ['js']))
86+
.to.contain(tmpFile('mocha-utils-link.js'))
87+
.and.contain(tmpFile('mocha-utils.js'))
88+
.and.have.length(2);
89+
90+
expect(existsSync(tmpFile('mocha-utils-link.js')))
91+
.to.be(true);
92+
93+
fs.renameSync(tmpFile('mocha-utils.js'), tmpFile('bob'));
94+
95+
expect(existsSync(tmpFile('mocha-utils-link.js')))
96+
.to.be(false);
97+
98+
expect(utils.files(tmpDir, ['js']))
99+
.to.eql([tmpFile('mocha-utils-link.js')]);
100+
});
101+
});
102+
103+
afterEach(removeTempDir);
104+
105+
function makeTempDir () {
106+
mkdirp.sync(tmpDir);
107+
}
108+
109+
function removeTempDir () {
110+
rimraf.sync(tmpDir);
111+
}
112+
});

test/acceptance/lookup-files.spec.js

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)