forked from mochajs/mocha
/
file-utils.spec.js
127 lines (103 loc) · 3.72 KB
/
file-utils.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'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 (ignore) {
// determine if file symlinks are supported
} finally {
removeTempDir();
}
})();
beforeEach(function() {
this.timeout(2000);
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() {
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'))
.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 parse extensions from extnsions parameter', function() {
var nonJsFile = tmpFile('mocha-utils-text.txt');
fs.writeFileSync(nonJsFile, 'yippy skippy ying yang yow');
var res = utils.lookupFiles(tmpDir, ['txt'], false);
expect(res)
.to.contain(nonJsFile)
.and.to.have.length(1);
});
it('should not require the extensions parameter when looking up a file', function() {
var res = utils.lookupFiles(tmpFile('mocha-utils'), undefined, false);
expect(res).to.equal(tmpFile('mocha-utils.js'));
});
it('should require the extensions parameter when looking up a directory', function() {
var dirLookup = function() {
return utils.lookupFiles(tmpDir, undefined, false);
};
expect(dirLookup).to.throwError();
});
});
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);
}
});