-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
file-utils.spec.js
127 lines (106 loc) · 3.77 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 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 = process.platform !== 'win32';
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'),
tmpFile('mocha-utils.js')
).and('to 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 equal', []);
});
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('to contain', tmpFile('mocha-utils-link.js'));
expectedLength++;
}
ex.and('to have length', expectedLength);
});
it('should parse extensions from extensions 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 return only the ".js" file', function() {
var TsFile = tmpFile('mocha-utils.ts');
fs.writeFileSync(TsFile, 'yippy skippy ying yang yow');
var res = utils
.lookupFiles(tmpFile('mocha-utils'), ['js'], false)
.map(path.normalize.bind(path));
expect(res, 'to contain', tmpFile('mocha-utils.js')).and(
'to have length',
1
);
});
it('should return ".js" and ".ts" files', function() {
var TsFile = tmpFile('mocha-utils.ts');
fs.writeFileSync(TsFile, 'yippy skippy ying yang yow');
var res = utils
.lookupFiles(tmpFile('mocha-utils'), ['js', 'ts'], false)
.map(path.normalize.bind(path));
expect(
res,
'to contain',
tmpFile('mocha-utils.js'),
tmpFile('mocha-utils.ts')
).and('to have length', 2);
});
it('should require the extensions parameter when looking up a file', function() {
var dirLookup = function() {
return utils.lookupFiles(tmpFile('mocha-utils'), undefined, false);
};
expect(dirLookup, 'to throw', {
name: 'Error',
code: 'ERR_MOCHA_NO_FILES_MATCH_PATTERN'
});
});
it('should require the extensions parameter when looking up a directory', function() {
var dirLookup = function() {
return utils.lookupFiles(tmpDir, undefined, false);
};
expect(dirLookup, 'to throw', {
name: 'TypeError',
code: 'ERR_MOCHA_INVALID_ARG_TYPE',
argument: 'extensions'
});
});
});
afterEach(removeTempDir);
function makeTempDir() {
utils.mkdir(tmpDir);
}
function removeTempDir() {
rimraf.sync(tmpDir);
}
});