1 | var path = require('path');
|
2 | var test = require('tape');
|
3 | var resolve = require('../');
|
4 |
|
5 | test('async foo', function (t) {
|
6 | t.plan(12);
|
7 | var dir = path.join(__dirname, 'resolver');
|
8 |
|
9 | resolve('./foo', { basedir: dir }, function (err, res, pkg) {
|
10 | if (err) t.fail(err);
|
11 | t.equal(res, path.join(dir, 'foo.js'));
|
12 | t.equal(pkg && pkg.name, 'resolve');
|
13 | });
|
14 |
|
15 | resolve('./foo.js', { basedir: dir }, function (err, res, pkg) {
|
16 | if (err) t.fail(err);
|
17 | t.equal(res, path.join(dir, 'foo.js'));
|
18 | t.equal(pkg && pkg.name, 'resolve');
|
19 | });
|
20 |
|
21 | resolve('./foo', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
|
22 | if (err) t.fail(err);
|
23 | t.equal(res, path.join(dir, 'foo.js'));
|
24 | t.equal(pkg && pkg.main, 'resolver');
|
25 | });
|
26 |
|
27 | resolve('./foo.js', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
|
28 | if (err) t.fail(err);
|
29 | t.equal(res, path.join(dir, 'foo.js'));
|
30 | t.equal(pkg.main, 'resolver');
|
31 | });
|
32 |
|
33 | resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
|
34 | if (err) t.fail(err);
|
35 | t.equal(res, path.join(dir, 'foo.js'));
|
36 | });
|
37 |
|
38 | resolve('foo', { basedir: dir }, function (err) {
|
39 | t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
|
40 | t.equal(err.code, 'MODULE_NOT_FOUND');
|
41 | });
|
42 |
|
43 |
|
44 | resolve('foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err) {
|
45 | t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
|
46 | });
|
47 | });
|
48 |
|
49 | test('bar', function (t) {
|
50 | t.plan(6);
|
51 | var dir = path.join(__dirname, 'resolver');
|
52 |
|
53 | resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
|
54 | if (err) t.fail(err);
|
55 | t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
|
56 | t.equal(pkg, undefined);
|
57 | });
|
58 |
|
59 | resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
|
60 | if (err) t.fail(err);
|
61 | t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
|
62 | t.equal(pkg, undefined);
|
63 | });
|
64 |
|
65 | resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
|
66 | if (err) t.fail(err);
|
67 | t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
|
68 | t.equal(pkg.main, 'bar');
|
69 | });
|
70 | });
|
71 |
|
72 | test('baz', function (t) {
|
73 | t.plan(4);
|
74 | var dir = path.join(__dirname, 'resolver');
|
75 |
|
76 | resolve('./baz', { basedir: dir }, function (err, res, pkg) {
|
77 | if (err) t.fail(err);
|
78 | t.equal(res, path.join(dir, 'baz/quux.js'));
|
79 | t.equal(pkg.main, 'quux.js');
|
80 | });
|
81 |
|
82 | resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
|
83 | if (err) t.fail(err);
|
84 | t.equal(res, path.join(dir, 'baz/quux.js'));
|
85 | t.equal(pkg.main, 'quux.js');
|
86 | });
|
87 | });
|
88 |
|
89 | test('biz', function (t) {
|
90 | t.plan(24);
|
91 | var dir = path.join(__dirname, 'resolver/biz/node_modules');
|
92 |
|
93 | resolve('./grux', { basedir: dir }, function (err, res, pkg) {
|
94 | if (err) t.fail(err);
|
95 | t.equal(res, path.join(dir, 'grux/index.js'));
|
96 | t.equal(pkg, undefined);
|
97 | });
|
98 |
|
99 | resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
|
100 | if (err) t.fail(err);
|
101 | t.equal(res, path.join(dir, 'grux/index.js'));
|
102 | t.equal(pkg.main, 'biz');
|
103 | });
|
104 |
|
105 | resolve('./garply', { basedir: dir }, function (err, res, pkg) {
|
106 | if (err) t.fail(err);
|
107 | t.equal(res, path.join(dir, 'garply/lib/index.js'));
|
108 | t.equal(pkg.main, './lib');
|
109 | });
|
110 |
|
111 | resolve('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
|
112 | if (err) t.fail(err);
|
113 | t.equal(res, path.join(dir, 'garply/lib/index.js'));
|
114 | t.equal(pkg.main, './lib');
|
115 | });
|
116 |
|
117 | resolve('tiv', { basedir: dir + '/grux' }, function (err, res, pkg) {
|
118 | if (err) t.fail(err);
|
119 | t.equal(res, path.join(dir, 'tiv/index.js'));
|
120 | t.equal(pkg, undefined);
|
121 | });
|
122 |
|
123 | resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
|
124 | if (err) t.fail(err);
|
125 | t.equal(res, path.join(dir, 'tiv/index.js'));
|
126 | t.equal(pkg.main, 'grux');
|
127 | });
|
128 |
|
129 | resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
|
130 | if (err) t.fail(err);
|
131 | t.equal(res, path.join(dir, 'tiv/index.js'));
|
132 | t.equal(pkg, undefined);
|
133 | });
|
134 |
|
135 | resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
|
136 | if (err) t.fail(err);
|
137 | t.equal(res, path.join(dir, 'tiv/index.js'));
|
138 | t.equal(pkg.main, './lib');
|
139 | });
|
140 |
|
141 | resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) {
|
142 | if (err) t.fail(err);
|
143 | t.equal(res, path.join(dir, 'grux/index.js'));
|
144 | t.equal(pkg, undefined);
|
145 | });
|
146 |
|
147 | resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
|
148 | if (err) t.fail(err);
|
149 | t.equal(res, path.join(dir, 'grux/index.js'));
|
150 | t.equal(pkg.main, 'tiv');
|
151 | });
|
152 |
|
153 | resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) {
|
154 | if (err) t.fail(err);
|
155 | t.equal(res, path.join(dir, 'garply/lib/index.js'));
|
156 | t.equal(pkg.main, './lib');
|
157 | });
|
158 |
|
159 | resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
|
160 | if (err) t.fail(err);
|
161 | t.equal(res, path.join(dir, 'garply/lib/index.js'));
|
162 | t.equal(pkg.main, './lib');
|
163 | });
|
164 | });
|
165 |
|
166 | test('quux', function (t) {
|
167 | t.plan(2);
|
168 | var dir = path.join(__dirname, 'resolver/quux');
|
169 |
|
170 | resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) {
|
171 | if (err) t.fail(err);
|
172 | t.equal(res, path.join(dir, 'foo/index.js'));
|
173 | t.equal(pkg.main, 'quux');
|
174 | });
|
175 | });
|
176 |
|
177 | test('normalize', function (t) {
|
178 | t.plan(2);
|
179 | var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
|
180 |
|
181 | resolve('../grux', { basedir: dir }, function (err, res, pkg) {
|
182 | if (err) t.fail(err);
|
183 | t.equal(res, path.join(dir, 'index.js'));
|
184 | t.equal(pkg, undefined);
|
185 | });
|
186 | });
|
187 |
|
188 | test('cup', function (t) {
|
189 | t.plan(5);
|
190 | var dir = path.join(__dirname, 'resolver');
|
191 |
|
192 | resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
|
193 | if (err) t.fail(err);
|
194 | t.equal(res, path.join(dir, 'cup.coffee'));
|
195 | });
|
196 |
|
197 | resolve('./cup.coffee', { basedir: dir }, function (err, res) {
|
198 | if (err) t.fail(err);
|
199 | t.equal(res, path.join(dir, 'cup.coffee'));
|
200 | });
|
201 |
|
202 | resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) {
|
203 | t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
|
204 | t.equal(err.code, 'MODULE_NOT_FOUND');
|
205 | });
|
206 |
|
207 |
|
208 | resolve('./cup', { basedir: dir, extensions: ['.js'], filename: path.join(dir, 'cupboard.js') }, function (err, res) {
|
209 | t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
|
210 | });
|
211 | });
|
212 |
|
213 | test('mug', function (t) {
|
214 | t.plan(3);
|
215 | var dir = path.join(__dirname, 'resolver');
|
216 |
|
217 | resolve('./mug', { basedir: dir }, function (err, res) {
|
218 | if (err) t.fail(err);
|
219 | t.equal(res, path.join(dir, 'mug.js'));
|
220 | });
|
221 |
|
222 | resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) {
|
223 | if (err) t.fail(err);
|
224 | t.equal(res, path.join(dir, '/mug.coffee'));
|
225 | });
|
226 |
|
227 | resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
|
228 | t.equal(res, path.join(dir, '/mug.js'));
|
229 | });
|
230 | });
|
231 |
|
232 | test('other path', function (t) {
|
233 | t.plan(6);
|
234 | var resolverDir = path.join(__dirname, 'resolver');
|
235 | var dir = path.join(resolverDir, 'bar');
|
236 | var otherDir = path.join(resolverDir, 'other_path');
|
237 |
|
238 | resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) {
|
239 | if (err) t.fail(err);
|
240 | t.equal(res, path.join(resolverDir, 'other_path/root.js'));
|
241 | });
|
242 |
|
243 | resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) {
|
244 | if (err) t.fail(err);
|
245 | t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js'));
|
246 | });
|
247 |
|
248 | resolve('root', { basedir: dir }, function (err, res) {
|
249 | t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
|
250 | t.equal(err.code, 'MODULE_NOT_FOUND');
|
251 | });
|
252 |
|
253 | resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) {
|
254 | t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
|
255 | t.equal(err.code, 'MODULE_NOT_FOUND');
|
256 | });
|
257 | });
|
258 |
|
259 | test('incorrect main', function (t) {
|
260 | t.plan(1);
|
261 |
|
262 | var resolverDir = path.join(__dirname, 'resolver');
|
263 | var dir = path.join(resolverDir, 'incorrect_main');
|
264 |
|
265 | resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
|
266 | if (err) t.fail(err);
|
267 | t.equal(res, path.join(dir, 'index.js'));
|
268 | });
|
269 | });
|
270 |
|
271 | test('without basedir', function (t) {
|
272 | t.plan(1);
|
273 |
|
274 | var dir = path.join(__dirname, 'resolver/without_basedir');
|
275 | var tester = require(path.join(dir, 'main.js'));
|
276 |
|
277 | tester(t, function (err, res, pkg) {
|
278 | if (err) {
|
279 | t.fail(err);
|
280 | } else {
|
281 | t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
|
282 | }
|
283 | });
|
284 | });
|
285 |
|
286 | test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
|
287 | t.plan(2);
|
288 |
|
289 | var dir = path.join(__dirname, 'resolver');
|
290 |
|
291 | resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
|
292 | if (err) t.fail(err);
|
293 | t.equal(res, path.join(dir, 'same_names/foo.js'));
|
294 | });
|
295 |
|
296 | resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
|
297 | if (err) t.fail(err);
|
298 | t.equal(res, path.join(dir, 'same_names/foo/index.js'));
|
299 | });
|
300 | });
|
301 |
|
302 | test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
|
303 | var testFile = path.basename(__filename);
|
304 |
|
305 | t.test('sanity check', function (st) {
|
306 | st.plan(1);
|
307 | resolve('./' + testFile, function (err, res, pkg) {
|
308 | if (err) t.fail(err);
|
309 | st.equal(res, __filename, 'sanity check');
|
310 | });
|
311 | });
|
312 |
|
313 | t.test('with a fake directory', function (st) {
|
314 | st.plan(4);
|
315 |
|
316 | resolve('./' + testFile + '/blah', function (err, res, pkg) {
|
317 | st.ok(err, 'there is an error');
|
318 | st.notOk(res, 'no result');
|
319 |
|
320 | st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
|
321 | st.equal(
|
322 | err && err.message,
|
323 | 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
|
324 | 'can not find nonexistent module'
|
325 | );
|
326 | st.end();
|
327 | });
|
328 | });
|
329 |
|
330 | t.end();
|
331 | });
|
332 |
|
333 | test('async dot main', function (t) {
|
334 | var start = new Date();
|
335 | t.plan(3);
|
336 | resolve('./resolver/dot_main', function (err, ret) {
|
337 | t.notOk(err);
|
338 | t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
|
339 | t.ok(new Date() - start < 50, 'resolve.sync timedout');
|
340 | t.end();
|
341 | });
|
342 | });
|
343 |
|
344 | test('async dot slash main', function (t) {
|
345 | var start = new Date();
|
346 | t.plan(3);
|
347 | resolve('./resolver/dot_slash_main', function (err, ret) {
|
348 | t.notOk(err);
|
349 | t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
|
350 | t.ok(new Date() - start < 50, 'resolve.sync timedout');
|
351 | t.end();
|
352 | });
|
353 | });
|
354 |
|
355 | test('not a directory', function (t) {
|
356 | t.plan(6);
|
357 | var path = './foo';
|
358 | resolve(path, { basedir: __filename }, function (err, res, pkg) {
|
359 | t.ok(err, 'a non-directory errors');
|
360 | t.equal(arguments.length, 1);
|
361 | t.equal(res, undefined);
|
362 | t.equal(pkg, undefined);
|
363 |
|
364 | t.equal(err && err.message, 'Cannot find module \'' + path + '\' from \'' + __filename + '\'');
|
365 | t.equal(err && err.code, 'MODULE_NOT_FOUND');
|
366 | });
|
367 | });
|
368 |
|
369 | test('non-string "main" field in package.json', function (t) {
|
370 | t.plan(5);
|
371 |
|
372 | var dir = path.join(__dirname, 'resolver');
|
373 | resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
|
374 | t.ok(err, 'errors on non-string main');
|
375 | t.equal(err.message, 'package “invalid main” `main` must be a string');
|
376 | t.equal(err.code, 'INVALID_PACKAGE_MAIN');
|
377 | t.equal(res, undefined, 'res is undefined');
|
378 | t.equal(pkg, undefined, 'pkg is undefined');
|
379 | });
|
380 | });
|
381 |
|
382 | test('non-string "main" field in package.json', function (t) {
|
383 | t.plan(5);
|
384 |
|
385 | var dir = path.join(__dirname, 'resolver');
|
386 | resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
|
387 | t.ok(err, 'errors on non-string main');
|
388 | t.equal(err.message, 'package “invalid main” `main` must be a string');
|
389 | t.equal(err.code, 'INVALID_PACKAGE_MAIN');
|
390 | t.equal(res, undefined, 'res is undefined');
|
391 | t.equal(pkg, undefined, 'pkg is undefined');
|
392 | });
|
393 | });
|
394 |
|
395 | test('browser field in package.json', function (t) {
|
396 | t.plan(3);
|
397 |
|
398 | var dir = path.join(__dirname, 'resolver');
|
399 | resolve(
|
400 | './browser_field',
|
401 | {
|
402 | basedir: dir,
|
403 | packageFilter: function packageFilter(pkg) {
|
404 | if (pkg.browser) {
|
405 | pkg.main = pkg.browser;
|
406 | delete pkg.browser;
|
407 | }
|
408 | return pkg;
|
409 | }
|
410 | },
|
411 | function (err, res, pkg) {
|
412 | if (err) t.fail(err);
|
413 | t.equal(res, path.join(dir, 'browser_field', 'b.js'));
|
414 | t.equal(pkg && pkg.main, 'b');
|
415 | t.equal(pkg && pkg.browser, undefined);
|
416 | }
|
417 | );
|
418 | });
|