-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
304 lines (252 loc) · 8.15 KB
/
index.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var path = require('path');
var Module = require('module');
var expect = require('expect');
var rechoir = require('../');
var extension = require('../lib/extension');
var normalize = require('../lib/normalize');
var register = require('../lib/register');
// save the original Module._extensions
var originalExtensions = Object.keys(Module._extensions);
var original = originalExtensions.reduce(function (result, key) {
result[key] = require.extensions[key];
return result;
}, {});
// save the original cache keys
var originalCacheKeys = Object.keys(require.cache);
function cleanupCache(key) {
if (originalCacheKeys.indexOf(key) === -1) {
delete require.cache[key];
}
}
function cleanupExtensions(ext) {
if (originalExtensions.indexOf(ext) === -1) {
delete Module._extensions[ext];
} else {
Module._extensions[ext] = original[ext];
}
}
function cleanup(done) {
// restore the require.cache to startup state
Object.keys(require.cache).forEach(cleanupCache);
// restore the original Module._extensions
Object.keys(Module._extensions).forEach(cleanupExtensions);
done();
}
describe('rechoir', function () {
describe('extension', function () {
it('should extract extension from filename/path from the first dot', function (done) {
expect(extension('file.js')[0]).toEqual('.js');
expect(extension('file.tmp.dot.js')[0]).toEqual('.tmp.dot.js');
expect(extension('file.tmp.dot.js')[1]).toEqual('.dot.js');
expect(extension('file.tmp.dot.js')[2]).toEqual('.js');
expect(extension('relative/path/to/file.js')[0]).toEqual('.js');
expect(extension('relative/path/to/file.dot.js')[0]).toEqual('.dot.js');
expect(extension('relative/path/to/file.dot.js')[1]).toEqual('.js');
expect(extension('relative/path.with.dot/to/file.dot.js')[0]).toEqual(
'.dot.js'
);
expect(extension('relative/path.with.dot/to/file.dot.js')[1]).toEqual(
'.js'
);
done();
});
it('does not match any if the path ends in a dot', function (done) {
expect(extension('file.js.')).toEqual(undefined);
done();
});
it('treats additional dots as a separate extension', function (done) {
// Double
expect(extension('file.babel..js')).toEqual([
'.babel..js',
'..js',
'.js',
]);
expect(extension('file..babel.js')).toEqual([
'..babel.js',
'.babel.js',
'.js',
]);
// Triple
expect(extension('file.babel...js')).toEqual([
'.babel...js',
'...js',
'..js',
'.js',
]);
expect(extension('file...babel.js')).toEqual([
'...babel.js',
'..babel.js',
'.babel.js',
'.js',
]);
done();
});
it('does not consider a leading dot to be an extension', function (done) {
expect(extension('.config')).toEqual(undefined);
done();
});
});
describe('normalize', function () {
it('should convert a string input into array/object format', function (done) {
expect(normalize('foo')).toEqual({ module: 'foo' });
done();
});
it('should convert object input into array format', function (done) {
var input = {
module: 'foo',
};
expect(normalize(input)).toEqual(input);
done();
});
it('should iterate an array, normalizing each item', function (done) {
var input = [{ module: 'foo' }, 'bar'];
expect(normalize(input)).toEqual([{ module: 'foo' }, { module: 'bar' }]);
done();
});
});
describe('register', function () {
it('should return the specified module relative to the provided cwd', function (done) {
expect(register(__dirname, 'expect')).toEqual(expect);
done();
});
it('should call a register function if provided, passing in the module', function (done) {
register(__dirname, 'expect', function (attempt) {
expect(attempt).toEqual(expect);
});
done();
});
it('should return an error if the specified module cannot be registered', function (done) {
expect(register(__dirname, 'whatev')).toBeInstanceOf(Error);
done();
});
});
describe('prepare', function () {
var testFilePath = path.join(__dirname, 'fixtures', 'test.stub');
beforeEach(cleanup);
it('should throw if extension is unknown', function (done) {
expect(function () {
rechoir.prepare({}, './test/fixtures/test.whatever');
}).toThrow(/No module loader found for/);
done();
});
it('should return undefined if an unknown extension is specified when nothrow is enabled', function (done) {
expect(
rechoir.prepare({}, './test/fixtures/.testrc', null, true)
).toEqual(undefined);
done();
});
it('should throw if a module loader cannot be found or loaded', function (done) {
expect(function () {
rechoir.prepare(
{
'.stub': ['nothere'],
},
testFilePath
);
require(testFilePath);
}).toThrow();
done();
});
describe('all module loaders that were attempted failed to load', function () {
var exts = {
'.stub': ['nothere', 'orhere'],
};
// Check the failure entries in the thrown or returned error object.
function checkFailures(e) {
expect(e.failures).toBeInstanceOf(Array);
expect(e.failures[0].error).toBeInstanceOf(Error);
expect(e.failures[0].moduleName).toEqual('nothere');
expect(e.failures[0].module).toEqual(null);
expect(e.failures[1].error).toBeInstanceOf(Error);
expect(e.failures[1].moduleName).toEqual('orhere');
expect(e.failures[1].module).toEqual(null);
}
it('should throw error listing each module loader that was attempted when nothrow = false', function (done) {
var err;
try {
rechoir.prepare(exts, testFilePath);
} catch (e) {
err = e;
checkFailures(e);
}
expect(err).toBeInstanceOf(Error);
done();
});
it('should return error listing each module loader that was attempted when nothrow = true', function (done) {
checkFailures(rechoir.prepare(exts, testFilePath, null, true));
done();
});
});
it('should register a module loader for the specified extension', function (done) {
rechoir.prepare(
{
'.stub': ['nothere', '../require-stub'],
},
testFilePath
);
expect(function () {
require(testFilePath);
}).not.toThrow(Error);
done();
});
it('should return true if the module loader for the specified extension is already available', function (done) {
rechoir.prepare(
{
'.stub': ['nothere', '../require-stub'],
},
testFilePath
);
expect(
rechoir.prepare(
{
'.stub': ['nothere', '../require-stub'],
},
testFilePath
)
).toEqual(true);
done();
});
it('must not fail on folders with dots', function (done) {
rechoir.prepare(
{ '.stub': '../../require-stub' },
'./test/fixtures/folder.with.dots/test.stub'
);
expect(require('./fixtures/folder.with.dots/test.stub')).toEqual({
data: {
trueKey: true,
falseKey: false,
subKey: {
subProp: 1,
},
},
});
done();
});
it('should register a module loader even if the extension is single character (issue #38)', function (done) {
var fpath = path.join(__dirname, 'fixtures', 'test.s');
rechoir.prepare(
{
'.s': ['nothere', '../require-stub'],
},
fpath
);
expect(function () {
require(fpath);
}).not.toThrow(Error);
done();
});
it('should register a module loader for the specified extension with cwd', function (done) {
rechoir.prepare(
{
'.foo': ['nothere', '../require-stub'],
},
path.join('cwd', 'test.foo'),
path.join(__dirname, 'fixtures')
);
expect(function () {
require(testFilePath);
}).not.toThrow(Error);
done();
});
});
});