Pathologic was missing because of a .git folder inside.
[yaffs-website] / node_modules / resolve / test / resolver.js
1 var path = require('path');
2 var test = require('tape');
3 var resolve = require('../');
4
5 test('async foo', function (t) {
6     t.plan(10);
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.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.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.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 }, function (err) {
34         t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
35         t.equal(err.code, 'MODULE_NOT_FOUND');
36     });
37 });
38
39 test('bar', function (t) {
40     t.plan(6);
41     var dir = path.join(__dirname, 'resolver');
42
43     resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
44         if (err) t.fail(err);
45         t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
46         t.equal(pkg, undefined);
47     });
48
49     resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
50         if (err) t.fail(err);
51         t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
52         t.equal(pkg, undefined);
53     });
54
55     resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
56         if (err) t.fail(err);
57         t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
58         t.equal(pkg, undefined);
59     });
60 });
61
62 test('baz', function (t) {
63     t.plan(4);
64     var dir = path.join(__dirname, 'resolver');
65
66     resolve('./baz', { basedir: dir }, function (err, res, pkg) {
67         if (err) t.fail(err);
68         t.equal(res, path.join(dir, 'baz/quux.js'));
69         t.equal(pkg.main, 'quux.js');
70     });
71
72     resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
73         if (err) t.fail(err);
74         t.equal(res, path.join(dir, 'baz/quux.js'));
75         t.equal(pkg.main, 'quux.js');
76     });
77 });
78
79 test('biz', function (t) {
80     t.plan(24);
81     var dir = path.join(__dirname, 'resolver/biz/node_modules');
82
83     resolve('./grux', { basedir: dir }, function (err, res, pkg) {
84         if (err) t.fail(err);
85         t.equal(res, path.join(dir, 'grux/index.js'));
86         t.equal(pkg, undefined);
87     });
88
89     resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
90         if (err) t.fail(err);
91         t.equal(res, path.join(dir, 'grux/index.js'));
92         t.equal(pkg.main, 'biz');
93     });
94
95     resolve('./garply', { basedir: dir }, function (err, res, pkg) {
96         if (err) t.fail(err);
97         t.equal(res, path.join(dir, 'garply/lib/index.js'));
98         t.equal(pkg.main, './lib');
99     });
100
101     resolve('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
102         if (err) t.fail(err);
103         t.equal(res, path.join(dir, 'garply/lib/index.js'));
104         t.equal(pkg.main, './lib');
105     });
106
107     resolve('tiv', { basedir: dir + '/grux' }, function (err, res, pkg) {
108         if (err) t.fail(err);
109         t.equal(res, path.join(dir, 'tiv/index.js'));
110         t.equal(pkg, undefined);
111     });
112
113     resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
114         if (err) t.fail(err);
115         t.equal(res, path.join(dir, 'tiv/index.js'));
116         t.equal(pkg, undefined);
117     });
118
119     resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
120         if (err) t.fail(err);
121         t.equal(res, path.join(dir, 'tiv/index.js'));
122         t.equal(pkg, undefined);
123     });
124
125     resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
126         if (err) t.fail(err);
127         t.equal(res, path.join(dir, 'tiv/index.js'));
128         t.equal(pkg, undefined);
129     });
130
131     resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) {
132         if (err) t.fail(err);
133         t.equal(res, path.join(dir, 'grux/index.js'));
134         t.equal(pkg, undefined);
135     });
136
137     resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
138         if (err) t.fail(err);
139         t.equal(res, path.join(dir, 'grux/index.js'));
140         t.equal(pkg, undefined);
141     });
142
143     resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) {
144         if (err) t.fail(err);
145         t.equal(res, path.join(dir, 'garply/lib/index.js'));
146         t.equal(pkg.main, './lib');
147     });
148
149     resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
150         if (err) t.fail(err);
151         t.equal(res, path.join(dir, 'garply/lib/index.js'));
152         t.equal(pkg.main, './lib');
153     });
154 });
155
156 test('quux', function (t) {
157     t.plan(2);
158     var dir = path.join(__dirname, 'resolver/quux');
159
160     resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) {
161         if (err) t.fail(err);
162         t.equal(res, path.join(dir, 'foo/index.js'));
163         t.equal(pkg.main, 'quux');
164     });
165 });
166
167 test('normalize', function (t) {
168     t.plan(2);
169     var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
170
171     resolve('../grux', { basedir: dir }, function (err, res, pkg) {
172         if (err) t.fail(err);
173         t.equal(res, path.join(dir, 'index.js'));
174         t.equal(pkg, undefined);
175     });
176 });
177
178 test('cup', function (t) {
179     t.plan(4);
180     var dir = path.join(__dirname, 'resolver');
181
182     resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
183         if (err) t.fail(err);
184         t.equal(res, path.join(dir, 'cup.coffee'));
185     });
186
187     resolve('./cup.coffee', { basedir: dir }, function (err, res) {
188         if (err) t.fail(err);
189         t.equal(res, path.join(dir, 'cup.coffee'));
190     });
191
192     resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) {
193         t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
194         t.equal(err.code, 'MODULE_NOT_FOUND');
195     });
196 });
197
198 test('mug', function (t) {
199     t.plan(3);
200     var dir = path.join(__dirname, 'resolver');
201
202     resolve('./mug', { basedir: dir }, function (err, res) {
203         if (err) t.fail(err);
204         t.equal(res, path.join(dir, 'mug.js'));
205     });
206
207     resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) {
208         if (err) t.fail(err);
209         t.equal(res, path.join(dir, '/mug.coffee'));
210     });
211
212     resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
213         t.equal(res, path.join(dir, '/mug.js'));
214     });
215 });
216
217 test('other path', function (t) {
218     t.plan(6);
219     var resolverDir = path.join(__dirname, 'resolver');
220     var dir = path.join(resolverDir, 'bar');
221     var otherDir = path.join(resolverDir, 'other_path');
222
223     resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) {
224         if (err) t.fail(err);
225         t.equal(res, path.join(resolverDir, 'other_path/root.js'));
226     });
227
228     resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) {
229         if (err) t.fail(err);
230         t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js'));
231     });
232
233     resolve('root', { basedir: dir }, function (err, res) {
234         t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
235         t.equal(err.code, 'MODULE_NOT_FOUND');
236     });
237
238     resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) {
239         t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
240         t.equal(err.code, 'MODULE_NOT_FOUND');
241     });
242 });
243
244 test('incorrect main', function (t) {
245     t.plan(1);
246
247     var resolverDir = path.join(__dirname, 'resolver');
248     var dir = path.join(resolverDir, 'incorrect_main');
249
250     resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
251         if (err) t.fail(err);
252         t.equal(res, path.join(dir, 'index.js'));
253     });
254 });
255
256 test('without basedir', function (t) {
257     t.plan(1);
258
259     var dir = path.join(__dirname, 'resolver/without_basedir');
260     var tester = require(path.join(dir, 'main.js'));
261
262     tester(t, function (err, res, pkg) {
263         if (err) {
264             t.fail(err);
265         } else {
266             t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
267         }
268     });
269 });
270
271 test('#25: node modules with the same name as node stdlib modules', function (t) {
272     t.plan(1);
273
274     var resolverDir = path.join(__dirname, 'resolver/punycode');
275
276     resolve('punycode', { basedir: resolverDir }, function (err, res, pkg) {
277         if (err) t.fail(err);
278         t.equal(res, path.join(resolverDir, 'node_modules/punycode/index.js'));
279     });
280 });